Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JTextField not visible in swing

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JTextField not visible in swing

    I want my program to display a text field. The text will be a random spanish word or phrase. Then I enter in the correct english translation. I want it to wait for 5 seconds after I enter text. Then set the text to a new word, wait for my translation, wait 5 seconds, etc etc until I close the program.

    import java.util.ArrayList;
    import java.util.Random;
    public class Library {
     
    	public ArrayList<String> spanish = new ArrayList<String>();		//make a spanish list
    	public ArrayList<String> english = new ArrayList<String>();		//make an english list
    	static Random random = new Random();		//make a random object
    	private int index;
     
    	public void setIndex(int index) {
    		this.index = index;
    	}
     
    	public int getIndex() {
    		return index;
    	}
     
    	//method to add all Strings to the lists
    	public void makeLists() {
    		spanish.add("Como te llamas?");	english.add("What is your name?");
    		spanish.add("Como se llama?");	english.add("What is his/her name?");
    		spanish.add("Por favor");		english.add("Please");
     
    	}
     
    	//method to grab a word or phrase from the spanish list
    	public String getOne() {
    		int x = random.nextInt(spanish.size());
    		setIndex(x);
    		return spanish.get(getIndex());
    	}	
     
    }
    import javax.swing.*;
    import java.io.*;
     
    import java.util.Random;
     
    public class Player extends Thread{
     
    	static JTextField text = new JTextField();		//make the text field
    	static Library library = new Library();			//make a library object
    	static Random random = new Random();			//make a random object
     
    	public static void main(String args[]) {
    		Player player = new Player();		//make a player
    		library.makeLists();				//make the lists
    		player.start();						//start
     
    	}
     
    	public void run() {
    		try {
    			text.setVisible(true);			//make the field visible
    			boolean loop = true;
    			while(loop) {					//loop forever
    				text.setText(library.getOne() + "\n");		//set the text to a random spanish phrase
    				InputStreamReader isr = new InputStreamReader(System.in);
    				BufferedReader br = new BufferedReader(isr);
    				if(((br.readLine().equalsIgnoreCase(library.english.get(library.getIndex())))))	
    				{
    					text.setText("Right!");		//if i enter in the correct english translation, i'm right
    				}
     
    				else text.setText("Wrong. The answer is " + library.english.get(library.getIndex()));		//if i'm wrong, display the right answer
     
    				Thread.sleep(5000);			//wait for 5 seconds
    			}	
    		}
    		catch(Exception ex) {
    			ex.printStackTrace();
    		}
     
    	}
     
    }

    Whenever I run the program...nothing happens. Well I'm assuming SOMETHING happens, but no text field comes up on my screen. My screen just remains the same and eventually I click the red terminate button (in Eclipse) when I give up on waiting. Can anyone tell me what's wrong or point me in some direction please?? Thanks.


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: JTextField not visible?

    It creates your JTextField but it dosnt have anything to show it on.
    You need a frame for your program first, for that you can use JFrame.
    I would suggest adding a JPanel to it first, but to make it simple you can skip it, and add your textfield right into JFrame.

    Add this constructor to your Player class
    public Player(){
    		//Creating frame for the program
    		JFrame frame = new JFrame();
    		//adding JTextField to the frame
    		frame.add(text);
    		//Make frame visible
    		frame.setVisible(true);
    		//Resize so everything fits in.
    		frame.pack();
    		}

  3. The Following User Says Thank You to Dalisra For This Useful Post:

    JavaPF (May 20th, 2009)

  4. #3
    Junior Member
    Join Date
    May 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTextField not visible?

    Okay, I added that into the constructor and it showed up. The problem I'm having now though is that when I type in a translation, and hit enter, nothing happens. Also, the JTextField is very smal.; only containing the initial text. Is there a way to make it bigger and/or set the cursor at the end of the String? If there is no way to do it...can a JTextArea do this?

  5. #4
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: JTextField not visible?

    Well let's see what is going wrong..

    First what I notice is that you are reading in lines from console, instead you should try to create new JTextField and use it for input.

    Also lets try adding everything to a JPanel first, so it will be showed properly.

    Take a look at my code, I added small changes, give me a shout if you don't understand it.

    import javax.swing.*;
    import java.util.Random;
     
    public class Player extends Thread {
     
    	//Text field that will show output
    	private JTextField text;
    	//Text field that will take input
    	private JTextField input;
     
    	static Library library = new Library();			//make a library object
    	static Random random = new Random();			//make a random object
     
     
    	public static void main(String args[]) {
    		library.makeLists();				//make the lists
    		Player player = new Player();		//make a player	
    		player.start();                                    //start
    	}
     
    	public Player(){
    		//Creating frame for the program
    		JFrame frame = new JFrame();
     
    		//Creating JPanel
    		JPanel contentPanel = new JPanel();
     
    		//creating text fields and labels ("initial text", length)
    		text = new JTextField("Output field", 30);
    		input = new JTextField("Input field", 30);
     
    		//adding textFields and label to the frame
    		contentPanel.add(text);
    		contentPanel.add(input);
     
    		//adding contentPanel to the frame
    		frame.add(contentPanel);
     
    		//Make frame visible
    		frame.setVisible(true);
     
    		//Resize so everything fits in.
    		frame.pack();
     
    		//Make program close properly once we press close button.
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
     
    		}
     
    	public void run() {
    		try {
    			text.setVisible(true);			//make the field visible
    			boolean loop = true;
    			while(loop) {					//loop forever
    				text.setText(library.getOne() + "\n");		//set the text to a random spanish phrase
    				//clear the string that was there.
    				input.setText("");
    				//give focus to the input field
    				input.grabFocus();
    				//give user 5 seconds to answer
    				Thread.sleep(5000);
    				if(((input.getText().equalsIgnoreCase(library.english.get(library.getIndex())))))	
    				{
    					text.setText("Right!");		//if i enter in the correct english translation, i'm right
    				}
     
    				else text.setText("Wrong. The answer is " + library.english.get(library.getIndex()));		//if i'm wrong, display the right answer
    				//Give user 5 seconds to see the response
    				Thread.sleep(5000);
    			}	
    		}
    		catch(Exception ex) {
    			ex.printStackTrace();
    		}
     
    	}
     
    }
    Last edited by Dalisra; May 21st, 2009 at 02:30 AM.

  6. #5
    Junior Member
    Join Date
    May 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTextField not visible?

    Awesome! With an added Thread.sleep(..) in the if/else statement the program is working exactly as I wanted it to. Thanks.