Java – Check if JTextField is empty or not

For that you need to add change listener (a DocumentListener which reacts for change in the text) for your JTextField, and within actionPerformed(), you need to update the loginButton to enabled/disabled depending on the whether the JTextfield is empty or not. Below is what I found from this thread. yourJTextField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent … Read more

JTextField limiting character amount input and accepting numeric only

You’re on the right track, except you will want to use a DocumentFilter instead of implementing your own document. MDP’s Weblog has a number of excellent examples (including limiting the length and character type). Now to the your question, you could create cascading filter, where you could chain a series of filters together. This would … Read more

Rotating a JTextField vertically

Actually, yes, it can be done, but will require some additional libraries and access to some source code which has vanished off the net. Using JXLayer, it is possible to transform live components at runtime… public class JLayerTransform { public static void main(String[] args) { new JLayerTransform(); } public JLayerTransform() { EventQueue.invokeLater(new Runnable() { @Override … Read more

Detect enter press in JTextField

A JTextField was designed to use an ActionListener just like a JButton is. See the addActionListener() method of JTextField. For example: Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.out.println(“some action”); } }; JTextField textField = new JTextField(10); textField.addActionListener( action ); Now the event is fired when the Enter key is … Read more

java swing JTextField set PlaceHolder [duplicate]

Try this class: package playground; import java.awt.*; import javax.swing.*; import javax.swing.text.Document; @SuppressWarnings(“serial”) public class PlaceholderTextField extends JTextField { public static void main(final String[] args) { final PlaceholderTextField tf = new PlaceholderTextField(“”); tf.setColumns(20); tf.setPlaceholder(“All your base are belong to us!”); final Font f = tf.getFont(); tf.setFont(new Font(f.getName(), f.getStyle(), 30)); JOptionPane.showMessageDialog(null, tf); } private String placeholder; public … Read more

DocumentListener Java, How do I prevent empty string in JTextBox?

As @HFOE suggests, InputVerifier is the right choice, but verify() “should have no side effects.” Instead, invoke calcProduct() in shouldYieldFocus(). /** * @see http://stackoverflow.com/a/11818946/230513 */ private class MyInputVerifier extends InputVerifier { private JTextField field; private double value; public MyInputVerifier(JTextField field) { this.field = field; } @Override public boolean shouldYieldFocus(JComponent input) { if (verify(input)) { field.setText(String.valueOf(value)); … Read more

How to UnFocus a JTextField

A log-in would be best done in a modal dialog, but that introduces problems in that the method requestFocusInWindow() must be called after the component is visible, but that is blocked by the fact the dialog is modal! This example uses Rob Camick’s RequestFocusListener (as presented in Dialog Focus) to manage focus after the dialog … Read more

Can I have a textfield inside a label?

Use a ‘composite component’ by adding the required parts to a JPanel. E.G. import java.awt.FlowLayout; import javax.swing.*; class TimeBeforeClass { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JPanel gui = new JPanel(new FlowLayout(FlowLayout.LEFT, 3,3)); gui.add(new JLabel(“Open”)); gui.add(new JSpinner(new SpinnerNumberModel(15,0,20,1))); gui.add(new JLabel(“minutes before class”)); JOptionPane.showMessageDialog(null, gui); } }); } } … Read more