Amit
Amit

Reputation: 34793

Custom Java JTree implementation

There are 2 JTree: JTree1 and JTree2. Note that the nodes (country, city, colors, blue ...) all will be implemented as JCheckboxes so that user can select particular colors for each city or for the whole country by selecting their corresponding checkboxes.

Problem:

Q1. I want that each country or city can have its own colors selected. Means if a user wants city1.1 to have colors blue and violet and city2.1 to have colors red, then he first have to select the city1.1 checkbox and then select blue and violet, and after that when he selects city2.1, then the checkboxes blue and violet are deselected automatically so that user can select the colors for city2.1. But when the user selects the city1.1 again, then the JTree2should show the selected colors (bule and violet) for city1.1.

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

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

Upvotes: 1

Views: 1206

Answers (1)

Klarth
Klarth

Reputation: 2045

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.

Upvotes: 2

Related Questions