Results 1 to 1 of 1
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    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


    Java 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.

Similar Threads

  1. searching within a JList
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 01:12 AM
  2. Applet doubts
    By anithababu in forum Java Applets
    Replies: 0
    Last Post: 01-18-2008, 06:09 AM
  3. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 04:42 PM
  4. Doubts with the method handleEvent()
    By Albert in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 06:12 PM
  5. add a jlist column
    By Alan in forum JCreator
    Replies: 1
    Last Post: 05-28-2007, 05:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •