【问题标题】:Force JTextField to select all of its contents when it appears强制 JTextField 在出现时选择其所有内容
【发布时间】:2012-12-16 19:29:50
【问题描述】:

我有一个JLabel,当您单击它时,它会被JTextField 替换。我需要 JTextField 在它出现时自动选择它的所有文本。

【问题讨论】:

标签: java swing jtextfield


【解决方案1】:

解决方案一:通过焦点事件进行。不是最好的解决方案。

public static void main(final String[] args) {
    // simple window preparation
    final JFrame f = new JFrame();
    f.setBounds(200, 200, 400, 400);
    f.setVisible(true);

    { // this sleep part shall simulate a user doing some stuff
        try { 
            Thread.sleep(2345);
        } catch (final InterruptedException ignore) {}
    }

    { // here's the interesting part for you, this is what you put inside your button listener or whatever
        final JTextField t = new JTextField("Hello World!");
        t.addFocusListener(new FocusListener() {
            @Override public void focusLost(final FocusEvent pE) {}
            @Override public void focusGained(final FocusEvent pE) {
                t.selectAll();
            }
        });
        f.add(t);
        f.validate();

        t.requestFocus();
    }
}

【讨论】:

  • 感谢您的回复,对我很有帮助
  • 使用Thread.sleep的目的是什么?这在 Swing 应用程序中通常被认为是不好的做法
  • 那只是为了让该字段在延迟后弹出,而不是像他那样做所有这些 click-button-hides-element-newelement-pop-sup-getsfocus-all-is-selected在原始问题中谈论。但我会编辑我的帖子,以便更清楚。
  • > 不是最好的解决方案 为什么?这个解决方案有什么问题,你能详细说明一下吗?
  • @MadProgrammer 哦,顺便说一句,现在我看到了,我有一个后续问题:当您说“Swing 中的 Thread.sleep() 是不好的做法”时,您实际上是指 Thread.sleep()在 EDT 内部,或者以任何其他方式阻止它,对吗?
【解决方案2】:

JTextField.selectAll() 是您所需要的。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SelectAll
{
    private int count = 0;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Select All");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JPanel contentPane = new JPanel();
        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                JTextField tfield = new JTextField(10);
                tfield.setText("" + (++count));             
                contentPane.add(tfield);
                tfield.requestFocusInWindow();
                tfield.selectAll();

                contentPane.revalidate();
                contentPane.repaint();
            }
        });

        contentPane.add(addButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new SelectAll().displayGUI();
            }
        });
    }
}

【讨论】:

  • 似乎您必须将 selectAll() 与 requestFocusInWindow() 结合使用才能获得所需的效果。如我的编辑所示。
【解决方案3】:

JTextField 类在其 API 中包含用于此目的的方法。

这会有所帮助:

http://forums.codeguru.com/showthread.php?308517-How-do-you-highlight-the-text-in-a-JTextfield

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2021-08-15
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多