单击之前,Java Swing,JComboBox下拉列表更改侦听器 [英] Java Swing, JComboBox drop down list change listener before clicking

查看:80
本文介绍了单击之前,Java Swing,JComboBox下拉列表更改侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JComboBox,其中包含Midi文件列表,我想知道以下是否可行: 当我单击JComboBox时,将打开一个下拉列表,当我将鼠标移至midi文件时,它将播放10秒钟的示例声音,因此在单击并选择该文件之前,我知道文件包含的内容. ,因此,如果我有50个midi文件,则可以打开列表并在列表上上下移动鼠标,而无需单击它,但是仍然可以播放鼠标指向的文件中的10秒钟样本,然后确定要单击的那一个,然后将其选为JComboBox中的一个.

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获得有关鼠标位置更改/指向事件的通知?

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

推荐答案

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

  • 通过实现 ItemListener ,它触发了两次(DESELECTED)
    • by implements ItemListener, that fired twice (SELECTED and DESELECTED)
      • 在其中添加CustomRenderer

      或...

      例如,JComboBox的三个可能的事件

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

      这篇关于单击之前,Java Swing,JComboBox下拉列表更改侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆