We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,894 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Add ActionListener to the Component of JComboBox

Hi, i am new here and i want an help in add the actionListener to the component of JCombo Box. Like there is Combo Box in Which there are components like name of color. When i click any component, for example i click Green color the background turns into Green. Thanx in advance..

4
Contributors
5
Replies
1 Day
Discussion Span
9 Months Ago
Last Updated
6
Views
OsamaJutt
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Can you post the code you are having trouble with?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Here is the listener, all you need to do further is get the selected value of the combo (within the actionPerformed(ActionEvent e) method )and then change the colour of the background based on that value!

myCombobox.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        //change back ground colour
    }
});

Hope that helps!

majestic0110
Nearly a Posting Virtuoso
1,332 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
Skill Endorsements: 4
public class Backgroundcolor implements ActionListener
{
    private JComboBox change;
    public Backgroundcolor()
    {
        JFrame f = new JFrame();
        JPanel panel = new JPanel();
        panel.setBackground(Color.DARK_GRAY);
        f.setSize(500,600);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] Change = {"Green","Blue"};
        change = new JComboBox(Change);
        change.setSize(120,30);
        change.setLocation(300,250);
        change.addActionListener(this);
        panel.add(change);
        f.setResizable(false);
        f.setVisible(true);
        f.add(panel);

    }
    public static void main (String[] args) 
        {
            new Backgroundcolor();
          }
               public void actionPerformed(ActionEvent e)
               {
                if(e.getSource()==change.getSelectedIndex())
                 {
                    change.setBackground(Color.blue);
                 }
               }

}
OsamaJutt
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This is my whole code, so please help me according to that...

OsamaJutt
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

if(e.getSource()==change.getSelectedIndex())

e.getSource() is the object where the event happened
change.getSelectedIndex() is the line that's currently selected
these are two quite different things, and will never be equal.

You probabaly want something more like:

if(e.getSource()==change) {  // use clicked in the change object
  int selectedLine = change.getSelectedIndex();  // this is the number of the selected line
  // change background colour
  ...
JamesCherrill
... trying to help
Moderator
8,514 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0987 seconds using 2.76MB