简体   繁体   English

如何在创建时将焦点设置在JOptionPane内的特定JTextfield上?

[英]How to set focus on specific JTextfield inside JOptionPane when created?

I want to set the focus on a specific JTextField which is passed to the JOptionPane as Object Message. 我想将焦点放在一个特定的JTextField上,它作为对象消息传递给JOptionPane。 Here is my code (I want the focus on txt2 but the focus always is on txt1): 这是我的代码(我希望专注于txt2,但焦点始终在于txt1):

import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TextArea extends JPanel
{
    private JTextArea txt1 = new JTextArea();
    private JTextArea txt2 = new JTextArea();
    public TextArea()
    {
        setLayout(null);
        setPreferredSize(new Dimension(200,100));
        txt1.setBounds (20, 20, 220, 20);
        txt2.setBounds (20, 45, 220, 20);
        txt1.setText("Text Field #1");
        txt2.setText("Text Field #2");
        add(txt1);
        add(txt2);
        txt2.requestFocus();
    }
    private void display()
    {
        Object[] options = {this};
        JOptionPane pane = new JOptionPane();
        pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, txt2);
    }
    public static void main(String[] args)
    {
        new TextArea().display();
    }
}

You could let the txt2 component request focus once it is added by overriding addNotify . 一旦通过覆盖addNotify添加txt2组件请求,就可以让它成为焦点。 Like this: 像这样:

private JTextArea txt2 = new JTextArea() {
    public void addNotify() {
        super.addNotify();
        requestFocus();
    }
};

Here's a fully functional / tested version of your program: 这是一个功能齐全/经过测试的程序版本:

import java.awt.Dimension;
import javax.swing.*;
public class Test extends JPanel {
    private JTextArea txt1 = new JTextArea();
    private JTextArea txt2 = new JTextArea() {
        public void addNotify() {
            super.addNotify();
            requestFocus();
        }
    };

    public Test() {
        setLayout(null);
        setPreferredSize(new Dimension(200, 100));
        txt1.setBounds(20, 20, 220, 20);
        txt2.setBounds(20, 45, 220, 20);
        txt1.setText("Text Field #1");
        txt2.setText("Text Field #2");
        add(txt1);
        add(txt2);
    }

    private void display() {
        Object[] options = { this };
        JOptionPane pane = new JOptionPane();
        pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE, null, options, txt2);
    }

    public static void main(String[] args) {
        new Test().display();
    }
}

I gave you the answer in your last question (http://stackoverflow.com/questions/6475320/how-to-set-the-orientation-of-jtextarea-from-right-to-left-inside-joptionpane). 我在你的上一个问题中给出了答案(http://stackoverflow.com/questions/6475320/how-to-set-the-orientation-of-jtextarea-from-right-to-left-inside-joptionpane)。 The concept is the same. 这个概念是一样的。 Think about the solution given and understand how it works so you can apply it in different situations. 考虑给出的解决方案并了解它的工作原理,以便您可以在不同的情况下应用它。

If you still don't understand the suggestion then see DialogFocus for reusable code. 如果您仍然不理解该建议,请参阅DialogFocus以获取可重用的代码。

Why not use a JDialog or JFrame for this purpose? 为什么不为此目的使用JDialog或JFrame?

   public void display2() {
      JDialog dialog = new JDialog(null, "Title", ModalityType.APPLICATION_MODAL);
      dialog.getContentPane().add(this);
      dialog.pack();
      dialog.setLocationRelativeTo(null);
      txt2.requestFocusInWindow();
      dialog.setVisible(true);
   }

You could use a workaround proposed in JDK-5018574 bug report . 您可以使用JDK-5018574错误报告中提出的解决方法。

Instead of 代替

txt2.requestFocus();

use 采用

txt2.addHierarchyListener(e -> {
    if(e.getComponent().isShowing() && (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0)
        SwingUtilities.invokeLater(e.getComponent()::requestFocusInWindow);
});

I modified the solution to use Java 8 features. 我修改了解决方案以使用Java 8功能。 For older versions of Java, see the original workaround . 对于旧版本的Java,请参阅原始解决方法

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

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