0

Question ~ I have an AddButton ActionListener that I'm trying to empty a JFormattedTextField with while keeping it's Mask("##/##/####") and SimpleDateFormat. If anyone can help me with the technical part of this, that would be cool. Thanks! When button is clicked, text of the JFormattedTextField is emptied, but i get exception if I try to setValue(df) or makeMask()

private JPanel borderPanel, northPanel, westPanel;
    private JTextField name, location;
    private JFormattedTextField date;
    private static final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    private MaskFormatter mask = null;
    private JButton add, remove;


    private static final int FRAME_WIDTH = 850;
    private static final int FRAME_HEIGHT = 600;

    public FoodGUI() {
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setLocationRelativeTo(null);
        // Creates the BorderLayout Panel which contains every item nested within each individual panel
        createBorderPanel();
    }

    class AddButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            name.setText("");
            location.setText("");
            try {
            date.setValue(new Date());
            date.setText("");

            } catch(Exception e1) {
                System.out.println("Hello error");
            }
            // date.setValue(df);
            // makeMask(date);

        }
    }

My makeMask method and simple date formats are orginally applied in the private JPanel that the JFormattedTextBox is created in.

public void makeMask(JFormattedTextField e) {
        try {
            mask = new MaskFormatter("##/##/####");
            mask.setPlaceholderCharacter('-');
            mask.install(e);
        } catch (ParseException ex) {
            Logger.getLogger(FoodGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You know that `date.setText("");` is undoing the setValue call, right? – VGR Mar 17 '20 at 01:26
  • To be honest, I don't know what I'm doing with either that line or the line before it. I'm trying to seek an answer for how I can return (or permanently keep) my mask. – Ethan Klein Mar 17 '20 at 02:32
  • Could you avoid using `SimpleDateFormat` and `Date`? Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead look into `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 17 '20 at 07:25
  • I will give it a look. Thanks! – Ethan Klein Mar 18 '20 at 06:02

2 Answers2

0

This question was answered by using 1 format. I was formatting twice, with SimpleDateFormat and MaskFormatter, but I should have only used MaskFormatter. It works, normally now.

0

You should not call the install method of a formatter directly. Instead, you pass the formatter to the JFormattedTextField’s constructor:

date = new JFormattedTextField(mask);

By default, MaskFormatter deals with Strings. So you’ll want to do the work of formatting and parsing yourself:

date.setValue(df.format(new Date()));

// ...

String text = (String) date.getValue();
Date value = (text != null ? df.parse(text) : null);

It is possible to make the JFormattedTextField accept and return dates directly, using its inherited setValueClass method, but you would have to create your own date class with a String constructor and custom toString method. It’s probably not worth the effort.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • I fixed the textfield(mask) as you suggested. I was planning to do something like that to format the string to a date. Thanks for the help! – Ethan Klein Mar 18 '20 at 06:24