Java JTextField with input hint

You could create your own: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.*; public class Main { public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); final JTextField textFieldA = new HintTextField(“A hint here”); final JTextField textFieldB = new HintTextField(“Another hint here”); frame.add(textFieldA, BorderLayout.NORTH); frame.add(textFieldB, BorderLayout.CENTER); … Read more

How to implement in Java ( JTextField class ) to allow entering only digits?

Add a DocumentFilter to the (Plain)Document used in the JTextField to avoid non-digits. PlainDocument doc = new PlainDocument(); doc.setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int off, String str, AttributeSet attr) throws BadLocationException { fb.insertString(off, str.replaceAll(“\\D++”, “”), attr); // remove non-digits } @Override public void replace(FilterBypass fb, int off, int len, String str, AttributeSet … Read more

JFormattedTextField : input time duration value

Here’s an example of using InputVerifier to accommodate multiple input formats. import java.awt.EventQueue; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.swing.Box; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; public class FormattedFields { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { //@Override … Read more

Limiting the number of characters in a JTextField

You can do something like this (taken from here): import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; class JTextFieldLimit extends PlainDocument { private int limit; JTextFieldLimit(int limit) { super(); this.limit = limit; } JTextFieldLimit(int limit, boolean upper) { super(); this.limit = limit; } public void insertString(int offset, String str, … Read more

How to allow introducing only digits in jTextField?

Here check this code snippet, that’s how you allow only digits in JTextField, by using DocumentFilter, as the most effeciive way : import java.awt.*; import javax.swing.*; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter; import javax.swing.text.DocumentFilter.FilterBypass; public class InputInteger { private JTextField tField; private MyDocumentFilter documentFilter; private void displayGUI() { JFrame frame = new JFrame(“Input … Read more

Is it possible to have an autocomplete using jtextfield and a Jlist?

1) you have to sort your array before use for better performance… 2) as I mentioned you have to take these two clasess 3) don’t forget set initial value for better and nicest work with these Components simple output from code import java.awt.*; import java.util.ArrayList; import javax.swing.*; public class AutoCompleteTextField { private JFrame frame; private … Read more

Value Change Listener to JTextField

Add a listener to the underlying Document, which is automatically created for you. // Listen for changes in the text textField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { warn(); } public void removeUpdate(DocumentEvent e) { warn(); } public void insertUpdate(DocumentEvent e) { warn(); } public void warn() { if (Integer.parseInt(textField.getText())<=0){ JOptionPane.showMessageDialog(null, “Error: Please enter number … Read more