Grammin
Grammin

Reputation: 12205

How can I select text over two different JTextField's?

Lets say I have this code:

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

I would like to select text over both text1 and text2 fields so that I can copy them both at the same time. But when I run the application I can only select the text from 1 text field at a time. How can I make it so that I can select text over all of the text fields that I might have in my program?

Upvotes: 4

Views: 1347

Answers (5)

Brandon Buck
Brandon Buck

Reputation: 7181

Posting this as an answer:

You can look into changing the functionality in JTextComponents copy(), paste(), and cut() methods to alter the way they work for that component.

The downside of this approach, is if you change the way copy() works - the user will never the results they expect when trying to copy a selection from a single JTextField. The way to solve this problem is implement a new KeyBinding for the component. Here is an example, replacing the "Copy" button with a a "Ctrl-G" KeyBinding.

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

Upvotes: 1

mKorbel
mKorbel

Reputation: 109815

I think that getSelectedText() can do that

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

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117589

How about making a JButton to copy the concatenation of both JTextField's?

For example:

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

Upvotes: 3

dogbane
dogbane

Reputation: 274532

Maybe you can use a JTable instead of 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);

A JTable will allow you to select and copy from multiple cells.

Upvotes: 0

John B
John B

Reputation: 32949

I don't think so. But you could add a hotkey listener programatically that could append all the text and add it to the clipboard.

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

Upvotes: 0

Related Questions