3

I'd like to create a JTree that more or less has the following structure (with a hidden root node)

[No Option] Main Dish
    [Radio Button] Steak
    [Radio Button] Fish
    [Radio Button] Filet Mignon
[Checkbox] Side Dish
    [Checkbox] Mashed Potatoes
    [Checkbox] Green Beans
    [Checkbox] Mixed Vegetables
[Checkbox] Dessert
    [Radio Button] Ice Cream
    [Radio Button] Pudding
    [Radio Button] Cake

Basically, at least one main dish (but no more than one) can be chosen, any number of side dishes can be chosen (or none, if the "Side Dish" checkbox is unchecked), and only one dessert can be chosen (or none, if the "Dessert" checkbox is unchecked).

This article describes how to create a tree that uses JCheckboxes (as I'd like to use for the Side Dish and Dessert sections) by creating a custom Renderer and page 3 of the same article describes how to use Radio buttons (as I'd like to use for the Main Course) section. But it doesn't describe how to mix them up within the same JTree.

Is it possible to create a structure like this? And if so, how?

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • +1 never to see JRadioButton in ButtonGroup as Renderer & Editor in JTree – mKorbel Dec 07 '12 at 21:31
  • 2
    Rely on the model to regulate the rules. The issue here is then updating all the other nodes when one is changed. – MadProgrammer Dec 07 '12 at 23:38
  • 1
    @MadProgrammer I would make your comment as answer. Depending on condition the renderer and editor should return either JCheckBx or JRadioButton. The setValue() method in the model should be overriden to add a check should be added to deselect previously active radio button – StanislavL Dec 08 '12 at 09:34

0 Answers0