views:

116

answers:

1
+1  A: 

Hi,

So for this purpose, Is the JTree (with its nodes as checkboxes) correct option to implement or I should use some other JComponent?

Not exactly sure what you meant, but I, personally, would not use a JTree to present the options on the right hand side. I think it is much simpler to present a JPanel that contains the options in this particular case. Left side seems fine for your example, although I don't really know what sort of data is going into the tree.

If JTree is a correct option, then how can I remember the colors of each city?

Note, I'm going to make a couple of assumptions:

  1. The left side that contains your countries and cities remains a JTree and the right hand side can still be a JTree or a JPanel.
  2. You want the options to appear exactly as the user last set it before they select a different node on the left hand side.

The simplest way of achieving this is to add a TreeSelectionListener to the tree's (the one containing the countries and cities) selection model. The TreeSelectionListener is provided with a TreeSelectionEvent which provides the node that was selected and the node that will become selected. This will provide you with the opportunity to extract the colour settings that were set for the node that the selection is changing from to the one that the selection is changing to. The TreeSelectionListener should be added to the TreeSelectionModel that is obtained from the JTree, by calling its getSelectionModel method.

If you use this technique, when you to perform the operation with the last selected options, you'll need to get the options one more time before you perform the operation. For example, if you had a "Save" button, you should check extract the colour settings for which node is selected on the left. This is to capture any changes that the user may have made that the listener has not captured (since the listener is triggered only when the left hand selection changes).

If you need an example, I've written one at http://www.box.net/shared/hgbet4uf6k.

Klarth