用于 Enter 键的 Java 可编辑 JCombobox Keylistener 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14056301/
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
Java Editable JCombobox Keylistener event for Enter key
提问by Joe
I have editable JCombobox and I added keylistener for combobox editor component. When user press 'Enter key' and if there is no text on the editable combobox I need to display message box using JOptinoPane. I have done necessary code in keyrelease event and it displays message as expected.
我有可编辑的 JCombobox,并为组合框编辑器组件添加了密钥侦听器。当用户按下“回车键”并且可编辑组合框上没有文本时,我需要使用 JOptinoPane 显示消息框。我已经在 keyrelease 事件中完成了必要的代码,它按预期显示了消息。
Problem is, when we get message box and if user press enter key on 'OK' button of JOptionPane, combobox editor keyevent fires again. Because of this, when user press Enter key on message box, JoptionPane displays continuously.
问题是,当我们收到消息框时,如果用户按下 JOptionPane 的“确定”按钮上的 Enter 键,组合框编辑器的 keyevent 会再次触发。因此,当用户在消息框中按下 Enter 键时,JoptionPane 会持续显示。
Any idea how to solve this?
知道如何解决这个问题吗?
Note that I can't use Action listener for this.
请注意,我不能为此使用 Action 侦听器。
回答by Chand Priyankara
Please check if this code helps you!!!
请检查此代码是否对您有帮助!!!
JFrame frame = new JFrame("Welcome!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox cmb = new JComboBox();
cmb.setEditable(true);
cmb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent event) {
if (event.getKeyChar() == KeyEvent.VK_ENTER) {
if (((JTextComponent) ((JComboBox) ((Component) event
.getSource()).getParent()).getEditor()
.getEditorComponent()).getText().isEmpty())
System.out.println("please dont make me blank");
}
}
});
frame.add(cmb);
frame.setLocationRelativeTo(null);
frame.setSize(300, 50);
frame.setVisible(true);
Most people find it difficult because of this casting.
大多数人会因为这种铸造而感到困难。
回答by Aqeel Haider
We need to add a key listener on the component that the combo box is using to service the editing.
我们需要在组合框用于为编辑提供服务的组件上添加一个关键侦听器。
JTextComponent editor = (JTextComponent) urCombo.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
// your code
}
});
Hope this code helps.
希望这段代码有帮助。
回答by mKorbel
Note that I can't use Action listener for this.
this doesn't make me any sence, then to use ItemListener
这对我没有任何意义,然后使用 ItemListener
Any idea how to solve this?
never to use KeyListener for
Swing JComponents
, use (Note that I can't use Action listener for this
.) KeyBindingsinstead,notice
ENTER key
is implemented forJComboBox
inAPI
by default, have to override this action fromENTER key pressed
永远不要使用KeyListener for
Swing JComponents
,而是使用 (Note that I can't use Action listener for this
.) KeyBindings,通知
ENTER key
是为实现JComboBox
在API
默认情况下,已覆盖这一行动ENTER key pressed
回答by phcoding
One option would be to replace the KeySelectionManager interface with your own. You want to replace the JComboBox.KeySelectionManager as it is responsible for taking the inputted char and returns the row number (as an int) which should be selected.
一种选择是用您自己的接口替换 KeySelectionManager 接口。您想替换 JComboBox.KeySelectionManager,因为它负责获取输入的字符并返回应选择的行号(作为整数)。
回答by vichu
Please check the event ascii code by ev.getkeycode()
and check if it is a number or character. If it is neither a number nor a character do nothing.
If it is what you want then do the process.
请检查事件ascii代码ev.getkeycode()
并检查它是数字还是字符。如果它既不是数字也不是字符,则什么也不做。如果这是您想要的,那么请执行该过程。
回答by Wajid Hussain
If you are using Netbeans then right click on your combobox and select customize code. add following lines of code
如果您使用的是 Netbeans,则右键单击您的组合框并选择自定义代码。添加以下代码行
JTextComponent editor = (JTextComponent) Code.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
if(evt.getKeyCode()==10)
//do your coding here.
}
});