grigory mendel grigory mendel - 22 days ago 14
Java Question

While scrolling using up/down arrow in JComboBox, It shouldn't update the top tile which shows selected item

I want my combobox top tile only get updated when we hit enter key, not while scrolling using up/down arrows..

Currently when I scroll using up/down arrow, the current item which is currently highlighted is getting updated in top tile of

JComboBox
as selected item. I need to avoid this.

Here is my code. Thanks in advance!

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

Answer

Oracle Java has fixed this issue as it is not in par with selection using mouse. But to enable the fix, we have to change default SWING UI property with key "ComboBox.noActionOnKeyNavigation". In the fix implemented in SourceCode they are using this property as flag. If we make it as true, JComboBox selection using keyboard wont trigger Action event. This link will give you details of the fix. This is from OpenJDK but i have confirmed in Oracle JDK 1.8.111 http://osdir.com/ml/swing-dev-java-openjdk/2013-01/msg00029.html

So Basically we have to add this lane

UIManager.getLookAndFeelDefaults().put("ComboBox.noActionOnKeyNavigation", true);

Comments