Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ItemListener  RSS feed

 
Fran Costigan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have set up an actionlistener as an inner class in my application to deal with my JButtons, butn i now want to add radio buttons and implement itemlistener.

i was going to do the following:

public class Cineplex extends JFrame implements itemListener

but when i run it it throws up an error.

do i need to create an inner class for itemlistener as i did actionlistener, and if so how do i do this??

this is how i implemented sctionlistener:

//set up the ActionListener
ButtonListener myListener = new ButtonListener();

 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code snippet would be more helpful. I think i can help you if you can give me that
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the first letter (should be capitalized) in ItemListener in your code. This is all you need for your class to implement ItemListener:
 
Fran Costigan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's some of the code i'm working with:

public class Cineplex extends JFrame implements itemListener
{
JRadioButton movie1Btn, movie2Btn, movie3Btn, movie4Btn;
ButtonGroup movieGroup;
}

public Cineplex()
{
movie1Btn = new JRadioButton("Brokeback Mountain");
movie1Btn.addItemListener(this);
c.add(movie1Btn);
etc.....

movieGroup = new ButtonGroup();

movieGroup.add(movie1Btn);
etc.....
}

for my JButtons I have set up my actionListener aas follows:

ButtonListener myListener = new ButtonListener();
 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig is right when he said that the 'i' in itemListener should be capitalized. Plus you need to appropriately implement the method
itemStateChanged(ItemEvent e)

And it should work fine. Do let me know if it still does not work
 
Sergey Skudaev
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

this is an example code how to use itemlistener. I hope it helps

import java.awt.*;
import java.awt.event.*;

public class ColorButtons extends Frame implements ItemListener
{
Checkbox blue, red, yellow, pink, gray;

public ColorButtons()
{
CheckboxGroup group = new CheckboxGroup();
blue = new Checkbox("blue", false, group);
red = new Checkbox("red", false, group);
yellow = new Checkbox("yellow", false, group);
pink = new Checkbox("pink", false, group);
gray = new Checkbox("gray", false, group);
blue.addItemListener(this);
red.addItemListener(this);
yellow.addItemListener(this);
pink.addItemListener(this);
gray.addItemListener(this);
setLayout(new FlowLayout());
add(blue);
add(red);
add(yellow);
add(pink);
add(gray);
setSize(300,175);
}




public void itemStateChanged(ItemEvent e)
{
Color color = null;
Checkbox checkbox=null;


if(e.getStateChange() == ItemEvent.SELECTED)
{
checkbox = (Checkbox)e.getSource();

if(blue.getState())
color = Color.blue;
else if(red.getState())
color = Color.red;
else if(yellow.getState())
color = Color.yellow;
else if(pink.getState())
color = Color.pink;
else if(gray.getState())
color = Color.gray;
}
else
{
System.out.println("unexpected checkbox sending events: " +
checkbox.getLabel());
color = color.black;
}

setBackground(color);
repaint();
}



public static void main(String[] args)
{
ColorButtons cb = new ColorButtons();

//Finish setting up the frame, and show it.
cb.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// cb.pack();
cb.setVisible(true);



}
}


// visit www.configure-all.com compuetr programming tutorials including java
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!