我希望我的组合框顶部磁贴仅在我们按下回车键时更新,而不是在使用向上/向下箭头滚动时更新..
目前,当我使用向上/向下箭头滚动时,当前突出显示的当前项目正在 JComboBox
的顶部磁贴中更新为所选项目。我需要避免这种情况。
这是我的代码。提前致谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo() {
prepareGUI();
}
public static void main(String[] args) {
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showComboboxDemo();
}
private void prepareGUI() {
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setSize(350, 100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showComboboxDemo() {
headerLabel.setText("Control in action: JComboBox");
final DefaultComboBoxModel fruitsName = new DefaultComboBoxModel();
fruitsName.addElement("Apple");
fruitsName.addElement("Grapes");
fruitsName.addElement("Mango");
fruitsName.addElement("Peer");
fruitsName.addElement("java");
fruitsName.addElement("Mango");
final JComboBox fruitCombo = new JComboBox(fruitsName);
fruitCombo.setSelectedIndex(0);
JScrollPane fruitListScrollPane = new JScrollPane(fruitCombo);
JButton showButton = new JButton("Show");
fruitCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getModifiers() == ActionEvent.MOUSE_EVENT_MASK || arg0.getModifiers() == ActionEvent.KEY_EVENT_MASK || true) {
System.out.println("arg0" + " " + arg0.getModifiers());
}
}
});
fruitCombo.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("enter key pressed" + arg0.getKeyCode());
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
System.out.println(fruitCombo.getComponentListeners().toString());
controlPanel.add(fruitListScrollPane);
controlPanel.add(showButton);
mainFrame.setVisible(true);
}
}
最佳答案
您可以在组合框上设置一个属性,以防止在使用箭头键时触发事件:
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
注意:您可能(也可能不需要)在使用上述属性时手动关闭弹出窗口:
/*
This works on non editable combo boxes
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.*;
public class ComboBoxAction extends JFrame implements ActionListener
{
public ComboBoxAction()
{
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addActionListener( this );
comboBox.addItem( "Item 1" );
comboBox.addItem( "Item 2" );
comboBox.addItem( "Item 3" );
comboBox.addItem( "Item 4" );
// This prevents action events from being fired when the
// up/down arrow keys are used on the dropdown menu
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
getContentPane().add( comboBox );
getContentPane().add( new JTextField(), BorderLayout.SOUTH );
}
public void actionPerformed(ActionEvent e)
{
System.out.println( e.getModifiers() );
JComboBox comboBox = (JComboBox)e.getSource();
System.out.println( comboBox.getSelectedItem() );
// make sure popup is closed when 'isTableCellEditor' is used
// comboBox.hidePopup();
}
public static void main(String[] args)
{
ComboBoxAction frame = new ComboBoxAction();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
关于java - 在 JComboBox 中使用向上/向下箭头滚动时,它不应该更新显示所选项目的顶部图 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480572/