質問

Using radio buttons displayed on a panel, is it possible to select the radio button and then display some text on the panel explaining what the user has selected?

So here is a list of radio buttons

    public void RadioButtons() {
    btLdap = new JRadioButton ("Ldap");
    btLdap.setBounds(60,85,100,20);
    panelHolder.add(btLdap);

    btKerbegos = new JRadioButton ("Kerbegos");
    btKerbegos.setBounds(60,115,100,20);
    panelHolder.add(btKerbegos);

    btSpnego =new JRadioButton("Spnego");
    btSpnego.setBounds(60,145,100,20);
    panelHolder.add(btSpnego);

    btSaml2 = new JRadioButton("Saml2");
    btSaml2.setBounds(60,175,100,20);
    panelHolder.add(btSaml2);
}

User selects btLdap

btLdap.setSelected(true);

Now how do you make the text appear on the panel not a message box

役に立ちましたか?

解決

If you want to display a text when a radio button is selected you could use ActionListener.

final JTextArea textArea = new JTextArea();
add(textArea);

JRadioButton radioButton = new JRadioButton();
add(radioButton);
radioButton.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        textArea.setText("Selected");
    }
});

JRadioButton radioButton2 = new JRadioButton();
add(radioButton2);
radioButton2.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        textArea.setText("Selected 2");
    }
});

radioButton.setSelected(true);

When the first is selected it will change the text of the JTextArea with The first is selected!, same the second radio button but with The second is selected.

As you said, radioButton.setSelected(true); setSelected is used to select/deselect a radio button.

In this example i used textArea, but you can use everything which have a method to change the text it contains (an image too!)

Official DOC, here.


Anyway, actionPerformed is not called when setSelected is used so i would go to something like a method

private void updateText(int index)
{
    String text = null;

    switch (index)
    {
        case 0:
            text = "Selected";
            break;
        case 1:
            text = "Selected 2";
            break;
    }

    textArea.setText(text);
}

And then call updateText(0 or 1 etc.) when you want to select setSelected another radio button and update the text too.

All this is useful, if you want to show a "what happens if you press it" message, but if you just want to change the text of the area with the text of the radio button, just use

textArea.setText(e.getActionCommand());

他のヒント

Here is some Example how to use an ActionListener on a JRadioButton:

public class ListenerExample extends JFrame implements ActionListener {

private JRadioButton check = new JRadioButton("hello");
private JLabel label = new JLabel();

public ListenerExample() {
    check.addActionListener(this);

    add(check);
    add(label);

    setLayout(new FlowLayout());
    setSize(800, 600);
    setVisible(true);
}

public static void main(String[] args) {
    new ListenerExample();
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof JRadioButton) {
        JRadioButton button = (JRadioButton) e.getSource();
        label.setText(String.valueOf(check.isSelected()));
    }
}

}

Using an anonymous inner class you will have something like:

yourRadioButton.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent event ) {

        // here you will write the code that you want to
        // be executed when your radio is clicked
        yourLabel.setText( "Some text..." );


        // if you want to exeute it onlye when it is selected
        // you will need to do this
        if ( yourRadioButton.isSelected() ) {
            // some code here
        }

    }
});

In Java 8, recently released, you can use a lambda expression to register your listener. Something like:

yourRadioButton.addActionListener( event -> {

    // here you will write the code that you want to
    // be executed when your radio is clicked
    yourLabel.setText( "Some text..." );


    // if you want to exeute it onlye when it is selected
    // you will need to do this
    if ( yourRadioButton.isSelected() ) {
        // some code here
    }

});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top