0

I need to know how to effectively add a mouse event to a JComboBox or any other approach that works. I found some possible solutions here and also different sites but I can't get it to work. It seems that mouseEvent is not appropriate to use on JComboBox as it is a compound component. I found a possible solution for a compound component but also doesn't work. So below is my code that works when I use a text field. Any ideas of which approach should I use? Thanks

    private void updateReviewers() {
        jComboBox_reviewer.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("clicked");
        }

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("pressed");
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println("released");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("entered");
        }

        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("exited");
        }

    }
    );

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bob
  • 564
  • 2
  • 10
  • 25

2 Answers2

1

You ought to be able to use addActionListener(ActionEvent e) on the JComboBox itself. Once any item is selected you may perform any sort of validation within the action listener.

jcomboBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            JComboBox comboBox = (JComboBox) event.getSource();
            Object o = comboBox.getSelectedItem();
            //Any extra code
        }
    });  

Ofcourse, Object may be cast to your desired Object type.

Oracle Documentation for Event handling with JComboBox

Juxhin
  • 5,068
  • 8
  • 29
  • 55
  • thanks @Juxhin, is there a way to add an event when the comboBox is clicked, even before selecting an item from the list? – Bob Apr 04 '15 at 10:32
  • Yes, the code presented will do that. I will be honest and say I'm not sure what `Object` is returning when calling `getSelectedItem` (as you would have just clicked the combo-box and no item inside it) however my gut tells me it would return the first Object by default. However from what you've said I don't think you should have issues with that. – Juxhin Apr 04 '15 at 10:34
  • Sorry @Juxhin, but the above code only works when I select an element from the list. Not sure if I miss understood something here but its not working for me. – Bob Apr 04 '15 at 10:55
  • Just tested the code and it seems to be working for me. Could you explain exactly what you are trying to achieve as I believe I'm misunderstanding something. Thanks – Juxhin Apr 04 '15 at 10:59
  • I am trying to update the items of the comboBox when the user clicks on it. For testing purposes I am using only a System.out.print to check if it will print when I click on the combo. However it prints only when I select an item from the list. see the code below. Thanks for your help. jComboBox_reviewer.addActionListener(( ActionEvent e) -> { JComboBox comboBox = (JComboBox) e.getSource(); Object o = comboBox.getSelectedItem(); System.out.println("tets"); }); – Bob Apr 04 '15 at 11:04
  • So from what you are telling me, you are trying to update the contents prior to clicking the JComboBox. That is fairly odd compared to the usual use of JComboBox. The ActionListener won't work nor will the ItemListener. I'm not quite sure how to achieve exactly what you're asking for. I'll do some research and keep you updated. – Juxhin Apr 04 '15 at 11:12
  • @user3545850 - I've asked around and it seems that your behaviour is quite possible unfortunately. You could try mocking however there is no guarantee. My personal advice would be to modify the behaviour slighty – Juxhin Apr 04 '15 at 11:18
  • Many thanks, I am trying to find a different approach. Thanks for your help. – Bob Apr 04 '15 at 11:42
  • My pleasure, if you have any more queries feel free to head over my profile and email me. Have a good one. EDIT: Just noticed my previous typo, meant to type, "is not quite possible". – Juxhin Apr 04 '15 at 11:42
0

It is a program to make a JComboBox and make a sting array and use those array items and make the ComboBox's list items. Then link each item with an image. Then we start the Action Listener and provide an action to each of the list item. Note that you must save the images in the source folder of the project and the class folder.

package JComboBox;

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    /*<applet code="JComboBoxDemo" width=200 height=120></applet>
     */

    public class JComboBoxDemo extends JApplet
    {

        JLabel jlab;
        ImageIcon hourglass, digital, analog, stopwatch;
        JComboBox <String> jcb;
        String timepieces[] = {"Digital", "Analog", "Hourglass",  "Stopwatch"};
        String s;

        public void init()
        {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {

                    public void run() {

                        makeGUI();
                    }
                });
            }

            catch(Exception exc)
            {
                System.out.println("Program can't run because of "+exc);
            }
        }

        private void makeGUI()
        {
            setLayout(new FlowLayout());
            jcb = new JComboBox<String>(timepieces);
            add(jcb);

            jcb.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent ae) {

                    s = (String) jcb.getSelectedItem();
                    jlab.setIcon(new ImageIcon(s + ".jpg"));
                }
            });

                jlab = new JLabel(new ImageIcon());
                add(jlab);
        }
    }