how to make a jtextfield having a fixed date format?
You can use JFormattedTextField with SimpleDateFormat DateFormat format = new SimpleDateFormat(“your_format”); JFormattedTextField dateTextField = new JFormattedTextField(format);
You can use JFormattedTextField with SimpleDateFormat DateFormat format = new SimpleDateFormat(“your_format”); JFormattedTextField dateTextField = new JFormattedTextField(format);
If you want your JTextField to be focused when your GUI shows up, you can use this: in = new JTextField(40); f.addWindowListener( new WindowAdapter() { public void windowOpened( WindowEvent e ){ in.requestFocus(); } }); Where f would be your JFrame and in is your JTextField.
From an answer by @JRL Use the underlying document: myTextField.getDocument().addDocumentListener();
You should use a DocumentFilter as per this tutorial. For example: import javax.swing.*; import javax.swing.text.*; public class JTextFieldLimit2 extends JPanel{ JTextField textfield = new JTextField(5); public JTextFieldLimit2() { PlainDocument doc = (PlainDocument) textfield.getDocument(); doc.setDocumentFilter(new TextLengthDocFilter(3)); add(textfield); } private class TextLengthDocFilter extends DocumentFilter { private int maxTextLength; public TextLengthDocFilter(int maxTextLength) { this.maxTextLength = maxTextLength; } private … Read more
For what it’s worth, I found it interesting to actually implement it, so I thought to share it with you (I am not looking for votes). It’s really non-invasive since all you have to do is call new GhostText(textField, “Please enter some text here…”);. The rest of the code is only to make it run. … Read more
Don’t use KeyListener on text components, there are a rafter of issues (not been notified, mutation exceptions, not been notified when the user pastes something into the field), instead, you should be using a DocumentFilter For example… import java.awt.EventQueue; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; … Read more
Use a DocumentFilter, here is an example I made, it will only accept alphabetic characters and white spaces: import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingUtilities; 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 Test { public Test() { initComponents(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void … Read more
I use to override the text fields paint method, until I ended up with more custom text fields then I really wanted… Then I found this prompt API which is simple to use and doesn’t require you to extend any components. It also has a nice “buddy” API This has now been included in the … Read more
I suggest use a DocumentFilter this will allow us to kill 2 birds with 1 stone. 1) we need to filter what is inputted to JTextFields to make sure our calculation wont go wrong 2) We need to update the total on the fly i.e as more digits are added/removed. Here is an example I … Read more
Use DocumentFilter: NumberOnlyFilter.java: import javax.swing.*; import javax.swing.text.*; import java.util.regex.*; public class NumberOnlyFilter extends DocumentFilter { public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { StringBuilder sb = new StringBuilder(); sb.append(fb.getDocument().getText(0, fb.getDocument().getLength())); sb.insert(offset, text); if(!containsOnlyNumbers(sb.toString())) return; fb.insertString(offset, text, attr); } public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) … Read more