Creating Custom Components - Extending to JFormattedField

71 views Asked by At

I am currently working on an application and I have a certain requirement for my interface: A time input.

I need my controls to accept 12hrs to 24hrs input using JFormattedTextField and found this:

 MaskFormatter mask = new MaskFormatter("##:##");
 mask.setPlaceHolderCharacter('0');

Now I created a class that extends to a JFormattedTextField

 public class JayTimeInput extends JFormattedTextField{....

now I peeked inside JFormattedTextField's source and found something like this:

public JFormattedTextField(Object mask){...

my question is: How do I create my JayTimeInput class that automatically has a mask formatter? I tried declaring it in my constructor but I am not sure about this:

public JayTimeInput(){
     try{
          MaskFormatter mask = new MaskFormatter("##:##");
          mask.setPlaceHolderCharacter('0');
          new JFormattedTextField(mask);
     }catch(Exception e){e.printStackTrace()}
}

Ive seen examples on how to use MaskFormatter and the only way I found was by declaring it like this:

MaskFormatter mask = new MaskFormatter("##:##");
mask.setPlaceHolderCharacter('0');
JFormattedTextField jformat = new JFormattedTextField(mask);

Im not sure if my actionlistener was correctly done but I need this to work first.

anyone help me out? im still new in creating my own controls by extending existing swings.

UPDATE:

I was looking at the wrong way of customizing my JFormattedTextField. I Should've used FormatFactory. Answer code has been posted for anyone who needs it.

1

There are 1 answers

0
james castillo On
import java.awt.event.KeyEvent;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public final class JayTimeInput extends JFormattedTextField{
  


public JayTimeInput(){
    myFormat();
    addKeyListener(new java.awt.event.KeyAdapter() {
        @Override
        public void keyTyped(java.awt.event.KeyEvent evt) {
            verify(evt);
        }
    });
    
}

public void myFormat() {
    try {
        MaskFormatter format = new MaskFormatter("##:##");
        format.setPlaceholderCharacter('0');
        this.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(format));
    } catch (java.text.ParseException ex) {
        ex.printStackTrace();
    }
}

public String getTime(){
    return this.getText();
}

public void setTime(String x){
    this.setText(x);
}

public void resetTime(){
    this.setText("00:00");
}

public void setFocus(boolean f){
    this.setFocusable(f);
    this.setVerifyInputWhenFocusTarget(f);
}
public void verify(KeyEvent evt){
    try {
            int carret = this.getCaretPosition();
            char c = evt.getKeyChar();

            if(carret==0){
                int hour = Integer.parseInt(c+"");
                if(hour>1){
                    evt.consume();
                }
            }

            if(carret==1){
                int hour = Integer.parseInt(c+"");
                if(hour>2){
                    evt.consume();
                }
            }

            if(carret==3){
                int min = Integer.parseInt(c+"");
                if(min>5){
                    evt.consume();
                }
            }
    } catch (Exception e) {
        //do nothing. nothing to catch since its keyevent
    }
}
}