自定义 Java JTree 实现

发布于 2024-08-24 13:53:01 字数 795 浏览 12 评论 0原文

有 2 个 JTreeJTree1JTree2。请注意,节点(国家、城市、颜色、蓝色...)都将实现为 JCheckboxes,以便用户可以通过选择相应的复选框来为每个城市或整个国家选择特定的颜色。

问题:

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-08-31 13:53:01

所以为了这个目的,JTree就是
(其节点作为复选框)正确
实施的选项或者我应该使用
其他一些 JComponent?

不太确定你的意思,但我个人不会使用 JTree 在右侧显示选项。我认为呈现一个 JPanel 包含此特定情况下的选项。对于您的示例,左侧似乎很好,尽管我真的不知道树中将包含什么类型的数据。

如果 JTree 是正确的选项,那么如何
我能记住每个的颜色吗
城市?

请注意,我将做出几个假设:

  1. 包含您的国家/地区和城市的左侧仍然是 JTree 并且右侧仍然可以是 JTreeJPanel.
  2. 您希望选项完全按照用户上次设置的方式显示,然后再选择左侧的不同节点。

实现此目的的最简单方法是添加 TreeSelectionListener 到树(包含国家和城市的树)选择模型。 TreeSelectionListener 提供了 TreeSelectionEvent,它提供选择的节点和将成为选择的节点。这将使您有机会提取为选择更改为选择更改为的节点设置的颜色设置。 TreeSelectionListener 应添加到 TreeSelectionModel 是从 获取的JTree,通过调用其 getSelectionModel 方法。

如果您使用此技术,当您使用最后选择的选项执行操作时,您需要在执行操作之前再次获取这些选项。例如,如果您有“保存”按钮,则应检查在左侧选择的节点的提取颜色设置。这是为了捕获用户可能做出的侦听器未捕获的任何更改(因为仅当左手选择更改时才会触发侦听器)。

如果您需要一个示例,我在 http://www.box.net/shared/ 上编写了一个示例hgbet4uf6k

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文