ChildProdigy
ChildProdigy

Reputation: 21

how to get int from JTextField with a JButton

I'm trying to get an int from my JTextField with the click of my JButton but I can't figure out how to do so. I'm trying to get the int and set it to a variable so I can use it in my program further down.

Here is the code(this is the whole method):

    JFrame presets = new JFrame("Presets");
    presets.setVisible(true);
    presets.setSize(500, 500);

    JPanel gui = new JPanel(new BorderLayout(2,2));

    JPanel labelFields = new JPanel(new BorderLayout(2,2));
    labelFields.setBorder(new TitledBorder("Presets"));

    JPanel labels = new JPanel(new GridLayout(0,1,1,1));

    JPanel fields = new JPanel(new GridLayout(0,1,1,1));

    labels.add(new JLabel("Place values on Cat.2/Cat.3 at"));
    JTextField f1 = new JTextField(10);
    String text = f1.getText();
    int first = Integer.parseInt(text);
    labels.add(new JLabel("and place follow up value at"));
    fields.add(new JTextField(10));


    labelFields.add(labels, BorderLayout.CENTER);
    labelFields.add(fields, BorderLayout.EAST);

    JPanel guiCenter = new JPanel(new BorderLayout(2,2));

    JPanel submit = new JPanel(new FlowLayout(FlowLayout.CENTER));

    submit.add( new JButton("Submit") );
    guiCenter.add( submit, BorderLayout.NORTH );

    gui.add(labelFields, BorderLayout.NORTH);
    gui.add(guiCenter, BorderLayout.CENTER);

    JOptionPane.showMessageDialog(null, gui);

Upvotes: 0

Views: 16442

Answers (3)

Paniz
Paniz

Reputation: 604

if you want to get the f1 text when the submit pressed, use this code:

   .
   .
   .
    JPanel submit = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Submit");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            int first = Integer.parseInt(f1.getText().trim());
        }
    });
    submit.add(button);
    guiCenter.add(submit, BorderLayout.NORTH);
      .
      .
      .

Upvotes: 2

newuser
newuser

Reputation: 8466

Try this,

      String getText()

      Returns the text contained in this TextComponent.

So, convert your String to Integer as:

try {
    int integerValue = Integer.parseInt(jTextField.getText());
}
catch(NumberFormatException ex)
{
    System.out.println("Exception : "+ex);
}

Upvotes: 3

Shahrzad
Shahrzad

Reputation: 1072

Probably you want the entered data as int. write it in the button action

JButton button = new JButton("Submit");
button.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent arg0) {
    try {
      int myInt=Integer.parseInt(jtextfield.getText());
      System.out.println("Integer is: "+myInt);
      //do some stuff
    }
    catch (NumberFormatException ex) {
      System.out.println("Not a number");
      //do you want
   }
  }
});

Remember Integer.parseInt throws NumberFormatException which must be caught. see java docs

Upvotes: 2

Related Questions