Adding a custom Swing component in a JFrame

66 Views Asked by At

I've created a simple JTextField (this will have a mask after and I'll use this in multiple projects) and I'm trying to add in a simple JFrame, however, when I run the code, doesn't appear on the frame.

Here's the code for the new 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(196, 196, 196)
            .addComponent(textFieldCPF1, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(214, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(101, Short.MAX_VALUE)
            .addComponent(textFieldCPF1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(99, 99, 99))
    );

    pack();
}// </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                   

}

And here's the code for the JTextField:

        package org.japo.java.swing.samples;

import java.text.ParseException;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author ambro
 */
public class TextFieldCPF extends JFormattedTextField {
    
    JFormattedTextField CPF = new JFormattedTextField();
    


    public TextFieldCPF() throws ParseException {
        
        CPF = new JFormattedTextField(new MaskFormatter("#########"));
    }

    public JFormattedTextField getCPF() {
        return CPF;
    }

    public void setCPF(JFormattedTextField CPF) {
        this.CPF = CPF;
    }
    
}

And here there a screenshot of how it is on my IDE:

IDE

And this is when I run the code:

Running

Here is the Tutorial I was following: Tutorial1 and mostly this one

0

There are 0 best solutions below