0

just a quick question but I have a little Gu set up which has a JFormattedTextField. I want the text box for date entry and I want the textbox to show "yyyy-mm-dd".

When I use the following code it doesn't work:

<pre>dateFormatter = new MaskFormatter("####-##-##");
            dateFormatter.setPlaceholderCharacter('_');
            //dateFormatter.setPlaceholder("yyyy-mm-dd");

            startDate = new JFormattedTextField  (dateFormatter);
            dateFormatter.getDisplayValue(startDate, "123");
            //startDate.addActionListener(this);
            startDate.addPropertyChangeListener("value", this

How do I modify the code to get it working? I have tried a few different approaches and the above works if I just want to use the "_" as the PlaceholderCharacter but not when I try to set the placeholder as "yyyy-mm-yy".

Incidentally I'm using java 1.6.

Does anyone know how to resolve this issue?

tarquin
  • 137
  • 2
  • 13
  • 1
    Sounds more like you want "placeholder" support which is provided by the default API (SwingX has it), but I doubt you'd have much luck getting it to work with `JFormattedTextField` because it displays "text" all the time. A better solution might be to use some kind of date picker instead – MadProgrammer Jun 18 '18 at 03:29
  • Please note, that the format "yyyy-mm-dd" will present year-minute-day. Probably you need "yy-MM-dd" when you want to see year-month-day. – Sergiy Medvynskyy Jun 18 '18 at 06:00
  • `I want the textbox to show "yyyy-mm-dd".` - I've never tried it with a JFormattedTextField but maybe the [Text Prompt](https://tips4java.wordpress.com/2009/11/29/text-prompt/) class will do what you want. – camickr Jun 18 '18 at 19:43

1 Answers1

0

There is no placeholder in java , instead you can use mouse adapter interface, onclick set textfiled.setText("");

textArea.addMouseListener(new MouseAdapter() {
  public void mouseClicked(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON1)
 {
      textArea.setText("..");
    } 
    }

: https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html