Java swing combobox actionListner





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















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();
}


}




}









share|improve this question























  • please learn java naming conventions and stick to them.

    – kleopatra
    Nov 4 '12 at 10:26











  • Well thanks for the constructive help on how to fix the proble....oh wait...

    – Eogcloud
    Nov 5 '12 at 16:05


















-1















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();
}


}




}









share|improve this question























  • please learn java naming conventions and stick to them.

    – kleopatra
    Nov 4 '12 at 10:26











  • Well thanks for the constructive help on how to fix the proble....oh wait...

    – Eogcloud
    Nov 5 '12 at 16:05














-1












-1








-1








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();
}


}




}









share|improve this question














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();
}


}




}






java swing user-interface combobox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 3 '12 at 17:47









EogcloudEogcloud

61631032




61631032













  • please learn java naming conventions and stick to them.

    – kleopatra
    Nov 4 '12 at 10:26











  • Well thanks for the constructive help on how to fix the proble....oh wait...

    – Eogcloud
    Nov 5 '12 at 16:05



















  • please learn java naming conventions and stick to them.

    – kleopatra
    Nov 4 '12 at 10:26











  • Well thanks for the constructive help on how to fix the proble....oh wait...

    – Eogcloud
    Nov 5 '12 at 16:05

















please learn java naming conventions and stick to them.

– kleopatra
Nov 4 '12 at 10:26





please learn java naming conventions and stick to them.

– kleopatra
Nov 4 '12 at 10:26













Well thanks for the constructive help on how to fix the proble....oh wait...

– Eogcloud
Nov 5 '12 at 16:05





Well thanks for the constructive help on how to fix the proble....oh wait...

– Eogcloud
Nov 5 '12 at 16:05












3 Answers
3






active

oldest

votes


















0














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();
}
}
}





share|improve this answer



















  • 2





    Sorry, but this makes an ugly switch-board actionPerformed method even uglier. One should strive to avoid combining too many things into one ActionListener as this makes for a debugging and updating nightmare.

    – Hovercraft Full Of Eels
    Nov 3 '12 at 18:03








  • 1





    I fully agree with the comment, but the above code helps the OP go on with the development. However, once this works, it should be cleaned up.

    – Dan D.
    Nov 3 '12 at 18:08






  • 2





    I disagree, in that it's much easier to create correct code than to try to clean it up later. Also, it's much safer to call toString() on the selected item than to cast it to a String. The latter is potentially dangerous, especially if the objects held by the JComboBox ever change and become non-String. Better still would be to use a generic ComboBox.

    – Hovercraft Full Of Eels
    Nov 3 '12 at 18:13






  • 1





    It depends how much time you have at the beginning. The toString() can return null and that can be dangerous as well. The best option would be the Java 1.7 generics enabled combo like JComboBox<String>.

    – Dan D.
    Nov 3 '12 at 18:16



















2














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.


e.g.,



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

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





share|improve this answer































    0














    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(...).






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13211817%2fjava-swing-combobox-actionlistner%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      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();
      }
      }
      }





      share|improve this answer



















      • 2





        Sorry, but this makes an ugly switch-board actionPerformed method even uglier. One should strive to avoid combining too many things into one ActionListener as this makes for a debugging and updating nightmare.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:03








      • 1





        I fully agree with the comment, but the above code helps the OP go on with the development. However, once this works, it should be cleaned up.

        – Dan D.
        Nov 3 '12 at 18:08






      • 2





        I disagree, in that it's much easier to create correct code than to try to clean it up later. Also, it's much safer to call toString() on the selected item than to cast it to a String. The latter is potentially dangerous, especially if the objects held by the JComboBox ever change and become non-String. Better still would be to use a generic ComboBox.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:13






      • 1





        It depends how much time you have at the beginning. The toString() can return null and that can be dangerous as well. The best option would be the Java 1.7 generics enabled combo like JComboBox<String>.

        – Dan D.
        Nov 3 '12 at 18:16
















      0














      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();
      }
      }
      }





      share|improve this answer



















      • 2





        Sorry, but this makes an ugly switch-board actionPerformed method even uglier. One should strive to avoid combining too many things into one ActionListener as this makes for a debugging and updating nightmare.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:03








      • 1





        I fully agree with the comment, but the above code helps the OP go on with the development. However, once this works, it should be cleaned up.

        – Dan D.
        Nov 3 '12 at 18:08






      • 2





        I disagree, in that it's much easier to create correct code than to try to clean it up later. Also, it's much safer to call toString() on the selected item than to cast it to a String. The latter is potentially dangerous, especially if the objects held by the JComboBox ever change and become non-String. Better still would be to use a generic ComboBox.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:13






      • 1





        It depends how much time you have at the beginning. The toString() can return null and that can be dangerous as well. The best option would be the Java 1.7 generics enabled combo like JComboBox<String>.

        – Dan D.
        Nov 3 '12 at 18:16














      0












      0








      0







      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();
      }
      }
      }





      share|improve this answer













      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();
      }
      }
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 3 '12 at 17:57









      Dan D.Dan D.

      30.9k45270




      30.9k45270








      • 2





        Sorry, but this makes an ugly switch-board actionPerformed method even uglier. One should strive to avoid combining too many things into one ActionListener as this makes for a debugging and updating nightmare.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:03








      • 1





        I fully agree with the comment, but the above code helps the OP go on with the development. However, once this works, it should be cleaned up.

        – Dan D.
        Nov 3 '12 at 18:08






      • 2





        I disagree, in that it's much easier to create correct code than to try to clean it up later. Also, it's much safer to call toString() on the selected item than to cast it to a String. The latter is potentially dangerous, especially if the objects held by the JComboBox ever change and become non-String. Better still would be to use a generic ComboBox.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:13






      • 1





        It depends how much time you have at the beginning. The toString() can return null and that can be dangerous as well. The best option would be the Java 1.7 generics enabled combo like JComboBox<String>.

        – Dan D.
        Nov 3 '12 at 18:16














      • 2





        Sorry, but this makes an ugly switch-board actionPerformed method even uglier. One should strive to avoid combining too many things into one ActionListener as this makes for a debugging and updating nightmare.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:03








      • 1





        I fully agree with the comment, but the above code helps the OP go on with the development. However, once this works, it should be cleaned up.

        – Dan D.
        Nov 3 '12 at 18:08






      • 2





        I disagree, in that it's much easier to create correct code than to try to clean it up later. Also, it's much safer to call toString() on the selected item than to cast it to a String. The latter is potentially dangerous, especially if the objects held by the JComboBox ever change and become non-String. Better still would be to use a generic ComboBox.

        – Hovercraft Full Of Eels
        Nov 3 '12 at 18:13






      • 1





        It depends how much time you have at the beginning. The toString() can return null and that can be dangerous as well. The best option would be the Java 1.7 generics enabled combo like JComboBox<String>.

        – Dan D.
        Nov 3 '12 at 18:16








      2




      2





      Sorry, but this makes an ugly switch-board actionPerformed method even uglier. One should strive to avoid combining too many things into one ActionListener as this makes for a debugging and updating nightmare.

      – Hovercraft Full Of Eels
      Nov 3 '12 at 18:03







      Sorry, but this makes an ugly switch-board actionPerformed method even uglier. One should strive to avoid combining too many things into one ActionListener as this makes for a debugging and updating nightmare.

      – Hovercraft Full Of Eels
      Nov 3 '12 at 18:03






      1




      1





      I fully agree with the comment, but the above code helps the OP go on with the development. However, once this works, it should be cleaned up.

      – Dan D.
      Nov 3 '12 at 18:08





      I fully agree with the comment, but the above code helps the OP go on with the development. However, once this works, it should be cleaned up.

      – Dan D.
      Nov 3 '12 at 18:08




      2




      2





      I disagree, in that it's much easier to create correct code than to try to clean it up later. Also, it's much safer to call toString() on the selected item than to cast it to a String. The latter is potentially dangerous, especially if the objects held by the JComboBox ever change and become non-String. Better still would be to use a generic ComboBox.

      – Hovercraft Full Of Eels
      Nov 3 '12 at 18:13





      I disagree, in that it's much easier to create correct code than to try to clean it up later. Also, it's much safer to call toString() on the selected item than to cast it to a String. The latter is potentially dangerous, especially if the objects held by the JComboBox ever change and become non-String. Better still would be to use a generic ComboBox.

      – Hovercraft Full Of Eels
      Nov 3 '12 at 18:13




      1




      1





      It depends how much time you have at the beginning. The toString() can return null and that can be dangerous as well. The best option would be the Java 1.7 generics enabled combo like JComboBox<String>.

      – Dan D.
      Nov 3 '12 at 18:16





      It depends how much time you have at the beginning. The toString() can return null and that can be dangerous as well. The best option would be the Java 1.7 generics enabled combo like JComboBox<String>.

      – Dan D.
      Nov 3 '12 at 18:16













      2














      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.


      e.g.,



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

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





      share|improve this answer




























        2














        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.


        e.g.,



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

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





        share|improve this answer


























          2












          2








          2







          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.


          e.g.,



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

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





          share|improve this answer













          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.


          e.g.,



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

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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 3 '12 at 17:54









          Hovercraft Full Of EelsHovercraft Full Of Eels

          263k20213319




          263k20213319























              0














              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(...).






              share|improve this answer




























                0














                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(...).






                share|improve this answer


























                  0












                  0








                  0







                  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(...).






                  share|improve this answer













                  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(...).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 3 '12 at 17:55









                  sarcansarcan

                  2,6691320




                  2,6691320






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13211817%2fjava-swing-combobox-actionlistner%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas