yang
yang

Reputation: 13

set Value to Jformattedtextfield using MaskFormatter in java

I have a MaskFormatter and Jformattedtextfield .The code for that is

try {
    mk = new MaskFormatter("**-**-**-**-**-**-**-**-**-**-**-**-**-**-**");
    mk.setPlaceholderCharacter('_');
} catch(ParseException e) {
    e.printStackTrace();
}

format1=new JFormattedTextField(mk);
format1.setBounds(170, 80, 280,25);
format1.setFont(new Font("serif",Font.PLAIN,13));
commandpanel.add(format1);

I have added maskformatter to jformattedTextfield.On click of a button i am trying to set a value to format1 but nothing is getting displayed in format1 field.

This is the code for setting the value

// value may be 1E0234
String value=hashmap1.get("Command 1").substring(hashmap1.get("Command 1").lastIndexOf("-")+2, hashmap1.get("Command 1").length());

format1.setValue(value);

I also tried with format1.setText(value),in this case it is displaying in the field but without a maskformatter.

How do i display the value along with the mask.

Upvotes: 0

Views: 1878

Answers (2)

Javier
Javier

Reputation: 678

In my opinion you are trying to enter a value the field can't handle. You need to find the way to make your values fit the format you want. Good luck with that.

This works for me

public class FormattedTextFieldTest {
    public static void main(String[] args) {
        try {
            JFrame frame = new JFrame();
            JPanel contentPane = new JPanel(); // default flow layout
            frame.setContentPane(contentPane);
            MaskFormatter mk = new MaskFormatter("**-**-**-**-**-**-**-**-**-**-**-**-**-**-**");
            JFormattedTextField field = new JFormattedTextField(mk);
            contentPane.add(field);
            frame.pack();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setVisible(true);

            // Doesnt work
            //field.setValue("1E0234");
            //field.setValue("ZZ99ZZ99ZZ99ZZ99ZZ99ZZ99ZZ99");

            // Works for me
            field.setValue("1E-02-34");
            //field.setValue("ZZ-99-ZZ-99-ZZ-99-ZZ-99-ZZ-99-ZZ-99-ZZ-99");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

(Postdata) I notice there is already this answer (which got all the credit). Apparently both provide working code and mine is apparently posterior. Why should I keep it?

  • I was troubleshooting the question in the comments before any answer was posted. It was me who prompted the OP for the actual value, which was the actual problem . It was unfortunate that the other guy jumped over my shoulders and got all the credit while I was prompting why "ZZ-99 ..." was not accepted (the OP claimed it did not work, but IT SHOULD , as the example shows), and then the other guy posted just the minute I had my working example.
  • My answers has some distinct virtues that the accepted answer does not. First it clearly shows which values are accepted and which not, which does a better job at answering the question. And less important second it does not leave a zombie process when you close the frame, and third it is more clean from stuff unrelated to the problem.
  • If anyone at this point has any reason why I should remove my answer, please comment before downvoting.

Upvotes: 0

WillShackleford
WillShackleford

Reputation: 7018

This works for me:

public class MyJFrame extends JFrame {

MyJFrame() {
    try {
        MaskFormatter mk = null;
        try {
            mk = new MaskFormatter("**-**-**-**-**-**-**-**-**-**-**-**-**-**-**");
            mk.setPlaceholderCharacter('_');
        } catch (ParseException e) {
            e.printStackTrace();
        }
        JFormattedTextField format1 = new JFormattedTextField(mk);
        format1.setBounds(170, 80, 280, 25);
        format1.setFont(new Font("serif", Font.PLAIN, 13));
        String value = "ZZ-99-ZZ-99-ZZ-99-ZZ-99-ZZ-99-ZZ-99-ZZ-99-ZZ";//hashmap1.get("Command 1").substring(hashmap1.get("Command 1").lastIndexOf("-")+2, hashmap1.get("Command 1").length());
        String display = mk.valueToString(value);
        System.out.println("display = " + display);
        format1.setValue(value);
        add(format1);
        pack();
    } catch (ParseException ex) {
        Logger.getLogger(MyJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MyJFrame().setVisible(true);
        }
    });
}
}

If value is "zz-99" it displays

zz-99-__-__-__-__-__-__-__-__-__-__-__-__-__

Upvotes: 1

Related Questions