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

ε祈祈猫儿з 提交于 2019-11-28 09:04:39

问题


I want to set the focus on a specific JTextField which is passed to the JOptionPane as Object Message. Here is my code (I want the focus on txt2 but the focus always is on 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();
    }
}

回答1:


You could let the txt2 component request focus once it is added by overriding addNotify. 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();
    }
}



回答2:


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). 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.




回答3:


Why not use a JDialog or JFrame for this purpose?

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



回答4:


You could use a workaround proposed in JDK-5018574 bug report.

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. For older versions of Java, see the original workaround.



来源:https://stackoverflow.com/questions/6478394/how-to-set-focus-on-specific-jtextfield-inside-joptionpane-when-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!