ItemListener
I am having trouble with adding an item listener to my buttons on a GUI I have made. I have tried the code
searchButton.addItemListener(this);
clearFormButton.addItemListener(this);
and I get the following error:
jobFinderGui.java:6: jobFinderGui should be declared abstract; it does not define actionPerformed(java.awt.event.ActionEvent) in jobFinderGui
public class jobFinderGui extends CloseableFrame implements ActionListener
The following is my code listing:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class jobFinderGui extends CloseableFrame implements ActionListener
JLabel jobTitleLabel;
JTextArea jobTitleTextArea;
JScrollPane sp_jobTitleTextArea;
JLabel salaryLabel;
Choice salaryChoice;
JLabel phrasesLabel;
JTextArea phrasesTextArea;
JScrollPane sp_phrasesTextArea;
JButton searchButton;
JButton clearFormButton;
JLabel resultsLabel;
JTextArea resultsTextArea;
JScrollPane sp_resultsTextArea;
private int totalJobs = 0;
public jobFinderGui()
jobFinderLayout customLayout = new jobFinderLayout();
getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
getContentPane().setLayout(customLayout);
jobTitleLabel = new JLabel("Enter the Title of the job(s) you are searching for:");
getContentPane().add(jobTitleLabel);
jobTitleTextArea = new JTextArea("");
sp_jobTitleTextArea = new JScrollPane(jobTitleTextArea);
getContentPane().add(sp_jobTitleTextArea);
salaryLabel = new JLabel("Choose desired salary:");
getContentPane().add(salaryLabel);
salaryChoice = new Choice();
salaryChoice.addItem("0 - 19999");
salaryChoice.addItem("20000 - 39999");
salaryChoice.addItem("40000 - 59999");
salaryChoice.addItem("60000 - 79999");
salaryChoice.addItem("80000 - 99999");
salaryChoice.addItem("100000+");
getContentPane().add(salaryChoice);
phrasesLabel = new JLabel("Enter key phrase(s):");
getContentPane().add(phrasesLabel);
phrasesTextArea = new JTextArea("");
sp_phrasesTextArea = new JScrollPane(phrasesTextArea);
getContentPane().add(sp_phrasesTextArea);
searchButton = new JButton("Search");
getContentPane().add(searchButton);
searchButton.addActionListener(this);
clearFormButton = new JButton("Clear");
getContentPane().add(clearFormButton);
resultsLabel = new JLabel("Please find below the results from your search:");
getContentPane().add(resultsLabel);
resultsTextArea = new JTextArea("");
sp_resultsTextArea = new JScrollPane(resultsTextArea);
resultsTextArea.setEditable(false);
getContentPane().add(sp_resultsTextArea);
setSize(getPreferredSize());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
public static void main(String args[]) {
jobFinderGui window = new jobFinderGui();
window.setTitle("jobFinder");
window.pack();
window.show();
class jobFinderLayout implements LayoutManager {
public jobFinderLayout() {
public void addLayoutComponent(String name, Component comp) {
public void removeLayoutComponent(Component comp) {
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 725 + insets.left + insets.right;
dim.height = 406 + insets.top + insets.bottom;
return dim;
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,312,24);}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+336,insets.top+8,368,72);}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+96,288,24);}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+336,insets.top+96,136,24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,288,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+336,insets.top+136,368,72);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+216,insets.top+224,96,24);}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+336,insets.top+224,96,24);}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+184,insets.top+264,336,24);}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+24,insets.top+296,688,104);}
Any help in sorting out this would be greatly appreciated.
you are getting the error cause you are implementing the ActionListener
and you dont have the actioPerformed() {} method in your class I am not sure what you are trying to do cause I have not took the time to look over all your code but you might be wanting to implement itemListener and use the itemStateChanged(){} method
just a thought as i was passing by
Similar Messages
-
Check Box/ItemListener question
Hello all,
I'm trying to create a GUI for a program that I've been working on for some time. In the program, various sections can be run depending on what variables are present in the input file AND whether or not certain previous sections have been selected to run. I'm trying to represent this with a menu comprised of check boxes, where if the proper sections have been selected other boxes become enabled accordingly. I tried to get this behavior simply by saying in itemStateChanged() that if stp1bt3 is selected and stp1bt1 has just been selected, to enable stp2bt1. For some reason, stp2bt1 isn't enabled whenever I try this. Any help would be greatly appreciated, as I'm sure it's something very small that I have to add to this. Here is the code I have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckGUI extends JPanel implements ItemListener {
private JCheckBox stp1bt1;
private JCheckBox stp1bt2;
private JCheckBox stp1bt3;
private JCheckBox stp2bt1;
private JCheckBox stp2bt2;
private JCheckBox stp2bt3;
private JCheckBox stp3bt1;
private JCheckBox stp3bt2;
private JCheckBox stp3bt3;
private JCheckBox stp3bt4;
private JCheckBox stp3bt5;
private JLabel emptyLabel;
private JLabel step1Label;
private JLabel step2Label;
private JLabel step3Label;
private boolean s1b2, s1b3, s2b1, s2b2, s2b3, s3b1, s3b2, s3b3, s3b4, s3b5;
public CheckGUI(boolean s12, boolean s13, boolean s21,
boolean s22, boolean s23, boolean s31,
boolean s32, boolean s33, boolean s34,
boolean s35){
s1b2 = s12;
s1b3 = s13;
s2b1 = s21;
s2b2 = s22;
s2b3 = s23;
s3b1 = s31;
s3b2 = s32;
s3b3 = s33;
s3b4 = s34;
s3b5 = s35;
emptyLabel = new JLabel("");
stp1bt1 = new JCheckBox("Button 1");
stp1bt2 = new JCheckBox("Button 2");
if (s1b2 == false){
stp1bt2.setEnabled(false);
stp1bt3 = new JCheckBox("Button 3");
if (s1b3 == false){
stp1bt3.setEnabled(false);
stp2bt1 = new JCheckBox("Button 1");
stp2bt1.setEnabled(false);
stp2bt2 = new JCheckBox("Button 2");
stp2bt2.setEnabled(false);
stp2bt3 = new JCheckBox("Button 3");
stp2bt3.setEnabled(false);
stp3bt1 = new JCheckBox("Button 1");
stp3bt1.setEnabled(false);
stp3bt2 = new JCheckBox("Button 2");
stp3bt2.setEnabled(false);
stp3bt3 = new JCheckBox("Button 3");
stp3bt3.setEnabled(false);
stp3bt4 = new JCheckBox("Button 4");
stp3bt4.setEnabled(false);
stp3bt5 = new JCheckBox("Button 5");
stp3bt5.setEnabled(false);
step1Label = new JLabel("Step 1");
step2Label = new JLabel("Step 2");
step3Label = new JLabel("Step 3");
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(step1Label);
add(stp1bt1);
if(s1b2 == true)
add(stp1bt2);
else
add(emptyLabel);
if(s1b3 == true)
add(stp1bt3);
else
add(emptyLabel);
add(step2Label);
if(s2b1 == true)
add(stp2bt1);
else
add(emptyLabel);
if(s2b2 == true)
add(stp2bt2);
else
add(emptyLabel);
if(s2b3 == true)
add(stp2bt3);
else
add(emptyLabel);
add(step3Label);
if(s3b1 == true)
add(stp3bt1);
else
add(emptyLabel);
if(s3b2 == true)
add(stp3bt2);
else
add(emptyLabel);
if(s3b3 ==true)
add(stp3bt3);
else
add(emptyLabel);
if(s3b4 == true)
add(stp3bt4);
else
add(emptyLabel);
if(s3b5 == true)
add(stp3bt5);
else
add(emptyLabel);
public void itemStateChanged(ItemEvent e){
Object source = e.getItemSelectable();
if (source == stp1bt1){
if(e.getStateChange() == ItemEvent.SELECTED && stp1bt3.isSelected() && s2b1 == true){
stp2bt1.setEnabled(true);
public static void main (String args[]) {
JFrame f = new JFrame ("CheckBox Test");
JPanel j = new CheckGUI(true,true,true,true,true,true,true,true,true,true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
f.getContentPane().add (j, BorderLayout.CENTER);
f.setSize (150, 450);
f.show();
}Hello,
My dear frind you have to add the item listener fro an item to do dymaic changes in the check box.
So for now i have added it.. check it down
i have also placed the system.out.println(....);
Alll THe Best
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckGUI extends JPanel implements ItemListener {
private JCheckBox stp1bt1;
private JCheckBox stp1bt2;
private JCheckBox stp1bt3;
private JCheckBox stp2bt1;
private JCheckBox stp2bt2;
private JCheckBox stp2bt3;
private JCheckBox stp3bt1;
private JCheckBox stp3bt2;
private JCheckBox stp3bt3;
private JCheckBox stp3bt4;
private JCheckBox stp3bt5;
private JLabel emptyLabel;
private JLabel step1Label;
private JLabel step2Label;
private JLabel step3Label;
private boolean s1b2, s1b3, s2b1, s2b2, s2b3, s3b1, s3b2, s3b3, s3b4, s3b5;
public CheckGUI(boolean s12, boolean s13, boolean s21,
boolean s22, boolean s23, boolean s31,
boolean s32, boolean s33, boolean s34,
boolean s35){
s1b2 = s12;
s1b3 = s13;
s2b1 = s21;
s2b2 = s22;
s2b3 = s23;
s3b1 = s31;
s3b2 = s32;
s3b3 = s33;
s3b4 = s34;
s3b5 = s35;
emptyLabel = new JLabel("");
stp1bt1 = new JCheckBox("Button 1");
stp1bt2 = new JCheckBox("Button 2");
if (s1b2 == false){
stp1bt2.setEnabled(false);
stp1bt3 = new JCheckBox("Button 3");
if (s1b3 == false){
stp1bt3.setEnabled(false);
stp2bt1 = new JCheckBox("Button 1");
stp2bt1.setEnabled(false);
stp2bt2 = new JCheckBox("Button 2");
stp2bt2.setEnabled(false);
stp2bt3 = new JCheckBox("Button 3");
stp2bt3.setEnabled(false);
stp3bt1 = new JCheckBox("Button 1");
stp3bt1.setEnabled(false);
stp3bt2 = new JCheckBox("Button 2");
stp3bt2.setEnabled(false);
stp3bt3 = new JCheckBox("Button 3");
stp3bt3.setEnabled(false);
stp3bt4 = new JCheckBox("Button 4");
stp3bt4.setEnabled(false);
stp3bt5 = new JCheckBox("Button 5");
stp3bt5.setEnabled(false);
step1Label = new JLabel("Step 1");
step2Label = new JLabel("Step 2");
step3Label = new JLabel("Step 3");
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(step1Label);
add(stp1bt1);
if(s1b2 == true)
add(stp1bt2);
else
add(emptyLabel);
if(s1b3 == true)
add(stp1bt3);
else
add(emptyLabel);
add(step2Label);
if(s2b1 == true)
add(stp2bt1);
else
add(emptyLabel);
if(s2b2 == true)
add(stp2bt2);
else
add(emptyLabel);
if(s2b3 == true)
add(stp2bt3);
else
add(emptyLabel);
add(step3Label);
if(s3b1 == true)
add(stp3bt1);
else
add(emptyLabel);
if(s3b2 == true)
add(stp3bt2);
else
add(emptyLabel);
if(s3b3 ==true)
add(stp3bt3);
else
add(emptyLabel);
if(s3b4 == true)
add(stp3bt4);
else
add(emptyLabel);
if(s3b5 == true)
add(stp3bt5);
else
add(emptyLabel);
stp1bt1.addItemListener(this);
stp1bt2.addItemListener(this);
stp1bt3.addItemListener(this);
stp2bt1.addItemListener(this);
stp2bt2.addItemListener(this);
stp2bt3.addItemListener(this);
stp3bt1.addItemListener(this);
stp3bt2.addItemListener(this);
stp3bt3.addItemListener(this);
stp3bt4.addItemListener(this);
stp3bt5.addItemListener(this);
public void itemStateChanged(ItemEvent e){
Object source = e.getSource();
if (source == stp1bt1){
/* System.out.println(e.getStateChange() == ItemEvent.SELECTED);
System.out.println(stp1bt3.isSelected());
System.out.println(s2b1 == true); */
if(e.getStateChange() == ItemEvent.SELECTED && stp1bt3.isSelected() && s2b1 == true){
stp2bt1.setEnabled(true);
public static void main (String args[]) {
JFrame f = new JFrame ("CheckBox Test");
JPanel j = new CheckGUI(true,true,true,true,true,true,true,true,true,true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
f.getContentPane().add (j, BorderLayout.CENTER);
f.setSize (150, 450);
f.show();
} -
How to veto a JComboBox selection change in ItemListener
I have a JComboBox whose current value is tied to some data cached in my application. I want to be able to examine the selection in an ItemListener attached to the JComboBox, and if the value is different than what I have cached, I want to ask the user if they really want to change because the change will render some other data as no longer valid. If the user wants to abandon the change, then I want to "abandon" or "veto" the selection change and reset it to the old value.
For instance, if the combo box has values "Foo" and "Bar" in it, and "Foo" is the cached selection, then the user goes and changes the selection to "Bar", I want to prompt them with something like "You have changed the value to Bar. Doing so will render XXX no longer valid. Do you want to continue?" -- if they cancel the change, then leave the selection as "Foo".
What is the best way to do this within ItemListener's itemStateChanged? Are there better ways to do this besides what I've outlined? I didn't see anything in "Core JFC" book or the JDK documentation that discussed this sort of thing.
Thanks in advance,
EricHi,
It is a design problem and not realy a language problem... The way I create vetoing design it that a keep the previous selection of the JComboBox... upon action I look if the application can do the new computation...if not...ask the user... if true| run calculations if not set the JComboBox to the previous selection...
JRG -
hi i am constructing an interface and i need to use the ItemListener for my radio buttons. when i add this to the code and do a test compile it gives me the following error:
F:\My Documents\Work\Uni\CNE1\Java\Semester 2\Remidial Assignment\Buttons>javac
ass.java
ass.java:13: cannot resolve symbol
symbol : class ItemListener
location: class ass
public class ass extends JApplet implements ChangeListener, ItemListener {
^
1 error
Thanks for any help
here is the source code:
/* CNE Level 1 */
/* Remedial Assignment */
/* Date: 4/08/03 */
/* Author: Patrick Louis-Jean */
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class ass extends JApplet implements ChangeListener, ItemListener {
JRadioButton Circle, Rect, Triangle;
JLabel Radius, Width, Height, Base;
JSlider RADIUS, WIDTH, HEIGHT;
JButton button;
Container contentArea;
public void init(){
contentArea = this.getContentPane();
FlowLayout layout=new FlowLayout();
contentArea.setLayout(layout);
Circle = new JRadioButton("Circle");
Rect = new JRadioButton("Rectangle ");
Triangle = new JRadioButton("Triangle");
Radius = new JLabel("Radius");
Width = new JLabel("Width");
Height = new JLabel("Height");
Base = new JLabel("Base");
RADIUS = new JSlider(0,100);
RADIUS.setMajorTickSpacing(10);
RADIUS.setMinorTickSpacing(5);
RADIUS.setPaintTicks(true);
RADIUS.setPaintLabels(true);
RADIUS.addChangeListener(this);
WIDTH = new JSlider(0,100);
WIDTH.setMajorTickSpacing(10);
WIDTH.setMinorTickSpacing(5);
WIDTH.setPaintTicks(true);
WIDTH.setPaintLabels(true);
WIDTH.addChangeListener(this);
HEIGHT = new JSlider(0,100);
HEIGHT.setMajorTickSpacing(10);
HEIGHT.setMinorTickSpacing(5);
HEIGHT.setPaintTicks(true);
HEIGHT.setPaintLabels(true);
HEIGHT.addChangeListener(this);
contentArea.add(Circle);
contentArea.add(Rect);
contentArea.add(Triangle);
contentArea.add(Radius);
contentArea.add(RADIUS);
contentArea.add(Width);
contentArea.add(WIDTH);
contentArea.add(Height);
contentArea.add(HEIGHT);
//contentArea.add(HEIGHT);
setContentPane(contentArea);
public void stateChanged(ChangeEvent event){
if(event.getSource()==RADIUS){
Radius.setText("Radius: "+RADIUS.getValue());
if(event.getSource()==WIDTH){
Width.setText("Width: "+WIDTH.getValue());
if(event.getSource()==HEIGHT){
Height.setText("Height: "+HEIGHT.getValue());Just worked that out after an hour of stressing. i needed java.awt.event.*
Thanks anyway good to know that there are people who reply so fast. this forum is great. -
How to use ActionListener and ItemListener in the same class
Why can't I use both ActionListener and ItemListener in the same Class?
I want to use a drop down list and an entry box on the same form.
How? I'm having trouble compling......
Thanks!
CODE:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Disp extends Applet implements ActionListener, ItemListener
{ TextField entry=new TextField(7);
Button Enter=new Button("Enter");
public void init()
add(entry);
add(Enter);
entry.addActionListener(this);
entry.requestFocus();
Enter.addActionListener(this);
public void actionPerformed(ActionEvent thisEvent)
String answer=entry.getText();Simply get your Applet to implement both ActionListener and ItemListener.
public class MyApplet extends Applet implements ActionListener, ItemListener
public void init()
itemWidget.addItemListener(this):
actionWidget.addActionListener(this);
public void actionPerformed(ActionEvent e)
System.out.println("ACTION PERFORMED!");
public void itemStateChanged(ItemEvent e)
System.out.println("ITEM STATE CHANGED!");
}Hope this helps. -
How do you specifically let the Itemlistener know which Item was selected..
How do you specifically let the Itemlistener know which Item was selected in a JComboBox? Anyone has any idea? What should the coding look like?
getSelectedItem()
-
How do you specifically let an Itemlistener know which Item was selected?
How do you specifically let the Itemlistener know which Item was selected in a JComboBox? Anyone has any idea? What should the coding look like?
Hi,
Try this method
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox petList = new JComboBox(petStrings);
if ( petStrings[0]==(String)petList.getSelectedItem() )
System.out.println("I'm a bird");
}else{ -
I need advise on JComboBox / ItemListener
I have 3 JComboBoxes which act as date fields (month, day and year). I want to monitor the boxes so the user does not enter a date which is below a certain minimum, or over a certain maximum.
The ItemListener is causing me some confusion though, whenever I adjust a JComboBox, two ItemListenerEvents are triggered back to back, and I'm guessing the first is a DESELECT event and the second is a SELECT event.
The thing is, the JComboBox will return the same index from getSelectedIndex() during each of these events, so I can't tell what was the old selected index was. I figured the DESELECT event would not change the selectedIndex on my combobox, but it looks like it did. Any better way to find out the old selected index so I can revert to it if I find out the new value is no good?Read this section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/events/itemlistener.html]How to Write an Item Listener
-
How to handle ItemListener in Dynamic JCheckBoxs
In my code i create dynamic JCheckBoxes and add ItemListener to every JCheckBox. I want to triger different actions by selecting JCheckBoxes.
here is my code....
String names[] ={"AAAA","BBBB","CCCC","DDDD"};
JCheckBox cb[]=new JCheckBox[names.length];
for( int j=0;j<names.length;j++) {
cb[j] = new JCheckBox(names[j]);
cb[j].addItemListener(this);
this.add(cb[j]);
in my item stateChanged method,...
public void itemStateChanged(ItemEvent e) {
Object x=e.getItemSelectable();
if (x == cb1) {
System.out.println("AAA is selected");
if (x == cb2) {
System.out.println("BBB is selected");
....like wise.
but here (x==cb1) gives an error
Is there any way to specify the exact JCheckBox which was selected?
pls helpyou only said you get an error, but how can we help if you did not show the error you are getting? Please make sure you post your error or your code even; so that we can figure out what the problem is.
-
Code initialization Sequence:
1. Init components creates an empty JComboBox and adds an ItemListener.
2. JComboBox gets loaded with a ComboBoxModel which contains the display data and Primary Keys.
When I run the application, and click on an item in the ComboBox, no item event gets fired.
I tried fixing this by changing the sequence to:
1. Create empty JComboBox.
2. Create ComboBoxModel (Keys + values)
3. Set JComboBox model.
4. Add ItemListener to JComboBox.
Problem persists. No event is being fired!
What am I doing wrong?
Thanks
EnricoI managed to fix this problem.
Our ComboBoxViewModel (which extends ComboBoxModel), didn't implement addListDataListener and removeListDataListener. So the JComboBox couldn't add itself to the model as a listener, and therefore wasn't picking up ListDataEvents.
Also...in the setSelectedItem method in the model, you need to check whether the old index != new index, then
create a ListDataEvent evt, and notify listeners via listdatalister.contentChanged(evt); -
How can add a ItemListener to a List
Hello mates,
I m trying to add a ItemListener to a List by :
list.addItemListener((ItemListener) this);
/*other actionListeners*/
list.addActionListener(this);
add.addActionListener(this);
update.addActionListener(this);
delete.addActionListener(this);but got ClassCastException. I extends JFrame in this class. What should I do to solve this problem?
Cheers,
EltonIn JList such method addItemListener doesnt exist. Try using
list.addListSelectionListener(
new ListSelectionListener()
public void valueChanged(ListSelectionEvent event)
// TODO
);or if you prefer list.addListSelectionListener(this) your class must implement ListSelectionListener interface.
Hope i've been of some assistance! -
ActionListeners and ItemListener
Hi
I'm using some ItemListener to detect when things are selected from a list but require a button to open a new GUI window. However, when i try and compile, the compiler says i can't use ActionListeners and ItemListeners in the same class. Is this true? Any ways i can get around it?
Cheersimplements ItemListener and ActionListener
gives the following error messages:
ListTest.java:6: '{' expected
public class ListTest extends GUIFrame implements ItemListener and ActionListener
^
ListTest.java:100: '}' expected
^
ListTest.java:6: ListTest should be declared abstract; it does not define itemStateChanged(java.awt.event.ItemEvent) in ListTest
public class ListTest extends GUIFrame implements ItemListener and ActionListener
anymore ideas? -
Hi guys,
having problems with ItemListener, seems the compiler doesn't recognize the variable. Here's the code ...
public void itemStateChanged(ItemEvent ie)
String arg = e.getActionCommand();
//have also tried ie.getActionCommand();
if(arg == "Red")
setBackground(Color.red);
if(arg == "Yellow")
setBackground(Color.yellow);
if(arg == "Cyan")
setBackground(Color.cyan);
if(arg == "Magenta")
setBackground(Color.magenta);
}The error message I get is this ...
Buttons.java:90: cannot find symbol
symbol : variable e
location: class Buttons
String arg = e.getActionCommand();
^
or this one ...
Buttons.java:83: cannot find symbol
symbol : method getActionCommand()
location: class java.awt.event.ItemEvent
String arg = ie.getActionCommand();
I guess I'm a little confused as how to use these methods, Any help would be greatly appreciated.OK let me start over ...
I have a textbook assignment, it requires me to add to provided code to produce a frame with five buttons that change the background color of the frame ... I have that working correctly ... here is the code...
Chapter 6: Buttons
Programmer: Doc Thompson
Date: 12 Jan 07
Filename: Buttons1.java
Purpose: Enhancing this frame
import java.awt.*;
import java.awt.event.*;
public class Buttons extends Frame implements ActionListener
public Buttons()
//set the layout
setLayout(new BorderLayout(20,10));
//Add buttons
Button red = new Button("Red");
red.addActionListener(this);
Button yellow = new Button("Yellow");
yellow.addActionListener(this);
Button cyan = new Button("Cyan");
cyan.addActionListener(this);
Button magenta = new Button("Magenta");
magenta.addActionListener(this);
Button white = new Button("White");
white.addActionListener(this);
add(red, BorderLayout.NORTH);
add(yellow, BorderLayout.SOUTH);
add(cyan, BorderLayout.EAST);
add(magenta, BorderLayout.WEST);
add(white, BorderLayout.CENTER);
//override the windowClosing event
addWindowListener(
new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
public static void main(String[] args)
// set frame properties
Buttons f = new Buttons();
f.setTitle("Border Application");
f.setBounds(200,200,300,300);
f.setVisible(true);
f.setBackground (Color.red);
public void actionPerformed(ActionEvent e)
String arg = e.getActionCommand();
if(arg == "Red")
setBackground(Color.red);
if(arg == "Yellow")
setBackground(Color.yellow);
if(arg == "Cyan")
setBackground(Color.cyan);
if(arg == "Magenta")
setBackground(Color.magenta);
if(arg == "White")
setBackground(Color.white);
} ...now I need to remove the center button and replace it with a choice component named colors, add an itemlistener to the choice component, add the choice comp to the center area of the BorderLayout, then write the ItemStateChanged() method to test for button clicks(it states to use the same code as in the actionPerformed() method.
So that is what I am trying to accomplish but I get the previously described errors. I thought that I was declaring the "ie" variable and assigning it to the ItemStateChanged() method, but it isn't recognizing the method . That is why I am at a loss. here is the code after I updated it ....
import java.awt.*;
import java.awt.event.*;
public class Buttons extends Frame implements ActionListener, ItemListener
public Buttons()
//set the layout
setLayout(new BorderLayout(20,10));
//Add buttons
Button red = new Button("Red");
red.addActionListener(this);
Button yellow = new Button("Yellow");
yellow.addActionListener(this);
Button cyan = new Button("Cyan");
cyan.addActionListener(this);
Button magenta = new Button("Magenta");
magenta.addActionListener(this);
Choice colors = new Choice();
colors.add("Red");
colors.add("Yellow");
colors.add("Cyan");
colors.add("Magenta");
colors.addItemListener(this);
add(red, BorderLayout.NORTH);
add(yellow, BorderLayout.SOUTH);
add(cyan, BorderLayout.EAST);
add(magenta, BorderLayout.WEST);
add(colors,BorderLayout.CENTER);
//override the windowClosing event
addWindowListener(
new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
public static void main(String[] args)
// set frame properties
Buttons f = new Buttons();
f.setTitle("Border Application");
f.setBounds(200,200,300,300);
f.setVisible(true);
f.setBackground (Color.red);
public void actionPerformed(ActionEvent e)
String arg = e.getActionCommand();
if(arg == "Red")
setBackground(Color.red);
if(arg == "Yellow")
setBackground(Color.yellow);
if(arg == "Cyan")
setBackground(Color.cyan);
if(arg == "Magenta")
setBackground(Color.magenta);
public void itemStateChanged(ItemEvent ie)
String arg = ie.getActionCommand();
if(arg == "Red")
setBackground(Color.red);
if(arg == "Yellow")
setBackground(Color.yellow);
if(arg == "Cyan")
setBackground(Color.cyan);
if(arg == "Magenta")
setBackground(Color.magenta);
}I don't understand what I am doing incorrectly, and any assistance would be greatly appreciated. (Thanks BZ for your previous suggestions) -
Using an ItemListener on a JMenu
I have the following:
JMenu menu = new JMenu( "File" );
menu.addItemListener( new ItemListener()
public void itemStateChanged( ItemEvent e )
JMenu m = (JMenu)e.getItem();
if ( e.getStateChanged() == ItemEvent.SELECTED )
if ( !valid() ) // which checks text field for correct entry
m.setPopupMenuVisible( false );
} );I've checked the popup menu and it is the correct one. The popup menu stays visible. How do I remove it?
I checked the isPopupMenuVisible() and it always says false.I browsed through that link though I have a few problems. First, my old computer only had itunes 6 (I believe) while my new computer now has itunes 7. Secondly, I can't set my ipod to disk due to the fact that my old computer is dead and there is no way to boot it. I can however get access to the HD if that would assist in some way by letting me copy all those files over. Lastly and this is rather silly of me my music is not all in one place. I know where most of it is in two directories but will anything be messed up if not all the music is present when I try and copy things over?
-
could anyone post an example showing how to use a list with an itemListener?
Here's a tutorial on "How to Write an ItemListener":
http://java.sun.com/docs/books/tutorial/uiswing/events/itemlistener.html
Maybe you are looking for
-
Outlook 2011 for Mac: Occurrence with this index was previously deleted from the recurrence
Client using Outlook 2011 (14.2.4) with Mac OS 10.8.2. I've never seen this error before, and it keeps popping up (next to the classic yellow triangle with exclamation point -- I'd post screenshot, but forum isn't letting me.) The message doesn't se
-
On what computer configuration is Ultiboard running at its best?
Hi, I will soon upgrade to the latest Multisim Ultiboar Power pro version 10.1 And I wondered if I'd better upgrade my PC as well, it is 4 years old now, and the current Ultiboar version (10.0, full edition) blocks from time to time, even crashes a f
-
Help! FCS3-How do I reduce bitmap size if drop down edit item is grayed out?
I am in Flash CS3. I need to reduce a bunch (300) Bitmaps pixels dimensions from 1068 px to 270 px across (and proportional height) in library but the "edit with..." (external editor) drop down item is grayed out. I have tried everything, including
-
JVM + Linux kernel, can it have faster program execution
Hello, I am a Garo Garabedyan from Sofia, Bulgaria. I study IT and I am a fan of Java programming language. I heard that Java is going Open Source, I want to say you that in my opinion this is a very good move. I am writing to you about one thought o
-
Ability to Change Data Manager Messages
Hi Experts, I would like to know if anyone has successfully changed the Data Manager (DM) messages. We have a DM package that runs a BAdI calculation. We successfully linked the BAdI message to the DM so that on the detailed section of the DM resu