2

Say I wanted to add a JComboBox (or more general a JPanel, perhaps?) to a JRadioButton, what would be the easiest way to do it?

Pseudo-wise, a radio button group where one of them includes multiple choices would look something like:

O The weather
O Parties
O {meta, pseudo}-science
O Animals

where the {} would be a dropdown list. The trick here is that if one clicks either the dropdown list or the label '-science' the radio button would be actived and showing the UI border and all of that fancy stuff.

Thanks :)

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Juhl
  • 473
  • 4
  • 17

2 Answers2

3

I hate giving answers like this, but in this case I feel it is best...

This seems like a very non-standard UI component. It would be much better UX if you just did:

O The weather
O Parties
O meta-science
O pseudo-science
O Animals

Users will not be familiar with the type of component you are proposing, and it is very inconsistent with the other options in the list. I highly recommend using a more standard convention.


Against my better judgement, I present you with ComboBoxRadioButton:
It is not complete, nor do I suggest using it, but it looks like what you want.

import java.awt.FlowLayout;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;

public class ComboBoxRadioButton extends JRadioButton {

    private JLabel beforeText, afterText;
    private JComboBox comboBox;

    public ComboBoxRadioButton(String beforeTxt, JComboBox comboBox, 
                                             String afterText) {
        this.comboBox = comboBox;
        this.beforeText = new JLabel("    " + beforeTxt);
        this.afterText = new JLabel(afterText);
        comboBox.setSelectedIndex(0);
        setLayout(new FlowLayout());
        setModel(new JToggleButton.ToggleButtonModel());
        add(this.beforeText);
        add(this.comboBox);
        add(this.afterText);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel mainPane = new JPanel();
        ButtonGroup group = new ButtonGroup();
        AbstractButton b2 = new JRadioButton("Java Swing");
        AbstractButton b3 = new ComboBoxRadioButton(
                "It's gonna be a", new JComboBox(new String[] { "good", "bad",
                "rainy" }), "day!");
        AbstractButton b4 = new JRadioButton("After the combo");
        group.add(b2);
        group.add(b3);
        group.add(b4);
        mainPane.add(b2);
        mainPane.add(b3);
        mainPane.add(b4);
        f.add(mainPane);
        f.pack();
        f.setVisible(true);
    }
}
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • Yeah non-standard UI FTW, why should I conform? :) Your solution is simple although for "lots" of options, and assuming the other radio button options are really related and relevant, I don't think it is a viable solution. Also, I would really like to see a Java Guru throw in some nice code to show off :) – Juhl Jan 27 '11 at 21:34
  • @Juhl, you asked for it, so here it is. It is not complete, nor do I suggest using it. See my edit. – jjnguy Jan 27 '11 at 22:48
0

I like Justin's answer, but another alternate suggestion:

Put all the options in a single JComboBox.

If you really want to want to take the route from your question it is possible. The best way to achieve this will be to:

  • Create a JPanel that has a JRadioButton on the left, Combo in the middle and label to the right.
  • Add a mouse listener to catch clicks on the panel.
  • Tweak the border, layout, and possibly other UI items to make it look nice.
jzd
  • 23,473
  • 9
  • 54
  • 76
  • Yeah thought about it, but what about cross-platform L&F of the component? I guess the hard way to go is to extend either _javax.swing.AbstractButton_ or _javax.swing.JToggleButton_ like _JRadioButton_ does and then design the behaviour from the bottom up. – Juhl Jan 27 '11 at 21:42
  • @Juhl, not sure how far you are but if you specify a look and feel like Nimbus, then cross platform look will be much closer than something like the Default OS look and feel. – jzd Jan 27 '11 at 22:30