开发者

pass radiobutton value that selected to another class

is there any example for me to pass radiobutton value that selected to another class?

jr1 = ne开发者_JAVA百科w JRadioButton ("11.40 AM");
    jr2 = new JRadioButton ("12.00 PM");
    jr3 = new JRadioButton ("1.40 PM");
    jr4 = new JRadioButton ("3.40 PM");
    jr5 = new JRadioButton ("5.40 PM");
    jr6 = new JRadioButton ("7.00 PM");
    jr7 = new JRadioButton ("9.00 PM");
    jr8 = new JRadioButton ("10.40 PM");
    jr9 = new JRadioButton ("11.40 PM");
    jr10 = new JRadioButton ("12.40 AM");

ButtonGroup group = new ButtonGroup ();
    group.add(jr1);
    group.add(jr2);
    group.add(jr3);
    group.add(jr4);
    group.add(jr5);
    group.add(jr6);
    group.add(jr7);
    group.add(jr8);
    group.add(jr9);
    group.add(jr10);

im using this kind of way...right now i need to know how should i add for example; if i pick 12.00 pm... it will pass the value of 12.00 to another class...TQ


If you look at the Swing Tutorial part on Buttons ...

Have the other class implement ActionListener and create this method

public void actionPerformed(ActionEvent e) {
    // do something
}

Make sure that on your Radio Button you call

radioButton.addActionListener(otherClass);  

EDIT: To answer getting the text of the button in the question do this in actionPerformed call getSource() on the ActionEvent and that will tell you which button fired the event. It is simply a matter of getting the text from the button (I think that is getText() but not sure.)


The simplest way is to pass the return value of isSelected() method to the object:

MyButtonWatcherClass watch = new MyButtonWatcherClass(); // not a real class, just an example
JRadioButton radioButton = new JRadioButton("Simple Radio Button");
...
watch.processRadioButtonState(radioButton.isSelected()); // not a real method, just an example

Where to put this code is depended on you, but that's the main concept.

Watching your code, I can't resist - use array:

String hours[] = {"11.40 AM", "12.00 PM" .... };
JRadioButton jrb[] = new JRadioButton[hours.length];
ButtonGroup group = new ButtonGroup ();
...
for (int i = 0; i < hours.length; i++)
{
    jrb[i] = new JRadioButton(hours[i]);
    group.add(jrb[i]);
}

Here, I said it :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜