Java Swing,JComboBox 下拉列表在单击前更改侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7984621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:34:09  来源:igfitidea点击:

Java Swing, JComboBox drop down list change listener before clicking

javaswinglistenerjcombobox

提问by Frank

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.

我有一个 JComboBox,它有一个 MIDI 文件列表,我想知道以下是否可行:当我点击 JComboBox 时,会打开一个下拉列表,当我将鼠标移到一个 MIDI 文件时,它会播放 10 秒示例声音,所以我在单击并选择该文件之前就知道文件包含什么,所以如果我有 50 个 midi 文件,我可以打开列表并在列表中上下移动鼠标而不点击它,但仍然播放 10鼠标指向的文件中的第二个样本,然后在我决定哪个样本后,单击它,该样本将成为 JComboBox 中的选定样本。

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

如何从 JComboBox 获得鼠标位置变化/指向事件的通知?

回答by mKorbel

How to get notified for the mouse position change/pointing events 
from the JComboBox ?
  • by implements ItemListener, that fired twice (SELECTEDand DESELECTED)
  • 通过实现ItemListener,触发两次(SELECTEDDESELECTED

or

或者

  • add there CustomRenderer
  • 在那里添加 CustomRenderer

or ...

或者 ...

three possible Events for JComboBox for example

例如 JComboBox 的三个可能的事件

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

回答by Mechkov

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

FocusListener 可能是个好主意。这里有一些关于如何实现一个的很好的信息。

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

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