Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-05-2007, 09:42 PM
Member
 
Join Date: Dec 2007
Posts: 20
Rep Power: 0
hemanthjava is on a distinguished road
Default Dialog and JList Doubts
Hi Guys,
I was doing a Mail Program and Got stuck in the following questions. I don't know How to approach.

I require a Right Click pop Up Menu for Attachments List and adding pop up listener for this. Some kind of a right click context menu for each item in the list box having attachment file names to "add" or "remove" from the list


Code:
/*
 1. Right Click pop Up Menu for Attachments List and adding pop up listener for this.
 2. Cancel Button Needs to be clicked twice to Close the dialog
 3. Close Button for dialog not working 
 4. Adding a new attachment subsequent time will remove the previous Attachment
 */
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class TestClient extends JDialog {

	private JPanel mailPanel = null;
	private JTextArea jtaMessage = null;
	private JButton jbtCancel = null;
	private JButton jbtAttachment = null;
	private JList jlstAttachedFiles = null;
	private JScrollPane jlstAttachedFilesPane = null;
	private JFileChooser jfcOpenChooser = null;
	private JPopupMenu jpopupAttachment = null;
	private JMenuItem jmenuitemAddAttachment = null;
	private JMenuItem jmenuitemRemoveAttachment = null;
	private HashMap attachedFiles = null;
	private static final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
	public TestClient(Frame frame, String title, boolean modal) {
		super(frame, title, modal);
		init();
	}
	public TestClient() {
		this((Frame) null, "", false);
	}
	private void init() {
		try {
			UIManager.setLookAndFeel(windows);
		} catch (Exception e1) {
			;
		}
		JFrame.setDefaultLookAndFeelDecorated(true);
		this.setSize(400, 400);
		this.setLayout(new GridBagLayout());
		GridBagConstraints toolbarCons = new GridBagConstraints();
		toolbarCons = new GridBagConstraints(0, 0, 1, 1, 0.1, 0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(5, 5, 0, 5), 0, 0);
		jlstAttachedFiles = new JList();
		jlstAttachedFilesPane = new JScrollPane(jlstAttachedFiles);
		jlstAttachedFilesPane.setPreferredSize(new Dimension(150, 75));
		jbtAttachment = new JButton("Attachments");
		jbtCancel = new JButton("Cancel");
		jtaMessage = new JTextArea();
		jtaMessage.setPreferredSize(new java.awt.Dimension(300, 300));
		JScrollPane mailMessage = new JScrollPane(jtaMessage);
		jtaMessage.setLineWrap(true);
		jfcOpenChooser = new JFileChooser();
		jpopupAttachment = new JPopupMenu();
		jpopupAttachment.add(new JMenuItem("Add Attachment"));
		jpopupAttachment.add(new JMenuItem("Remove Attachment"));
		jmenuitemAddAttachment = new JMenuItem("Add Attachment");
		jmenuitemRemoveAttachment = new JMenuItem("Remove Attachment");
		mailPanel = new JPanel();
		mailPanel.setLayout(new GridBagLayout());
		GridBagConstraints cons = new GridBagConstraints();
		cons = new GridBagConstraints(0, 4, 1, 1, 0.2, 0.1,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(jbtAttachment, cons);
		cons = new GridBagConstraints(1, 4, 4, 1, 0.8, 0.1,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(jlstAttachedFilesPane, cons);
		cons = new GridBagConstraints(1, 5, 4, 4, 0.8, 0.4,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(mailMessage, cons);
		cons = new GridBagConstraints(1, 9, 1, 1, 0.2, 0.1,
				GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(5, 5, 0, 5), 0, 0);
		mailPanel.add(jbtCancel, cons);
		jbtCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				TestClient.this.dispose();
			}
		});
		jbtAttachment.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jbtAttachment_actionPerformed(e);
			}
		});
		this.getContentPane().add(mailPanel, toolbarCons);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		GridBagConstraints mainPanelCons = new GridBagConstraints();
		mainPanelCons = new GridBagConstraints(0, 1, 1, 1, 0.1, 0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(5, 5, 0, 5), 0, 0);
		this.getContentPane().add(mailPanel, mainPanelCons);
		this.pack();
		this.setVisible(true);
	}
	private void jbtOK_actionPerformed(ActionEvent e) {
	}
	private void jbtAttachment_actionPerformed(ActionEvent e) {
		jfcOpenChooser.setMultiSelectionEnabled(true);
		jfcOpenChooser.showOpenDialog(null);
		File[] fileArray = jfcOpenChooser.getSelectedFiles();
		String[] fileNames = new String[fileArray.length];
		attachedFiles = new HashMap();
		for (int i = 0; i < fileArray.length; i++) {
			File file = fileArray[i];
			attachedFiles
					.put(file.getPath().trim(), file.getName().trim()); // (Path,
																		// Name)
			fileNames[i] = file.getName().trim();
		}
		jlstAttachedFiles.setListData(fileNames);
	}
	public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(windows);
		} catch (Exception e1) {
			;
		}
		TestClient mc = new TestClient(null, "Mail Client", true);
	}
}

Last edited by hemanthjava; 12-05-2007 at 10:21 PM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
searching within a JList newtojava7 New To Java 1 03-10-2008 01:12 AM
Applet doubts anithababu Java Applets 0 01-18-2008 06:09 AM
Help with JList Albert NetBeans 1 07-13-2007 04:42 PM
Doubts with the method handleEvent() Albert Advanced Java 1 07-06-2007 06:12 PM
add a jlist column Alan JCreator 1 05-28-2007 05:51 AM


All times are GMT +2. The time now is 08:33 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org