How to select text over two different JTextField?

Let's 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 the text above the text1 and text2 fields so that I can copy them at the same time. But when I run the application, I can only select the text from the text field at a time. How can I do this so that I can select text for all the text fields that I can have in my program?

+4
source share
5 answers

How to make JButton to copy concatenation like JTextField 's?

For instance:

 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); } }); 
+3
source

I think getSelectedText ( ) can do this

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

Putting this as an answer:

You can explore the possibility of changing functionality in the JTextComponent copy() , paste() and cut() methods to change how it works for this component.

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

 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); } } 
+1
source

I do not think so. But you can add a hotkey listener programmatically to add 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

0
source

Perhaps you can use 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); 

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

0
source

Source: https://habr.com/ru/post/1381130/


All Articles