自定义 Java JTree 实现
有 2 个 JTree
:JTree1
和 JTree2
。请注意,节点(国家、城市、颜色、蓝色...)都将实现为 JCheckbox
es,以便用户可以通过选择相应的复选框来为每个城市或整个国家选择特定的颜色。
问题:
Q1。我希望每个国家或城市都可以选择自己的颜色。意味着如果用户希望 city1.1
的颜色为蓝色和紫色,而 city2.1
的颜色为红色,那么他首先必须选择 city1.1
复选框,然后选择蓝色和紫色,之后当选择 city2.1
时,蓝色和紫色复选框会自动取消选中,以便用户可以选择 city2 的颜色.1
。但是,当用户再次选择 city1.1
时,JTree2
应显示为 city1.1 选择的颜色(蓝色和紫色)。
因此,出于此目的,JTree(其节点作为复选框)是正确的实现选项还是我应该使用其他
JComponent
?如果 JTree 是正确的选项,那么我如何记住每个城市的颜色?
There are 2 JTree
: JTree1
and JTree2
. Note that the nodes (country, city, colors, blue ...) all will be implemented as JCheckbox
es 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 JTree2
should 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不太确定你的意思,但我个人不会使用 JTree 在右侧显示选项。我认为呈现一个 JPanel 包含此特定情况下的选项。对于您的示例,左侧似乎很好,尽管我真的不知道树中将包含什么类型的数据。
请注意,我将做出几个假设:
实现此目的的最简单方法是添加 TreeSelectionListener 到树(包含国家和城市的树)选择模型。 TreeSelectionListener 提供了 TreeSelectionEvent,它提供被选择的节点和将成为选择的节点。这将使您有机会提取为选择从更改为选择更改为的节点设置的颜色设置。 TreeSelectionListener 应添加到 TreeSelectionModel 是从 获取的JTree,通过调用其 getSelectionModel 方法。
如果您使用此技术,当您使用最后选择的选项执行操作时,您需要在执行操作之前再次获取这些选项。例如,如果您有“保存”按钮,则应检查在左侧选择的节点的提取颜色设置。这是为了捕获用户可能做出的侦听器未捕获的任何更改(因为仅当左手选择更改时才会触发侦听器)。
如果您需要一个示例,我在 http://www.box.net/shared/ 上编写了一个示例hgbet4uf6k。
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.
Note, I'm going to make a couple of assumptions:
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.