如何在两个不同的JTextField上选择文本?

时间:2011-11-14 14:09:00

标签: java swing jtextfield

假设我有这段代码:

  public static void main(final String [] args)
  {
    final JFrame frame = new JFrame("Display Keyword Panel");
    final JPanel panel = new JPanel(new BorderLayout());


    JTextField text1 = new JTextField("This is the first text field");
    text1.setBorder(null);
    text1.setOpaque(false);
    text1.setEditable(false);

    JTextField text2 = new JTextField("This is the second text field");
    text2.setBorder(null);
    text2.setOpaque(false);
    text2.setEditable(false);

    panel.add(text1, BorderLayout.NORTH);
    panel.add(text2, BorderLayout.SOUTH);

    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    frame.setLocation(450, 400);
    frame.pack();
    frame.setVisible(true);
  }

我想在text1和text2字段上选择文本,以便我可以同时复制它们。但是当我运行应用程序时,我一次只能从1个文本字段中选择文本。我该如何制作它以便我可以在我的程序中可能包含的所有文本字段中选择文本?

5 个答案:

答案 0 :(得分:3)

如何让JButton复制JTextField的连接?

例如:

btn.setActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        java.awt.datatransfer.StringSelection strsel = new java.awt.datatransfer.StringSelection(textField1.getText() + textField2.getText());
        java.awt.datatransfer.Clipboard clbrd = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
        clbrd.setContents(strsel, strsel);
    }
});

答案 1 :(得分:1)

我认为getSelectedText()可以做到这一点

enter image description here

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

public class CaretPositionTest {

    public CaretPositionTest() {
        final JTextField textField = new JTextField("0123456789");
        final JTextField textField1 = new JTextField("0123456789");
        textField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
            }

            @Override
            public void focusLost(FocusEvent e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        textField1.setText(textField.getSelectedText());
                    }
                });
            }
        });
        JPanel p = new JPanel();
        p.add(textField);
        p.add(textField1);
        JButton b;
        p.add(b = new JButton(new AbstractAction("0->5") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                textField.select(5, textField.getText().length());
                textField.setCaretPosition(5);
                textField.moveCaretPosition(textField.getText().length());
            }
        }));
        b.setFocusable(false);
        p.add(b = new JButton(new AbstractAction("5->0") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setCaretPosition(5);
                textField.moveCaretPosition(0);
            }
        }));
        b.setFocusable(false);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.add(p);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new CaretPositionTest();
            }
        });
    }
}

答案 2 :(得分:1)

将此作为答案发布:

您可以考虑更改JTextComponent s copy()paste()cut()方法中的功能,以改变它们对该组件的工作方式。

这种方法的缺点是,如果您改变copy()的工作方式 - 用户在尝试从单个JTextField复制选择时将永远不会得到他们期望的结果。解决此问题的方法是为组件实现新的KeyBinding。下面是一个示例,将“复制”按钮替换为“Ctrl-G”键绑定。

public class Test {
    public static JTextField text1 = new JTextField("This is the first text field");
    public static JTextField text2 = new JTextField("This is the second text field");

    public static void main(final String [] args)
    {
        final JFrame frame = new JFrame("Display Keyword Panel");
        final JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        text1.setBorder(null);
        text1.setOpaque(false);
        text1.setEditable(false);

        text2.setBorder(null);
        text2.setOpaque(false);
        text2.setEditable(false);

        text1.getInputMap().put(KeyStroke.getKeyStroke('G', KeyEvent.CTRL_DOWN_MASK), "copyAll");
        text1.getActionMap().put("copyAll", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                StringBuilder s = new StringBuilder();
                s.append(text1.getText()).append("\n").append(text2.getText());
                System.out.println(s.toString());
            }
        });

        panel.add(text1);
        panel.add(text2);

        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.setLocation(450, 400);
        frame.pack();
        frame.setVisible(true);
    }
}

答案 3 :(得分:0)

我不这么认为。但是你可以以编程方式添加一个热键监听器,它可以附加所有文本并将其添加到剪贴板。

http://www.javapractices.com/topic/TopicAction.do?Id=82 http://blogs.oracle.com/JavaFundamentals/entry/transferring_text_through_the_clipboard

答案 4 :(得分:0)

也许您可以使用JTable而不是JTextFields?

final JTable table = new JTable(2,1);
table.setValueAt("This is the first text field", 0, 0);
table.setValueAt("This is the second text field", 1, 0);

JTable允许您从多个单元格中进行选择和复制。