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