tags:

views:

31

answers:

1

I have a menu that displays say

  • Item1
  • Item2
  • Item3

etc. Clicking on Item1 should display me a Table with 2 columns.

I tried something below with displaying just a label. But the label gets displayed somewhere in the screen where as I am expecting it to be a submenu under menuitem1.

JMenu mainMenu = new JMenu("MainMenuHeader");

JMenu menuItem1 = new JMenu(new SomeClassExtendingAbstractAction("menuItem1"));

public class SomeClassExtendingAbstractAction extends AbstractAction {

public SomeClassExtendingAbstractAction(String displayText) {
    super(displayText);
}

public void actionPerformed(ActionEvent event) {
    try {
        SubMenuDialog.showDialog(parent);
    } catch (Throwable e) {

    }
}

}

public class SubMenuDialog extends JDialog {

 public SubMenuDialog(JFrame parent) {
   super();
   initialize();
 }

private void initialize() {
    JLabel label = new JLabel();
    label.setText("This is test submenu");
    getContentPane().add(label);
}

public static void showDialog(JFrame parent) {
    SubMenuDialog subMenuDialog = new SubMenuDialog(parent);
    subMenuDialog.pack();
    subMenuDialog.setVisible(true);
}

}

A: 

If I understand correctly, what you want to do is to show arbitrary component in a submenu popup. You obviously cannot use JDialog as it would give you a, you know, JDialog.

Your SubMenuDialog#showDialog can create a JPopupMenu, add any component to it (for example JScrollPane containing a table, or JPanel with labels), and show it where a normal submenu would be.

However it's not a good idea in general to show complicated things in a popup, since it's too easy to lose the popup, plus you may get focus problems with components inside the popup.

I'd suggest you consult a UI designer for the best UI representation of the functionality you want to implement.

Geoffrey Zheng
I am wanting my submenu to be similar to browser's View - Toolbar - Standard Toolbar/Navigation Toolbar etc. Can I still use JPopupMenu in that case?
Which browser? I don't see that combination in Firefox 3.6, IE 8, or Chrome 6. Firefox View - Toolbar just shows a bunch of check box menu items, which is what `JCheckBoxMenuItem` is for.
Geoffrey Zheng
Yea. I want mine to look like View Toolbars without checkbox. Instead I want it to be a JTable. Instead of Action/Jdialog, I tried to use a JMenuItem. Cant figure out how to add a Jtable to JMenuItem