0

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?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

2 Answers2

3

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).

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • So Implement both of those in my skills class? How are they different? Could you expand on what you mean a little please? – AncientSwordRage Aug 03 '12 at 12:04
  • It could be one class implementing both interfaces. In fact you need the 2 methods to get component for renderer and editor. – StanislavL Aug 03 '12 at 12:18
  • 1
    @Pureferret: This [example](http://stackoverflow.com/a/11113648/230513) illustrates a `TreeCellEditor` using the default renderer. – trashgod Aug 03 '12 at 12:30
  • @trashgod Am I thinking rightly that all I need to do is edit `getTreeCellEditorComponent` to return a different comboBox depending on checks I make, instead of `user` in your example? @Both how does `getTreeCellRenderingComponent` factor into this, just overloading the toString method? I.e. for a combo: `getSelected().getText()` (or something similar)? Is there a tutorial on this somewhere, the Java tutorials don't really seem to cover this in any great detail. Would [this](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) be a good place to start? – AncientSwordRage Aug 03 '12 at 12:53
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.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045