Search code examples
javaswingjbuttonactionlistenerjcombobox

Issue with JComboBox and JButton listen event


I am trying to create a program where the user selects a colour from a JComboBox and presses a JButton to change the JPanel colour to the colour the user selects. I cannot seem to get the action event logic working correctly.

I have set the action listener to get the selected item value and apply it as the selected background for the panel but it does not compile.

My Attempt

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class EventTest extends JFrame implements ActionListener
{

    EventTest()
    {

        super("My First Frame");

        //Create a JPanel and content frame
        JPanel panel = new JPanel();
        Container contentPane = getContentPane();

        //Create a list of colours for our JCombo Box
        String colours[] = { "Red", "Yellow", "Green", "Blue", "Orange" };

        //Create a JCombo Box
        JComboBox colourSelector = new JComboBox ( colours );
        colourSelector.addActionListener ( this );  //Add listener to the box
        panel.add ( colourSelector );   //Add combobox to the panel

        //Create a JButton
        JButton changeColour = new JButton ( "Change Colour" ); //Create a new JButton.
        changeColour.addActionListener ( this ); //Add listener to the button.
        panel.add ( changeColour ); //Add button to the panel.

        //Add the content to the pane
        contentPane.add ( panel );

        //Set window parameters
        setTitle ( "Lab4" );
        setSize ( 800, 600 );
        setVisible ( true );

    }//End Constructor


    public static void main ( String args[] )
    {
        EventTest myFrame = new EventTest();
    }

    //Action Listener - An action listener that changes the colour of our JPanel to the
    //colour selected by the user.
    public void actionPerformed ( ActionEvent e)
    {

        int selectedItem = colourSelector.getSelectedItem();
        panel.setBackground ( Color.selectedItem ); //Set the background colour based on our list.

    }


}//End Class

Solution

  • Your basic problem is one of variable scope. The variables you are defining in your constructor can not be accessed out side the scope of the constructor.

    You could solve the problem using a anonymous listener, but for the sake of education, you should probably make those variables you want to access in different methods instance fields, for example:

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class EventTest extends JFrame implements ActionListener {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new EventTest();
                }
            });
        }
    
        private JComboBox<String> colourSelector;
        private String colours[] = {"Red", "Yellow", "Green", "Blue", "Orange"};
        private JPanel panel;
    
        EventTest() {
    
            super("My First Frame");
    
            //Create a JPanel and content frame
            Container contentPane = getContentPane();
            panel = new JPanel();
    
            //Create a JCombo Box
            colourSelector = new JComboBox(colours);
            colourSelector.addActionListener(this);  //Add listener to the box
            panel.add(colourSelector);   //Add combobox to the panel
    
            //Create a JButton
            JButton changeColour = new JButton("Change Colour"); //Create a new JButton.
            changeColour.addActionListener(this); //Add listener to the button.
            panel.add(changeColour); //Add button to the panel.
    
            //Add the content to the pane
            contentPane.add(panel);
    
            //Set window parameters
            setTitle("Lab4");
            setSize(800, 600);
            setVisible(true);
    
        }//End Constructor
    
        //Action Listener - An action listener that changes the colour of our JPanel to the
        //colour selected by the user.
        public void actionPerformed(ActionEvent e) {
    
    //        private String colours[] = {"Red", "Yellow", "Green", "Blue", "Orange"};
    
            String selectedItem = (String)colourSelector.getSelectedItem();
            System.out.println(selectedItem);
            switch (selectedItem) {
                case "Red": 
                    panel.setBackground(Color.RED);
                    break;
                case "Yellow": 
                    panel.setBackground(Color.YELLOW);
                    break;
                case "Green": 
                    panel.setBackground(Color.GREEN);
                    break;
                case "Blue": 
                    panel.setBackground(Color.BLUE);
                    break;
                case "Orange": 
                    panel.setBackground(Color.ORANGE);
                    break;
            }
        }
    
    }//End Class