简体   繁体   中英

Java swing combobox actionListner

I'm writing a program using basic swing components and actionlisteners. I've got it mostly working but I can't seem to get my combobox action listners to work, what am I doing wrong?

As far as I understand, the string should be being passed to the actionlistner method, but it's not working at runtime!

public class LightControl extends JFrame implements ActionListener
{

private JButton on, off, twentyWatt, fortyWatt, sixtyWatt;
private JComboBox lightTimer;
private String [] comboSelection = new String[]{"Morning","Evening","All day"};
private JTextField statusText;
private LightBulb lightbulb;
private JPanel frameContainer;
private JPanel wattFrame;
private JPanel toggleFrame;
private JPanel comboFrame;

public LightControl()
{
    super("Lightbulb");
    lightbulb=new LightBulb();

    Container container = getContentPane();
    //FlowLayout layout=new FlowLayout();

    //instantiate
    statusText=new JTextField("Select an option");
    statusText.setSize(100, 50);
    statusText.setEditable(false);
    lightTimer = new JComboBox(comboSelection);
    on = new JButton("On");
    off = new JButton("Off");
    twentyWatt=new JButton("20W");
    fortyWatt=new JButton("40W");
    sixtyWatt=new JButton("60W");


    //right hand side frames
    comboFrame=new JPanel();
    comboFrame.add(lightTimer);

    toggleFrame=new JPanel();
    toggleFrame.setLayout(new GridLayout(1, 2));
    toggleFrame.add(on);
    toggleFrame.add(off);

    wattFrame=new JPanel();
    wattFrame.setLayout(new GridLayout(1, 3));
    wattFrame.add(twentyWatt);
    wattFrame.add(fortyWatt);
    wattFrame.add(sixtyWatt);

    frameContainer=new JPanel();
    frameContainer.setLayout(new GridLayout(3,3));
    frameContainer.add(toggleFrame);
    frameContainer.add(wattFrame);
    frameContainer.add(comboFrame);



    container.add(frameContainer, BorderLayout.EAST);
    container.add(statusText);

    //actions
    on.addActionListener(this);
    off.addActionListener(this);
    twentyWatt.addActionListener(this);
    fortyWatt.addActionListener(this);
    sixtyWatt.addActionListener(this);
    lightTimer.addActionListener(this);

    setSize(600, 400);
    setVisible(true);
}




public void actionPerformed(ActionEvent e) 
{
    String Action = e.getActionCommand();

    if (Action.equals ("On"))
    {
        lightbulb.setState(true);

        twentyWatt.setEnabled(true);
        fortyWatt.setEnabled(true);
        sixtyWatt.setEnabled(true);
        lightTimer.setEnabled(true);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }

    else if (Action.equals ("Off")) 
    {
        lightbulb.setState(false);
        twentyWatt.setEnabled(false);
        fortyWatt.setEnabled(false);
        sixtyWatt.setEnabled(false);
        lightTimer.setEnabled(false);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }

    else if (Action.equals ("20W")) 
    {
        lightbulb.setWattage(20);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }

    else if (Action.equals ("40W")) 
    {
        lightbulb.setWattage(40);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }

    else if (Action.equals ("60W")) 
    {
        lightbulb.setWattage(60);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }

    else if (Action.equals ("Morning")) 
    {
        lightbulb.setTime("Morning");
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }

    else if (Action.equals ("Evening")) 
    {
        lightbulb.setTime("Evening");
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }

    else if (Action.equals ("All day")) 
    {
        lightbulb.setTime("All day");
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    }


}   




 }

That's not how JComboBox's work.

  • Use separate listeners for separate components.
  • In this vein give the JComboBox its own ActionListener. An anonymous inner class will work well.
  • In its listener, get the selected item, call toString() on it, and you will have the combobox's selected String.

eg,

// lightTimer.addActionListener(this);
lightTimer.addActionListener(new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e) {
        String selection = lightTimer.getSelectedItem().toString();
        // use the String here
     }
});

Kinda hard to tell what the actual problem is since you did not provide an executable example, but looking at your code I guess the issue is that you did not specify the action command for your controls. The action command you obtain is not the text of the component, but rather the field set using component.setActionCommand(...) .

When an actionPerformed event on your combo box is triggered, the e.getActionCommand() value is comboBoxChanged . I changed the code in your actionPerformed method to the one below and it does run the code you expect it run:

public void actionPerformed(ActionEvent e) {
    String Action = e.getActionCommand();

    if (Action.equals("On")) {
        lightbulb.setState(true);
        twentyWatt.setEnabled(true);
        fortyWatt.setEnabled(true);
        sixtyWatt.setEnabled(true);
        lightTimer.setEnabled(true);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    } else if (Action.equals("Off")) {
        lightbulb.setState(false);
        twentyWatt.setEnabled(false);
        fortyWatt.setEnabled(false);
        sixtyWatt.setEnabled(false);
        lightTimer.setEnabled(false);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    } else if (Action.equals("20W")) {
        lightbulb.setWattage(20);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    } else if (Action.equals("40W")) {
        lightbulb.setWattage(40);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    } else if (Action.equals("60W")) {
        lightbulb.setWattage(60);
        statusText.setText("\t"+lightbulb.toString());
        this.repaint();
    } else if (Action.equals("comboBoxChanged")) {
        String item = (String) lightTimer.getSelectedItem();
        if (item.equals("Morning")) {
            lightbulb.setTime("Morning");
            statusText.setText("\t"+lightbulb.toString());
            this.repaint();
        } else if (item.equals("Evening")) {
            lightbulb.setTime("Evening");
            statusText.setText("\t"+lightbulb.toString());
            this.repaint();
        } else if (item.equals("All day")) {
            lightbulb.setTime("All day");
            statusText.setText("\t"+lightbulb.toString());
            this.repaint();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM