Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ItemListener().....need help? 1

Status
Not open for further replies.

qb828

Programmer
Sep 27, 2000
98
US
hello!
can't find what is wrong wiht my code for eventhandling.
this is my check button event handling.
i am using swing. it won't compile.

here is my code. can yo take a look at it , please..

JRadioButton radiob1=new JRadioButton("Computer", true);
addComponent(radiob1,1,3,3,1);
JRadioButton radiob2=new JRadioButton("Player2", false);
addComponent(radiob2,5,3,3,1);
//create relationship of group
ButtonGroup radiogroup= new ButtonGroup();

radiogroup.add(radiob1);
radiob1.addItemListener(this);
radiogroup.add(radiob2);


i did this on top my class implements ItemListener

public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == radiob1) <==error here
System.out.println(&quot;playing against computer&quot;);
else
System.out.println(&quot;Playing against player2&quot;);
}

thanks very much

Q
 
As far as i can see the error that you get is the undefined variable on radiob1. This will be solved if you declare the buttons global in your code.

// global declaration of the buttons and other stuff
private JRadioButton radiob1;
private JRadioButton radiob2;
private ButtonGroup radiogroup;

//your function where you initiate everything
{
radiob1 =new JRadioButton(&quot;Computer&quot;, true);
addComponent(radiob1,1,3,3,1);
radiob2=new JRadioButton(&quot;Player2&quot;, false);
addComponent(radiob2,5,3,3,1);
//create relationship of group
radiogroup= new ButtonGroup();

radiogroup.add(radiob1);
radiob1.addItemListener(this);
radiogroup.add(radiob2);
}

public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == radiob1) // error gone, now it knows radiob1
System.out.println(&quot;playing against computer&quot;);
else
System.out.println(&quot;Playing against player2&quot;);
}

Hope this helps.
Greetings,
Steven.
 
thanks steven..
i don't know why i didn't think about that!!
have Question about index array.
length gives integer of how many array there are.
i want to acess what a current array index is.

say int array[]=new int[3];
int c=array.length; //gives c=3, array size
array[2]; i want to acess index 2

can you do that?

Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top