How do I add a different JComboBox to each JTree node?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 12:12:08

问题


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?


回答1:


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.




回答2:


You should implement TreeCellRenderer as well as TreeCellEditor. Both should return JComboBox with different model. Which model to choose you should decide in the getTreeCellEditorComponent/getTreeCellRenderingComponent depending on value parameter (the tree node in fact).



来源:https://stackoverflow.com/questions/11794376/how-do-i-add-a-different-jcombobox-to-each-jtree-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!