0

Brand new coder here. I've been searching around, but cannot seem to find topics on how to set multiple variables from a line of textboxes in jPanel to use later for algorithmic functions. In this case, I need 5 unique variables for later use. Any help setting up these variables would be highly appreciated. Here's the code I have for setting up text fields and gathering user input:

 import java.util.Scanner;
 import javax.swing.*; //Used to create JPanel

 public class SimpleMath {
     public static void main(String[] args) { //Setup text boxes
         JTextField aField = new JTextField(5);
         JTextField bField = new JTextField(5);
         JTextField cField = new JTextField(5);
         JTextField dField = new JTextField(5);
         JTextField eField = new JTextField(5);

         //Creating JPanel
         JPanel myPanel = new JPanel();
         myPanel.add(new JLabel("1:"));
         myPanel.add(aField);
         myPanel.add(Box.createHorizontalStrut(15)); //a spacer
         myPanel.add(new JLabel("2:"));
         myPanel.add(bField);
         myPanel.add(Box.createHorizontalStrut(15));
         myPanel.add(new JLabel("3:"));
         myPanel.add(cField);
         myPanel.add(Box.createHorizontalStrut(15));
         myPanel.add(new JLabel("4:"));
         myPanel.add(dField);
         myPanel.add(Box.createHorizontalStrut(15));
         myPanel.add(new JLabel("5:"));
         myPanel.add(eField);

         //Gathering data
         int result = JOptionPane.showConfirmDialog(null, myPanel, "Please enter 5 integers", JOptionPane.OK_CANCEL_OPTION);
         if (result == JOptionPane.OK_OPTION) {
             System.out.println("value 1: " + aField.getText());
             System.out.println("value 2: " + bField.getText());
             System.out.println("value 3: " + cField.getText());
             System.out.println("value 4: " + dField.getText());
             System.out.println("value 5: " + eField.getText());

             Scanner input = new Scanner(System.in);
         }
     }
 }
4
  • how to set multiple variables from a line of textboxes in jPanel What variables? What variable type? Can't you just call aField.getText() to get the String value? Commented Sep 7, 2016 at 15:52
  • Why are you creating a Scanner and then never using it? Commented Sep 7, 2016 at 15:53
  • 2
    "Brand new coder here." Then you've jumped into making GUIs (an advanced topic) too soon. Figure out basic OOP, control & data structures in command line apps. Commented Sep 7, 2016 at 15:54
  • Thank you for the swift input, everyone. I honestly had not realized I'd been diving in too deep too soon. I will certainly follow your advice @Andrew_Thompson Commented Sep 7, 2016 at 16:03

1 Answer 1

2

You're putting everything into the static main method, and trying to create an organic viable and complex Java program, and this simply won't work. You need to stop what you're doing and first learn Java basics including how to create and use instance fields and non-static methods. It's these fields that will be available for mutation in other parts of your program if created correctly. Get a decent book or tutorial and start learning first principles before doing GUI programming -- you won't regret doing this.

Sign up to request clarification or add additional context in comments.

2 Comments

While this is good advice that I agree with, it does not answer the question. I think a comment would be more appropriate.
@BrandonIbbotson: the solution is to use instance fields, but to do that, he should first throw this terrible code and start afresh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.