Reputation: 13
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
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?
Upvotes: 0
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