I Created a JFormattedTextField and using as a javaBean for multiple java Project, however, i didn't manage to figure out how use this field with a mask, the mask just doesn't work
Here's my class:
package org.japo.java.swing.samples;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
/**
*
* @author ambro
*/
public class TextFieldCPF extends JFormattedTextField {
public TextFieldCPF() {
}
public TextFieldCPF (String string){
TextFieldCPF FormatoCPF = new TextFieldCPF("");
try {
MaskFormatter mask = new MaskFormatter("###");
mask.install(FormatoCPF);
} catch (ParseException ex) {
Logger.getLogger(TextFieldCPF.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I also tried to use a mask in this way:
try {
MaskFormatter mask = new MaskFormatter("###");
FormatoCPF = new JFormattedTextField(mask);
} catch (ParseException ex) {
Logger.getLogger(TextFieldCPF.class.getName()).log(Level.SEVERE, null, ex);
}
So, how to mask a JFormattedTextField?
To improve what my question is:
I have this simple frame:
public class TestFrame extends javax.swing.JFrame {
public TestFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
textFieldCPF1 = new org.japo.java.swing.samples.TextFieldCPF();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(117, 117, 117)
.addComponent(textFieldCPF1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(137, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(79, 79, 79)
.addComponent(textFieldCPF1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(90, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private org.japo.java.swing.samples.TextFieldCPF textFieldCPF1;
// End of variables declaration
}
What I want is to use the class TextFieldCPF in multiple java projects, not only in TestFrame
TextFieldCPF will have more than just the mask, afterwards i'll try to set other features and this field will be use as the pattern for other projects.
Your constructor
public TextFieldCPF (String string){is wrong. Don't create a new instance of theTextFieldCPFwithin the constructor, instead make use of the instance of the class which is been created (iethis). Also, you shouldn't be consuming the exception, the caller needs to know when this fails.Having said that, the constructor is ambiguous, what is
string? Is it the text of field? Some kind of prompt? A tooltip?Instead, you should make the parameter meaningful, in this case, ask for an instance of
MaskFormatter, for example...or, alternatively, provide a factory method...
then code becomes more self documenting.
Talking of factories, since you're not actually adding any new functionality to the class, you should prefer composition over inheritance, instead, create and actual an factory class, for example...
That's because you've not acutally implemented the
public TextFieldCPF(MaskFormatter mask) {constructor - the error message is telling you every thing you need to know, for example...Works fine for me...
And just to be sure, so does the factory...
My point remains the same, prefer composition over inheritance, you're not actually adding new functionality to the class, you're just "configuring" it, which might make a builder pattern more useful, for example...