radioButton question

Hello. I have a question. how can i simplify my code. this is ridiculous. i was trying to learn about radiobuttons and thought to write a simple code. Good Jesus took me 30 to 40 min almost. Just because resizing, setting the color and adding everything all together.

is there a simple way or may be another way to write my program?. I wish i have JBuilder so i can build swing programs easily by using drag and drop. eclipse sucks in swing and same as netbeans. they dont have the actionEvent method for you. with JBuilder you have everything that is why it is so expensive and efficient.

Thanks for your help.

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

publicclass RadioButtonExampleextends JFrame{

privatestaticint FRAME_WIDTH = 300;

privatestaticint FRAME_HEIGHT = 200;

private JPanel panel;

private JTextField text;

private JButton button;

private JRadioButton radio1;

private JRadioButton radio2;

private JRadioButton radio3;

private ButtonGroup group;

privateint choice;

publicstaticvoid main(String[] args){

RadioButtonExample frame =new RadioButtonExample();

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public RadioButtonExample(){

setSize(FRAME_WIDTH, FRAME_HEIGHT);

Container contentPane = getContentPane();

panel =new JPanel(new GridLayout(5,1));

button =new JButton("Click me");

button.setForeground(Color.MAGENTA);

button.setSize(10,10);

text =new JTextField(5);

radio1 =new JRadioButton("Radio1");

radio2 =new JRadioButton("Radio2");

radio3 =new JRadioButton("Radio3");

text.setText("");

group =new ButtonGroup();

group.add(radio1);

group.add(radio2);

group.add(radio3);

panel.add(radio1);

panel.add(radio2);

panel.add(radio3);

panel.add(text, BorderLayout.CENTER);

panel.add(button, BorderLayout.SOUTH);

contentPane.add(panel);

ActionListener radio1Listener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

choice = 1;

};

};

radio1.addActionListener(radio1Listener);

ActionListener radio2Listener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

choice = 2;

};

};

radio2.addActionListener(radio2Listener);

ActionListener radio3Listener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

choice = 3;

};

};

radio3.addActionListener(radio3Listener);

ActionListener okListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

switch(choice)

{

case 1:

text.setText("you press radio1");

break;

case 2:

text.setText("You press radio2");

break;

case 3:

text.setText("You press radio3");

break;

}

};

};

button.addActionListener(okListener);

}

}

[5704 byte] By [lrngjavaa] at [2007-11-27 4:42:47]
# 1

Looks simple enough to me. But you could make a method that takes a couple parameters (like the label) and will create the radio button, add it to the group, and add it to the panel.

You also don't need the 3 radio button actionlisteners. You can just have the okay button listener check the isSelected state of the radio buttons directly.

But I've seen a lot more complex UI code then that.

bsampieria at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 2
ok let me try. thanks for the input.
lrngjavaa at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 3

now here is my code a little imporved. but now its not updating text.

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class RadioButtonExample extends JFrame{

private static int FRAME_WIDTH = 400;

private static int FRAME_HEIGHT = 300;

private JPanel panel;

private JTextField text;

private JButton button;

private ButtonGroup group;

private int choice;

private JRadioButton[] radioButton;

public static void main(String[] args) {

RadioButtonExample frame = new RadioButtonExample();

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public RadioButtonExample() {

setSize(FRAME_WIDTH, FRAME_HEIGHT);

Container contentPane = getContentPane();

String[] btnText = {"radio1", "radio2", "radio3"};

panel = new JPanel(new GridLayout(5,1));

panel.setBorder(BorderFactory.createTitledBorder("pick one "));

button = new JButton("Click me");

button.setForeground(Color.MAGENTA);

button.setSize(10,10);

text = new JTextField(5);

text.setText("");

group = new ButtonGroup();

radioButton = new JRadioButton[btnText.length];

for(int i=0; i<radioButton.length; i++) {

radioButton[i] = new JRadioButton(btnText[i]);

group.add(radioButton[i]);

panel.add(radioButton[i]);

}

radioButton[0].setSelected(true);

contentPane.add(panel,BorderLayout.NORTH);

contentPane.add(text, BorderLayout.CENTER);

contentPane.add(button, BorderLayout.SOUTH);

ActionListener radio1Listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

int i=0;

while(text == null) {

if(radioButton[i].isSelected()) {

text.setText("You selected " + radioButton[i]);

}

i++;

}

};

};

button.addActionListener(radio1Listener);

}

}

>

lrngjavaa at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 4

ok i got it.

ActionListener radio1Listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

int i=0;

String text1="";

while(text1!=null) {

if(radioButton[i].isSelected()) {

text1 = radioButton[i].getText();

text.setText("you selected " + text1);

}

i++;

}

};

};

button.addActionListener(radio1Listener);

is this good?. i mean this is more complex lol but it follows the steps of dividing your task into arrays and etc.. any more ideas..

Thanks

lrngjavaa at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 5

text is always != null in your code...

ActionListener radio1Listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

for(int i = 0; i < radioButton.length; i++) {

if(radioButton[i].isSelected()) {

text.setText("You selected " + radioButton[i]);

}

}

};

};

bsampieria at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 6
that's not good either... your last one will be an infinite loop, cuz you never break out of the while loop. There's only so much you can do. UI code tends to be larger than many other types of app code due to all the configurations and settings in various components.
bsampieria at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 7

your suggestion works but output is different

this is what i get if i use your command in my actionPerformed method

if i select radio1 this is the output i get in my text. i believe i had to set it != null

You selected javax.swing.JRadioButton[,5,45,382x24,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1acd47,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=radio2]

lrngjavaa at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 8
Basically, if you are writing code and see the same basic thing done more than 2 or 3 times, then you want to generalize it and break it out to a separate method.
bsampieria at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 9
right.. sorry, forgot the .getText() call... That last wasn't an infinite loop, I misread the variables. But with the for loop, you don't need the extra variable, so a little clean, IMO.
bsampieria at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 10

ok this works now.

ActionListener radio1Listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

String text1 = "";

for(int i=0; i<radioButton.length; i++) {

if(radioButton[i].isSelected()) {

text1 = radioButton[i].getText();

text.setText("You selected " + text1);

}

}

};

};

button.addActionListener(radio1Listener);

>

lrngjavaa at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 11

Thanks bsampieri. Your for loop is great wonder how can i miss it :). the only difference between yours and mine is your method outputs a whole bunch of stuff as i posted earlier with my latest method the output is perfect no problem. if i dont have

String text1 = "";

and if i directly do this.

text.setText("You selected " + radioButton)

outputs fine but runtime gives me lots of eventDispatcher exception and etc...

Overall thanks for the for loop input.

Just wish i have money to buy jbuilder :)

lrngjavaa at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 12

one more adjustment...

ctionListener radio1Listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

for(int i=0; i<radioButton.length; i++) {

if(radioButton[i].isSelected()) {

text.setText("You selected " + radioButton[i].getText());

break; // done, so stop now

}

}

};

};

Forget JBuilder. Eclipse is free.>

bsampieria at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 13

Thanks alot bro. It works like a charm now. your code + mine code. Since you are a senior member so had to adapt your option first.

Anyways eclipse is not worth if you are using the visual class to build swing components. like for example i have a JPanel. in eclipse i had to define the layout manually rather than having an option of displaying the layout in the properties window.

Secondly if i need an actionPerformed method for a button eclipse will not generate the actionEvent method so i had to create one by myself. when i say actionEVent method that means every button should have an actionMethod that is why buttons are there for you and this is where eclipse sucks big time.

ANyways thanks for the input and thanks for the suggestions. looks like i will stick to hand programming :) you learn more rather than depending on the IDE.

lrngjavaa at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 14
You learn a whole heck of a lot more without the IDE. I do UI development primarily, and never touch those graphical editors. I've been using Eclipse for 2+ years now. Was using TextPad and Ant or plain javac before that.
bsampieria at 2007-7-12 9:54:26 > top of Java-index,Java Essentials,New To Java...
# 15

> You learn a whole heck of a lot more without the IDE.

> I do UI development primarily, and never touch those

> graphical editors. I've been using Eclipse for 2+

> years now. Was using TextPad and Ant or plain javac

> before that.

Exactly sounds good to me. I am not using any IDE to program in java just using a lower level JCreator V EE (Which is for free) and does not offer alot. its the same thing like you are using command prompt or text editor.

lrngjavaa at 2007-7-21 21:12:29 > top of Java-index,Java Essentials,New To Java...