基于最新的KeyStroke填充JTextField [英] Populating JTextField based on latest KeyStroke
问题描述
我的UI中的用例是基于双击 JList
中的项目来填充两个 JTextField
组件。容易的是使用 JCheckBox
填充 jTextField1
如果选中该复选框,如果不是则填充另一个选择反之亦然,反之亦然。
The use case in my UI is to populate two JTextField
components based on double clicking items in a JList
. The easy was is to use a JCheckBox
populate jTextField1
if the checkbox is selected, and populate the other if it is not selected or vice versa which is working perfectly.
但我想探讨是否可以在没有复选框的情况下完成此操作。
But I would like to explore if this can be done without the checkbox.
如果我在 jtextfield1
中键入内容并双击列表中的项目以完成文本,点击的项目应根据我使用的最新 KeyStroke
附加到 jtextfield1
。
As in, if I type something in jtextfield1
and double click the item in list to complete the text, the item clicked should be appended to jtextfield1
based on fetching the latest KeyStroke
I used.
有没有办法做到这一点?
Is there are any way to do this?
推荐答案
使用方法在我的UI中的情况是基于双击jlist中的项目来填充两个jTextField。
The use case in my UI is to populate two jTextField's based on double clicking items in a jlist.
嗯,通常应该设计一个UI您可以使用鼠标或键盘来调用Action。那就是你应该能够双击或使用所选项目上的Enter键。
Well, normally a UI should be designed so that you can use the mouse or the keyboard to invoke an Action. That is you should be able to double click or use the Enter key on the selected item.
查看列表操作,用于允许您使用 Action $ c实现此功能的简单类$ c>。
Check out List Action for a simple class that allows you to implement this functionality by using an Action
.
现在,当您创建 Action
时,您可以扩展 TextAction
。然后你可以使用 TextAction
中的 getFocustComponent()
方法来确定最后有焦点的文本组件并添加从所选项目到该文本字段的文本。
Now when you create your Action
you can extend TextAction
. Then you can use the getFocustComponent()
method from the TextAction
to determine the text component that last had focus and add the text from the selected item to that text field.
自定义操作
的基本代码为:
JList list = (JList)e.getSource();
JTextComponent textField = getFocusedComponent();
textField.setText( list.getSelectedValue().toString() );
注意:如果窗口包含更多内容,您需要验证焦点是否在两个字段之一两个文本组件。
Note: you will need to verify if focus is on one of the two fields if your window contains more than two text components.
要使用FocusListener方法,您需要在类中定义一个实例变量:
To use the FocusListener approach you would need to define an instance variable in your class:
private JTextField lastFocusedTextField = null;
然后在您创建文本字段的类的构造函数中,您将创建监听器:
Then in the constructor of your class where you create the text fields you would create the listener:
FocusListener fl = new FocusAdapter()
{
@Override
public void focusGained(FocusEvent e)
{
lastFocusedTextField = (JTextField)e.getSource();
}
};
JTextField textField1 = new JTextField(...);
textField1.addFocusListener( fl );
// same for the other text field
现在需要添加 MouseListener
到 JList
。在 mouseClicked(...)
方法中,您可以执行以下操作:
Now you need to add a MouseListener
to the JList
. In the mouseClicked(...)
method you do something like:
JList list = (JList)e.getSource();
lastFocusedTextField.setText( list.getSelectedValue().toString() );
所以你需要:
- 一个实例变量
- 一个FocusListener
- 一个MouseListener
这篇关于基于最新的KeyStroke填充JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!