0

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);
ChildProdigy
  • 21
  • 1
  • 1
  • 2
  • 1
    More details please? It's like shooting an arrow at a target you barely know exists in a dark room. – nanofarad Jul 29 '13 at 01:59
  • Not clear which data? in what form? – Shahrzad Jul 29 '13 at 01:59
  • Start with [`JTextField#getText`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#getText%28%29) and then take a look at [How to use text fields](http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html) and [How to use formatted text fields](http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html) and [How to use spinners](http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html) – MadProgrammer Jul 29 '13 at 02:01
  • 1) Use a `JSpinner` in a `SpinnerNumberModel` as seen in [this answer](http://stackoverflow.com/q/17874717/418556). 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jul 29 '13 at 02:10
  • Noticing the latest edit: Someone might be able to help with the addition of a code snippet, but I still recommend an SSCCE. – Andrew Thompson Jul 29 '13 at 02:16
  • does this make things clear? – ChildProdigy Jul 29 '13 at 02:17
  • Did you even read the SSCCE document? – Andrew Thompson Jul 29 '13 at 02:27

3 Answers3

3

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);
}
newuser
  • 8,338
  • 2
  • 25
  • 33
  • how do I get my JButton to do this? Do I just use this code right underneath my JButton? – ChildProdigy Jul 29 '13 at 02:07
  • @ChildProdigy Take a look at [Creating a GUI with Swing](http://docs.oracle.com/javase/tutorial/uiswing/), [How to use buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) and [How to write an action listener](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – MadProgrammer Jul 29 '13 at 02:09
  • @MadProgrammer That's why i am waiting. – newuser Jul 29 '13 at 02:12
  • @MadProgrammer I have tried nothing has worked that's why I have come to you guys for help – ChildProdigy Jul 29 '13 at 02:21
  • 1
    @ChildProdigy That's fine, but be prepared for some intense learning ;) - Take the time to read through the linked tutorials and when you have a particular issue with those, let us know. We're happy to help, but you need to be prepared to work for it ;) – MadProgrammer Jul 29 '13 at 02:31
2

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);
      .
      .
      .
Paniz
  • 594
  • 6
  • 19
  • That works perfectly. If I was to do that for the second textfield I could use the same button listener, correct? – ChildProdigy Jul 29 '13 at 02:42
  • yes of course. and you must define first and f1 before the method you are using, other wise you will get the error which asks you to make them final. – Paniz Jul 29 '13 at 02:48
  • As a quick note, you might want to use `f1.getText().trim()` to remove any random whitespace that could be at the start or end of the input `String` – Erik Nguyen Jul 29 '13 at 03:56
2

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

Shahrzad
  • 1,062
  • 8
  • 26