My action button reads a JTextField value and updates that same field with only the digits included in the original value. It also copies the digits to clipboard automatically and makes a JLabel visible, letting the user know it's copied. This label goes invisible again as soon as the user types again in the text field.
Example: user inputs "abc123cde456" and clicks the action button. The output is "123456". A label is shown, telling the value was copied.
I've made this button default with getRootPane().setDefaultButton(button) so the user can trigger it using Enter key. The problem is, when using the key instead of mouse clicking, the output and copy to clipboard work, but the JLabel does not get visible.
I noticed the Enter key works properly when I set the focus off the JTextField before hitting the key, but the only way to do that in my layout is to hold the mouse click on top of the button and "drag it" outside, so the focus goes to the button instead of JTextField, like this:
passing focus to button before hitting Enter key
Code below - by the way, I'm working on NetBeans IDE:
JFrame class
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
public class FrameZ extends javax.swing.JFrame {
public FrameZ() {
initComponents();
labelCopied.setVisible(false);
inputTxt.addActionListener(actionButton.getActionListeners()[0]);
getRootPane().setDefaultButton(actionButton);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
inputTxt = new javax.swing.JTextField();
actionButton = new javax.swing.JButton();
labelCopied = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
inputTxt.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
inputTxtKeyTyped(evt);
}
});
actionButton.setText("Only numbers");
actionButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionButtonActionPerformed(evt);
}
});
labelCopied.setText("Copied!");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(labelCopied)
.addGap(72, 72, 72))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(inputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
.addComponent(actionButton)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(actionButton)
.addComponent(inputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelCopied)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void actionButtonActionPerformed(java.awt.event.ActionEvent evt) {
String inputValue = inputTxt.getText();
StringBuilder digitsOnly = new StringBuilder();
for (int i = 0; i < inputValue.length(); i++) {
char c = inputValue.charAt(i);
if (Character.isDigit(c)) {
digitsOnly.append(c);
}
}
inputTxt.setText(digitsOnly.toString());
//copying to clipboard:
StringSelection strSelect = new StringSelection(inputTxt.getText());
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(strSelect, null);
labelCopied.setVisible(true);
}
private void inputTxtKeyTyped(java.awt.event.KeyEvent evt) {
labelCopied.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameZ().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton actionButton;
private javax.swing.JTextField inputTxt;
private javax.swing.JLabel labelCopied;
}
Main class
public class Main {
public static void main(String args[]){
FrameZ frm = new FrameZ();
frm.setVisible(true);
frm.setResizable(false);
frm.setLocationRelativeTo(null);
}
}