I am trying to read a textfield. I am using the string=textfield.getText() notion, but all I get a is a blank string back. I am using netbeans and I am using the gui designer within it. I want to hit the "create grid" button and have it create a grid from the integers that the user inputs into the textfield. I have a listener on the button, but I do not know what listener (if any) to put on the the JTextField.

Here is some of the code. Any help would be greatly appreciated.
First the GUI code

//This is the action listener for the button
private void   CreateNewGridButtonActionPerformed (java.awt.event.ActionEvent evt)
 {                                                    
        CookieMonster cookie =new CookieMonster();//CookieMonster is my main class
        numberOfRows=numberOfRowsField.getText();
        numberOfColumns=numberOfColumnsField.getText();
        System.out.println(numberOfRows);
}                                                   
//this is my best guess of a listener for the JTextfield
    private void numberOfRowsFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                  
                    numberOfRows=numberOfRowsField.getText();
    }                                                 

it will always return an empty string.

Again any help is appreciated. My code in its entirety is attached in files

Recommended Answers

All 2 Replies

Hmm you are obtaining the text in correct manner. I hope there is no problem with the declaration of the string which is being used to get the text from the text box.

private void CreateNewGridButtonActionPerformed (java.awt.event.ActionEvent evt)
{ 
String numberOfRows;//Here
CookieMonster cookie =new CookieMonster();//CookieMonster is my main class
numberOfRows=numberOfRowsField.getText();
numberOfColumns=numberOfColumnsField.getText();
System.out.println(numberOfRows);
} 
//this is my best guess of a listener for the JTextfield
private void numberOfRowsFieldActionPerformed(java.awt.event.ActionEvent evt) { 
String numberOfRows;//And here
numberOfRows=numberOfRowsField.getText();
}

Try this :]

There is no listener to put on your text field at less for that question, the action performed return effectively the data typed in you text field so the error is not where you expect.

I dont now if it is necessary to hide those data membeers in CookieMonster:
numberOfRows and numberOfColumns are declared as int;
and in the class FirstWindow that is a super class of CookieMonster they are declared as String.
I dont now also if it is necesary to initialize the commandConsole variable witch is an instance of FirstWindow out of the main method.
where you redeclare "another" variable with the same name as a JFrame!!!!

Hope this help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.