Results 1 to 1 of 1
Thread: Dialog and JList Doubts
- 12-05-2007, 08:42 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 22
- Rep Power
- 0
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 09:21 PM.
Similar Threads
-
searching within a JList
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-10-2008, 12:12 AM -
Applet doubts
By anithababu in forum Java AppletsReplies: 0Last Post: 01-18-2008, 05:09 AM -
Help with JList
By Albert in forum NetBeansReplies: 1Last Post: 07-13-2007, 03:42 PM -
Doubts with the method handleEvent()
By Albert in forum Advanced JavaReplies: 1Last Post: 07-06-2007, 05:12 PM -
add a jlist column
By Alan in forum JCreatorReplies: 1Last Post: 05-28-2007, 04:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks