I would like to be able to respond when the current highlighted item in the JComboBox drop-down list changes. Note that I'm not looking for a way to get the currently selected item, but highlighted. When the mouse hovers over this popup, it selects the item at the mouse position, but it does not affect the currently selected item, so I canβt just listen through the ItemListener or ActionListener to achieve what I want.
I am trying to create a component that consists of a JComboBox and an associated tooltip that displays additional information (documentation) for the currently selected item.
As a first attempt, I add code to my constructor (extended JComboBox ):
import java.awt.BorderLayout; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.accessibility.AccessibleContext; import javax.accessibility.AccessibleState; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.SwingUtilities; import javax.swing.plaf.basic.ComboPopup; public class SomeFrame extends JFrame { private MyComboBox combo; public SomeFrame() { setDefaultCloseOperation(DISPOSE_ON_CLOSE); setSize(100,20); setLocationRelativeTo(null); setLayout(new BorderLayout()); combo = new MyComboBox(); combo.setModel(new DefaultComboBoxModel(new String[]{"one", "two", "three", "four"})); add(combo); pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { SomeFrame frame = new SomeFrame(); frame.setVisible(true); } }); }
This seems to work, but I got into this code through several shadow channels and trial / error, so I think there should be a better way to do this. Any ideas? Is production even safe over code?
predi source share