0

Is it possible to grab the value of a JFormatedTextField by clicking on a button?
Because that is what I have been racking my brain on today. Assume a run of the mill GUI consisting of two Panels, a JFormatedTextField and a button. What the panels show is not important - importantis the TextField. in this text field is obviuosly a value to be entered and the pressed button shall kick of the processing of the value. The following I made the TextField:

JFormattedTextField NutzerID=new JFormattedTextField(NumberFormat.getIntegerInstance());
NutzerID.setFocusLostBehavior(JFormattedTextField.COMMIT);
NutzerID.setEditable(true);

And receive the value inside the Actionlistener of the Button as follows(output intended as debug):

int Label=((Number) NutzerID.getValue()).intValue();
                System.out.println(Label);

Now, if I click on the button, I receive a NullpointerException inside the Actionlistener to my button.
Two things: The GUI is build using Intellijs GUI generator so I do not exactly know how the Cmponents are attached. Secondly, I am already working with a FormatedTextField but in a different Dialog. And that part works wonderful...
So I do not understand how one Button coupled FormattedTextField could work but the other not. Could it be because I did the Layout for the working FormattedTextField manually and let myself rely on the automatic layout of IntelliJ's GUI builder?

I have tried commiting (comitEdit)in the Button Actionlistener, add an Actionlistener to the TextField and adding an Focuslistener to the Textfield. Nothing worked.

Please tell me what I did wrong.

Quamatoc
  • 5
  • 4

1 Answers1

0

You can force a commit of the editor value, which will trigger a parser exception or you could just check if the return value is null or not first.

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JFormattedTextField textField = new JFormattedTextField(NumberFormat.getIntegerInstance());
            textField.setColumns(20);
            textField.setFocusLostBehavior(JFormattedTextField.COMMIT);

            add(textField, gbc);

            JButton button = new JButton("Make it so");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        textField.commitEdit();
                        int value = ((Number) textField.getValue()).intValue();
                        JOptionPane.showMessageDialog(TestPane.this, "You have entered " + value);
                    } catch (ParseException ex) {
                        JOptionPane.showMessageDialog(TestPane.this, "That's not a valid value, try again");
                    }
                }
            });

            add(button, gbc);
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366