Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Problem Taking Input Using JFrame

Hey all, first time poster.

Im going to be straight up this is to do with a college assignment but I have isolated the part of my program where I am struggling, which is with JFrame.

 import java.awt.Container;
 import java.awt.Dimension;  
 import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.*;           // JFrame is a swing class


  public class testJFrame {
static int propertyID;
static int daysNeeded;


public static void main(String[] args) {
    // TODO Auto-generated method stub


    JFrame frame= new JFrame("myfirstJFrame"); 
    Container content = frame.getContentPane(); 
    JLabel tag1 ;
    JLabel tag2 ;
    JTextField textArea;
    JTextField textArea2;
    JButton inputButton;


    frame.setSize(400,100); 

    tag1 = new JLabel("Property ID");
    tag2 = new JLabel("Days Needed");
    textArea = new JTextField(" ");
    textArea2 = new JTextField(" ");
    inputButton = new JButton("Send");


    tag1.setPreferredSize(new Dimension(80, 20));
    tag2.setPreferredSize(new Dimension(80, 20));
            textArea.setPreferredSize(new Dimension(20, 15));
            textArea2.setPreferredSize(new Dimension(25, 15));
            inputButton.setPreferredSize(new Dimension(75, 20));

            inputButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e){

               String propertyID = textArea.getText();
               String daysNeeded = textArea2.getText();
               testJFrame.propertyID = Integer.parseInt(propertyID);
               testJFrame.daysNeeded = Integer.parseInt(daysNeeded);



        }

    });        

    content.add(tag1);
    content.add(tag2);
    content.add(textArea);
    content.add(textArea2);
    content.add(inputButton);
    content.setLayout (new FlowLayout());
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    System.out.println(propertyID + daysNeeded);

}

 }

So JFrame pops up fine but then runs through and prints out 0 before any input can be captured, however if I directly in action listener print the variables after being inputted it works. That however is no use to me as I need access to these variables throughout the rest of the code and for the life of me do not know what to do here.

Any help much appreciated!

Hello and welcome in Treehouse community! There are no Swing courses here on Treehouse, and it might be difficult to get an answer. May I suggest you also post your question here at Code Ranch: https://coderanch.com/f/2/GUI

This forum has many very competent and helpful developpers who are likely to have an answer for you.

2 Answers

Essentially what you need here are member variables to hold the data and then the handler will store the input there. It's probably a good idea to spin-off the frame as a custom class so it can be instantiated with new instead of having all the code stuck in main()

Hi thanks for the comment, I am/will not be running this code in main and will be calling it but for the purpose of testing I just separated this out into a separate package to run it by itself.

To add a little more, when I call this as part of the full package in actionPreformed, I call a method in another class which takes the input from jFrame and sets the input to a variable in a different class which I should then be able to use how I want.

But as I said in this when I do that methods keep running while the jFrame is open and by the time I hit submit the program has finished and therefore returns an error because my input from jFrame at this stage is pointing to nothing.

In a GUI-based program, instead of establishing a flow from start to finish like a console-based program, you set up the UI and establish what should happen when certain events happen in the UI. So instead of calculating the results in main() or whatever methods are called from main() you set up a handler to calculate the result on submit.