Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a small script for taking text from jtextfiled and save to a variable and print it in eclipse. But it is not getting print. Here is my code.-

Java
package not;

import javax.swing.*;
public class Not {
	public static void main(String args[]) {
		
		JTextField a = new JTextField();
        a.setBounds(20, 20, 20, 20);
		
		String b = a.getText();
		JFrame f = new JFrame();
        
f.setVisible(true);		
f.add(a);
System.out.println(b);
	}

	}


Please help me. This is a sample project which I used in a very big program.

What I have tried:

Please help. Please. I beg. This is part of a very big program.
Posted
Updated 13-Jan-21 2:27am

There is literally nothing to print, because you haven't set any text, and you haven't given the user the opportunity to enter any text.

Even if you moved the line that retrieves the text after the line that adds the text field to the frame, it still wouldn't work, because you haven't given the user any time to enter some text.
 
Share this answer
 
Comments
Mr.Corona 13-Jan-21 4:30am    
Sir You you mean I should put the TimeUnit.SECONDS.sleep(10);
Richard Deeming 13-Jan-21 4:33am    
No, that will cause the current thread to sleep for 10 milliseconds. Even if the user was fast enough to type in some text in that time, they wouldn't be able to, because the UI thread will be blocked.

You need to display the frame to the user, and wait for an event to fire once the user has finished entering the text.

Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)[^]
See my suggested solution to your previous question at Java program ideas..[^]. There really is no substitute, or short cut, to actually learning the language and framework. The tutorials contain working samples that demonstrate, and explain, how to use all these features.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900