0

I'm a web developer and I'm new in Java and I want know how to change content of a JPanel when choice any node of JTree or any item of menu. Because I don't want display many windows when click in any option but will reload only part of the program as if it were an ajax call in Web.

IE I want to display different content in the JPanel according to the option chosen, but now how.

If you could give me some tips, items to use, a better option to do or some example, greatly appreciate.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
SoldierCorp
  • 7,610
  • 16
  • 60
  • 100

1 Answers1

2

If you're a web developer, then it should be simple, because it works in a similar way as in JavaScript: you need to register an event listener on the tree (TreeSelectionListener) and on the menu item (ActionListener). When the user selects a node or chooses the menu item, the event you're interested in (node selected, or action performed on the menu item) will be fired, your listener will be called, and this listener just needs to replace the panel content with the new one.

Read the tutorial about JTree and the tutorial about JMenu for more guidance.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yeah but How replace or update content of Panel? IE, when choice another option, remove current content elements on Panel and update with a new content? – SoldierCorp Nov 23 '12 at 22:17
  • I suggest using a container panel containing a unique content panel. When the menu item is clicked, create a brand new content panel, populate it with the components you need, and remove the current content panel from the container panel, and add the new content panel to the container panel. Or, if you don't want to recreate a new content panel each time, you could use a [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) and ask it to show the appropriate panel. – JB Nizet Nov 23 '12 at 22:22
  • With your explanation you have given me another idea, think CardLayout is used in this example? http://www.randelshofer.ch/quaqua/jws/quaqua-test.jnlp because that's how I want to do – SoldierCorp Nov 24 '12 at 03:12
  • No idea. It could use one. Or it could generate the panels on the fly. – JB Nizet Nov 24 '12 at 07:57