简体   繁体   中英

How to make a menu like a JTree menu in Java?

I want to make a menu which can be expanded. For example, the structure is so:

Item 1
Item 2
|- Sub item
|- Sub item

Item 3
Item 4

But how do I do that? Do I need to go about overwriting things like JTree? Or another component to get this working? Or is it better to just work with other components and event listeners? The menu is not used to show file structures, but for showing pages and subpages.

Assuming you're looking for a popup menu (JMenu), you can call add on your existing menu with another JMenu.

Eg

JMenu outer = new JMenu();
outer.add(new JMenuItem("item 1"));

JMenu nested = new JMenu("Nested");
nested.add(new JMenuItem("inner item"));
outer.add(nested);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM