How to clear mask formatted Jformattedtextfield

151 views Asked by At

Good day all, there are 1 Jformattedtextfield (mask formatted) and 1 jcombobox on my program;

The program running well first but ;

If I select item ""; Jformattedtextfield doesn't return to first formatted value even used x.setvalue() or x.setvalue(" / / : "); and program freezes

i think i need to recall the format, some codes from my project as below, thank you for advance ``JFormattedTextField f1 = new JFormattedTextField(new SimpleDateFormat("dd-MM-yyyy HH:mm"));

    f1_1 = new JFormattedTextField();
    f1_1.setFont(new Font("Calibri", Font.PLAIN, 12));
    f1_1.setBounds(88, 97, 104, 30);
    panel.add(f1_1);

    try {
        MaskFormatter dateMask = new MaskFormatter(" ##/##/####  ##:##");
        dateMask.install(f1_1);   
    } 
    catch (ParseException ex) {
        Logger.getLogger(MaskFormatter.class.getName()).log(Level.SEVERE, null, ex);   
    }`

For the return to the first running condition how it should be ?

if (c1.getSelectedItem().toString() == "")

{

  f1_1.setValue("   /  /        :  ");


}   
2

There are 2 answers

0
Mohammad Hossin Samadian On

use setText method:

f1_1.setText("");
f1_1.setText(null);
0
awgold90 On

If you test if two strings have the same sequence of characters, you should use the equals() function of the class String instead of ==.

I would write:

if (c1.getSelectedItem().toString().equals(""))

instead of

if (c1.getSelectedItem().toString() == "").