user2190492
user2190492

Reputation: 1202

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.

Upvotes: 0

Views: 367

Answers (1)

rbakhru
rbakhru

Reputation: 61

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

E.g.

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

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

Upvotes: 2

Related Questions