Search code examples
javaswingjformattedtextfieldmaskformatter

Receiving text inputs via a JFormattedTextField


I am having problems taking text inputs from the user via a JFormattedTextField. I've tried passing a sample value, for example 172.16.10.0 into the text field and would like to have it transferred into the string variable strIP.

Currently I am using the following method to extract the value from the text field:

strIP = txtIP.getText();

However, after calling the aforementioned method, I keep getting an error. The full code for my example is as follows:

import java.awt.*;
import java.awt.event.*;
import javax.swing.text.MaskFormatter;
import javax.swing.*;
import java.text.ParseException;
import java.util.regex.Pattern;
import java.math.BigInteger;
import java.util.Arrays;

public class update extends JPanel implements ActionListener
{
    private JButton cmdCalculate;
    JFormattedTextField txtIP;
    JFormattedTextField txtSub;

    private JLabel  lblCalculate, lblIP, lblSub, lblNetwork, lblHost;
    private static String IPaddress = "",Subenetaddress= "", Networkaddress= "", Lastaddress= "";   
    private JPanel panAnswerArea, panNorthArea, panBase, panAddressGrid, panIP, panSub, panButton;

    public update() 
    {       
        super.setLayout(new BorderLayout());    
        cmdCalculate = new JButton("Calculate");
        cmdCalculate.addActionListener(this);

        lblIP = new JLabel("IP Address: ");
        lblSub = new JLabel("Subnet Mask: ");
        panIP = new JPanel();
        panSub = new JPanel();
        panIP.add(lblIP);
        panSub.add(lblSub);
        MaskFormatter mf = null;

        try {
           MaskFormatter mf = new MaskFormatter("***.***.***.***");
           JFormattedTextField txtIP = new JFormattedTextField(mf);
           panIP.add(txtIP);
           txtIP.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
           txtIP.setPreferredSize(new Dimension(150, 24)); 
        } catch (ParseException pe) {
            e.printStackTrace();
        }       

        lblCalculate = new JLabel("Calculate Network and Host parts: ", JLabel.LEFT);
        lblNetwork = new JLabel("Network address: ", JLabel.LEFT);
        lblHost = new JLabel("Host address: ", JLabel.LEFT);

        lblNetwork.setText("Network address: ");
        lblHost.setText("Host address: ");

        panAnswerArea = new JPanel(new BorderLayout()); 
        panNorthArea = new JPanel(new BorderLayout()); 
        panAddressGrid = new JPanel(new GridLayout(4,2)); 
        panButton = new JPanel();

        panAnswerArea = new JPanel(new BorderLayout()); 
        this.add(panAnswerArea, BorderLayout.SOUTH);
        panAnswerArea.add(lblNetwork, BorderLayout.NORTH);
        panAnswerArea.add(lblHost, BorderLayout.SOUTH);

        panNorthArea = new JPanel(new BorderLayout());
        this.add(panNorthArea, BorderLayout.NORTH);
        panNorthArea.add(panAddressGrid, BorderLayout.SOUTH);
        panAddressGrid.add(panIP);
        panAddressGrid.add(panSub);
        panAddressGrid.add(panButton);
        panButton.add(lblCalculate);
        panButton.add(cmdCalculate);
    }

    public void actionPerformed (ActionEvent e)
    {
        String strIP= "";
        String strSub= "";
        String IPaddress = "",Subenetaddress= "", Networkaddress= "", Lastaddress= "", inversbits= "",inversesubnetmask= "";
        strIP = txtIP.getText();// Problem is here 
    }

    public static void main(String[] args)
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Subnet Calculators"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        JComponent paneMain = new update();
        paneMain.setOpaque(true);
        paneMain.setPreferredSize(new Dimension(500, 300));
        frame.setContentPane(paneMain);
        frame.pack();
        frame.setVisible(true);
    }
}

Any feedback & suggestions greatly appreciated.


Solution

  • When I ran your code, the value for txtIP was null.

    In your constructor, change the following snippet:

    try {
        MaskFormatter mf = new MaskFormatter("***.***.***.***");
        JFormattedTextField txtIP = new JFormattedTextField(mf);
        panIP.add(txtIP);
        txtIP.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        txtIP.setPreferredSize( new Dimension( 150, 24 )); 
    } catch (ParseException pe) {
        e.printStackTrace();
    }
    

    to something like the following instead:

    try {
            mf = new MaskFormatter("***.***.***.***");
            // You have txtIP declared as a class variable, and you (I believe the term is called shadowed) 
            // hid the class variable with your original code and txtIP is treated as a local variable in your constructor
            // so the class field was never initialized
            txtIP = new JFormattedTextField(mf);
            panIP.add(txtIP);
            txtIP.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
            txtIP.setPreferredSize(new Dimension( 150, 24)); 
        } catch (ParseException pe) {
            e.printStackTrace();
        }