如何为每个JTree节点添加一个不同的JComboBox? [英] How do I add a different JComboBox to each JTree node?

查看:88
本文介绍了如何为每个JTree节点添加一个不同的JComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTree,我正在为自己编写的Game Database程序填充技能.

有几个类别和子类别(实际技能),然后将其下的技能级别提高(有时).目前,我正在使用一个技能类别,一些内部选项和一些枚举以及一种检查该技能是否为类别的方法(称为isCategory)来模拟该技能.另外两件事要注意:

  1. 不同类型的技能的行为会有所不同.
    有些是一次购买的,有些是几次购买的,有些可以选择,等等.
  2. 不同类别包含不同的技能.
    例如,武器技能"类别具有不同类型的武器,但装甲技能"位于不同的部分.

我已经看到了一个非常好的示例,该示例是通过单元格渲染器将ComboBox附加到树中的每个节点上的.这是我找到的示例.

我理解上面的代码,但是看不到如何将组合框附加到节点而不是树上?我已经读过"如何使用树",并且我运行,然后查看了 demos中的一些代码用于树.我可以制作基本的树,但是我发现这些教程有点晦涩难懂,并且缺乏足够的细节来自己寻找如何进行的方法.我发现了另一个示例,其中仅将叶节点呈现为复选框,更复杂.

很明显,我想将两者结合起来,能够拥有不同类别的人,这些人具有不同的技能,而不同的技能则具有不同的熟练程度.但是,我可以想到的唯一方法是针对不同类型使用不同的JComboBoxModels,但是我不知道如何执行此操作,而且我也找不到方法.我试图编辑复选框示例以使用ComboBoxes,但是对于我来说,我无法弄清楚.

有人会给我一个提示,告诉我采取什么方法,因为我是Java的新手,正在努力弄清楚该怎么做?

解决方案

是个好地方开始吗?

从概念上讲,是的. JTableJTree都使用flyweight模式来渲染和编辑单元格/节点.

  • 示例引用了基本的TreeCellRenderer.
  • 示例说明了使用默认渲染器的简单TreeCellEditor.
  • 示例显示了一个Outline视图,该视图结合了JTableJTree的功能.

I have a JTree that I'm filling with skills for a Game Database programme that I'm writing.

There are several categories, and subcategories (actual skills), and then levels skill below that (sometimes). Currently I'm emulating this with one skill class, some options inside, and a few enums, plus a method to check that if the skill is a category (called isCategory). Two other things to note:

  1. Different types of skill behave differently.
    Some are bought once, other several times, some have options to pick from etc
  2. Different categories contain different skill.
    For instance The Weapon Skills category has different types of weapons, but the Armour Skills are in a different section.

I've seen a very good example of attaching a ComboBox via a cell renderer to each node in the tree. Here is the example I found.

I understand the above code, but I can't see how to would attach the combo box to a node, and not to the tree? I've read 'How to use Trees', and I've run, and looked at the code for a few of the demos for tree. I can make basic trees, but I find the tutorials a bit obtuse and lacking in enough detail to figure out for myself how to proceed. I've found another example of only rendering leaf nodes as checkboxes, which is much more complex.

Obviously I'd like to combine the two, being able to have different categories able to have different skills and different skills have different levels of proficiency. However the only way I can think of doing so it to have different JComboBoxModels for the different types but I don't know how to do this, and I can't find out how. I've tried to edit the checkbox example to use ComboBoxes, but for the life of me I can't figure it out.

Could someone give me a hint as to what approach to take on this, as I'm new to Java and strugglingto figure out what to do?

解决方案

Would this be a good place to start?

Conceptually, yes. Both JTable and JTree use the flyweight pattern to render and edit cells/nodes.

  • This example cites a basic TreeCellRenderer.
  • This example illustrates a simple TreeCellEditor using the default renderer.
  • This example shows an Outline view that incorporates features of both JTable and JTree.

这篇关于如何为每个JTree节点添加一个不同的JComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆