简体   繁体   English

如何在Java中聚焦找到JTextField?

[英]How to find the JTextField in focus in Java?

In my Java Swing app, I have multiple JTextFields for dates, there is a JButton when clicked, it will open a calendar to pick a date, and the date string should be inserted into one of the JTextFields, so I want to design the program so that user first clicks in a date JTextField he wants to input a date [ get focus on that field and remember it ], the program saves the JTextField as target component, then that component is passed to the calendar object to input the picked date. 在我的Java Swing应用程序中,我有多个用于日期的JTextField,单击时有一个JButton,它将打开一个日历以选择一个日期,并且日期字符串应插入其中一个JTextField中,因此我想设计程序因此,用户首先单击要输入日期的日期JTextField [专注于该字段并记住它],程序将JTextField保存为目标组件,然后将该组件传递到日历对象以输入选择的日期。 So far I can hard code the program so it can input any date string I pick in to a certain JTextField I've hard coded with, but the problem is how to remember the JTextField user clicked on ? 到目前为止,我可以对程序进行硬编码,以便可以将任何输入的日期字符串输入经过硬编码的某个JTextField中,但是问题是如何记住JTextField用户所单击的内容? So I don't have to hard code it. 因此,我不必对其进行硬编码。

I've tried : Date_Field.getDocument().addDocumentListener(A_Listener); 我试过了:Date_Field.getDocument()。addDocumentListener(A_Listener); It won't get the focus until user starts typing in it. 除非用户开始输入焦点,否则它不会成为焦点。

I've also tried : 我也尝试过:

Date_Field.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
     Focused_Date_TextField=(JTextField)evt.getSource();
  }
});

Also didn't work, because when user clicked on it, there is no action yet [ therefore no focus ] until user starts typing. 也不起作用,因为当用户单击它时,在用户开始键入之前尚无任何操作[因此没有焦点]。

So what's a way to get the JTextField when user JUST clicks on it without typing anything ? 那么,当用户仅单击而不输入任何内容时,如何获取JTextField?

在此处输入图片说明

Combine both the JTextField and JButton into a custom component (maybe a JPanel ) and isolate the functionality within it. JTextFieldJButton到一个自定义组件(可能是JPanel )中,并隔离其中的功能。 This way, the button is ALWAYS associated with a given JTextField and you don't care about which field was "focused" last 这样,该按钮始终与给定的JTextField关联,并且您不必担心最后“聚焦”哪个字段

public class DateField extends JPanel {
    private JTextField field;
    private JButton btn;

    public DateField() {
        setLayout(new FlowLayout(FlowLayout.LEFT));

        field = new JTextField(11);
        btn = new JButton("...");

        add(field);
        add(btn);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Show calendar, get date do all that funky stuff
                field.setText(dateAsString);
            }
        });
    }

}

Then you can create as many of these as you want and add them to whatever container you want 然后,您可以根据需要创建任意多个容器,并将它们添加到所需的任何容器中

The suggestion by MadProgrammer is the way you should be solving the problem. MadProgrammer的建议是您应该解决问题的方式。 It is the common UI that you will see in other application. 这是您将在其他应用程序中看到的常见UI。

How to find the JTextField in focus in Java? 如何在Java中聚焦找到JTextField?

However to answer your more general question above when dealing with text components you can create an Action that extends from TextAction . 但是回答与文字打交道的组件,你可以创建一个以上的时候你更普遍的问题Action ,从扩展TextAction The TextAction has a getFocusedComponent() method which returns the last text component with focus. TextAction具有getFocusedComponent()方法,该方法返回具有焦点的最后一个文本组件。

For example: 例如:

public class SelectAll extends TextAction
{
    public SelectAll()
    {
        super("Select All");
    }

    public void actionPerformed(ActionEvent e)
    {
        JTextComponent component = getFocusedComponent();
        component.selectAll();
        component.requestFocusInWindow();
    }
}

So typically you would use this class for creating a JMenuItem and then add the menu item to a JMenu on your JMenuBar . 因此,通常,您将使用此类创建JMenuItem ,然后将菜单项添加到JMenuBar上的JMenu This is in fact how the cut/copy/paste Actions of the JDK work. 实际上,这就是JDK的剪切/复制/粘贴操作的工作方式。

Again, I don't recommend it as a solution for this problem, but for future problems when you want to share an Action between multiple text components. 同样,我不建议您将其作为解决此问题的解决方案,而建议您在多个文本组件之间共享Action时解决此问题。

Also, when you use this approach the getFocusedComponent() method will return any text component so you can't be guaranteed that the last component to have focus would be one of your date fields. 同样,当您使用这种方法时, getFocusedComponent()方法将返回任何文本组件,因此不能保证最后一个具有焦点的组件将是您的日期字段之一。 Another reason not to use this approach. 不使用这种方法的另一个原因。

Thanks for the answers and suggestions, but they are not what I was looking to find. 感谢您的回答和建议,但它们并不是我一直在寻找的。 Too much trouble and I don't have enough space in my app to add a button to each field, even if I do have enough space, they look too busy for dozens of fields each has it's own button, I want to clean simple look and yet still functions the way I like, so I did some search and found the ideal answer, here is how to achieve it : 太麻烦了,我的应用程序中没有足够的空间向每个字段添加按钮,即使我确实有足够的空间,它们对于几十个字段也都显得很忙,每个字段都有自己的按钮,我想清理简单的外观但仍然按照我喜欢的方式运行,因此我进行了一些搜索,找到了理想的答案,这是如何实现的:

Date_TextField[Index].addFocusListener(new FocusListener()
{
  public void focusGained(FocusEvent e)
  {
    Out("Focus gained : "+e.toString());
    Focused_Date_TextField=(JTextField)e.getSource();
  }

  public void focusLost(FocusEvent e)
  {
    Out("Focus lost : "+e.toString());
  }
}); 

I then pass Focused_Date_TextField to the calendar, so when a date is picked, the date text will be input into the user selected JTextField. 然后,我将Focused_Date_TextField传递给日历,因此,选择日期后,日期文本将输入到用户选择的JTextField中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM