Java Swing, JComboBox drop down list change listener before clicking

北战南征 提交于 2019-12-02 05:11:41

问题


I have a JComboBox which has a list of midi files, I wonder if the following is doable : when I click on the JComboBox, a drop down list opens up, when I move the mouse on to a midi file, it plays a 10 second sample sound, so I know what the file contains before I click and select that file, so if I have 50 midi files, I can open the list and move the mouse up and down the list without clicking on it, but still play the 10 second samples from the file the mouse points to, then after I decide which one, click on it, and that one will be the selected one in the JComboBox.

How to get notified for the mouse position change/pointing events from the JComboBox ?


回答1:


How to get notified for the mouse position change/pointing events 
from the JComboBox ?
  • by implements ItemListener, that fired twice (SELECTED and DESELECTED)

or

  • add there CustomRenderer

or ...

three possible Events for JComboBox for example

import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;


public class ComboBoxHoverOver {

    private JComboBox combo = new JComboBox();

    public ComboBoxHoverOver() {
        combo.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXX");
        combo.setRenderer(new ComboToolTipRenderer(combo));
        combo.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                System.out.println(combo.getSelectedItem().toString());
            }
        });
        combo.addItem("");
        combo.addItem("Long text 4");
        combo.addItem("Long text 3");
        combo.addItem("Long text 2");
        combo.addItem("Long text 1");
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(combo);
        f.pack();
        f.setVisible(true);
    }

    private class ComboToolTipRenderer extends BasicComboBoxRenderer {

        private static final long serialVersionUID = 1L;
        private JComboBox combo;
        private JList comboList;

        ComboToolTipRenderer(JComboBox combo) {
            this.combo = combo;
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (comboList == null) {
                comboList = list;
                KeyAdapter listener = new KeyAdapter() {

                    @Override
                    public void keyReleased(KeyEvent e) {
                        if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP) {
                            int x = 5;
                            int y = comboList.indexToLocation(comboList.getSelectedIndex()).y;
                            System.out.println(comboList.getSelectedIndex());
                        }
                    }
                };
                combo.addKeyListener(listener);
                combo.getEditor().getEditorComponent().addKeyListener(listener);
            }
            if (isSelected) {
                System.out.println(value.toString());
            }
            return this;
        }
    }

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComboBoxHoverOver comboBoxHoverOver = new ComboBoxHoverOver();
            }
        });
    }
}



回答2:


FocusListener might be a good idea. Here is some good information about how to implement one.

http://download.oracle.com/javase/tutorial/uiswing/events/focuslistener.html



来源:https://stackoverflow.com/questions/7984621/java-swing-jcombobox-drop-down-list-change-listener-before-clicking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!