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 4 of 4

Thread: JTextField

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    A sea of confusion.
    Posts
    3
    My Mood
    Cheeky
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JTextField

    I'm working with a JTextField, and JRadioButtons. I want to know how to make the GUI update every time I enter new text into the text field. Below is the main function in my event handling portion of my code. All the buttons update every time the are clicked, but when a new temp is entered the output will only update when a new button is clicked. Any help is welcome, thanks.

    public void itemStateChanged(ItemEvent UserEvent)
    		{
    			userInput = String.format("%s", Input.getText());
     
    			if(userInput.isEmpty() == true)
    			{
    				Output.setText(String.format("No input."));
    			}
     
    			else if(isNumeric(userInput) == false)
    			{
    				Output.setText(String.format("No input."));
    			}
     
    			else
    			{
    				temperature = Double.parseDouble(userInput);
    				System.out.print(userInput);
     
     
     
    				if(userInput.isEmpty() == false && isNumeric(userInput) == true)
    				{
    					if(Celsius1.isSelected())
    					{
     
    						if(Celsius2.isSelected())
    						{
    							Output.setText(String.format("%6.2f \u2103", temperature));
    						}
     
    						else if(Fahrenheit2.isSelected())
    						{
    							newTemp = (temperature * 1.8) + 32;
    							Output.setText(String.format("%6.2f \u2109", newTemp));
    						}
     
    						else if(Kelvin2.isSelected())
    						{
    							newTemp = temperature + 273.15;
    							Output.setText(String.format("%6.2f \u00B0" + "K", newTemp));
    						}
    					}
     
     
     
    					else if(Fahrenheit1.isSelected())
    					{
    						if(Celsius2.isSelected())
    						{
    							newTemp =  (temperature - 32)/ 1.8;
    							Output.setText(String.format("%6.2f \u2103", newTemp));
    						}
     
    						else if(Fahrenheit2.isSelected())
    						{
    							Output.setText(String.format("%6.2f \u2109", temperature));
    						}
     
    						else if(Kelvin2.isSelected())
    						{
    							newTemp = (temperature + 459.67)/ 1.8;
    							Output.setText(String.format("%6.2f \u00B0" + "K", newTemp));
    						}
    					}
     
    					else if(Kelvin1.isSelected())
    					{
    						if(Celsius2.isSelected())
    						{
    							newTemp = temperature - 273.15;
    							Output.setText(String.format("%6.2f \u2103", newTemp));
    						}
     
    						else if(Fahrenheit2.isSelected())
    						{
    							newTemp = (temperature * 1.8) - 459.67;
    							Output.setText(String.format("%6.2f \u2109", newTemp));
    						}
     
    						else if(Kelvin2.isSelected())
    						{
    							Output.setText(String.format("%6.2f \u00B0" + "K", temperature));
    						}
    					}
    					else if (keyIsPressed == false)
    					{
    						Output.setText(String.format("No input."));
    					}
    				}
    				else
    				{
    					Output.setText(String.format("No input."));
    				}
    			}	
    		}
    	}//End of Input UserEvent


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 299 Times in 282 Posts

    Default Re: JTextField

    Do some research into adding DocumentListeners to JTextFields. Here is an oracle tutorial about it: How to Write a Document Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    DR_W (November 25th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Location
    A sea of confusion.
    Posts
    3
    My Mood
    Cheeky
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JTextField

    Thanks, I need to turn this in tonight so I may not get the document listener implemented by tonight. I was hoping there might be a quick fix with the way I have it implemented now.

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 299 Times in 282 Posts

    Default Re: JTextField

    Not that I know of. I assume that is an ItemListener. In order for events to take place on a GUI object, you need to add listeners to them. As far as I know, JTextField does not allow you add ItemListeners to it.

    The fastest way you could implement your design would be:
    1. Set up the framework to implement the Document Listener (create the class and include the required methods, but don't add any code to the methods yet)
    2. Take all the code currently in your itemStateChanged() method and place it in its own method, which is within the visible scope of both the Document Listener class and wherever your ItemListener is (so both listeners can use the method)
    3. In your itemStateChanged() method of your ItemListener, just make it call the method you created from #2
    4. In the insertUpdate() and removeUpdate() methods of your Document Listener, also make it call the method you created from #2
    5. Add the DocumentListener to the JTextField by saying: textField.getDocument().addDocumentListener(new MyDocumentListener()); (like the tutorial link says)

    If you want a quick fix, that's the best I've got. It shouldn't take more than a few minutes to do. Really just a bit of copying and pasting.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    GregBrannon (November 25th, 2013)

Similar Threads

  1. [SOLVED] JTextfield
    By usherlad in forum Java Theory & Questions
    Replies: 4
    Last Post: August 22nd, 2012, 11:24 AM
  2. JTextField
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 7
    Last Post: June 20th, 2012, 10:54 AM
  3. Need help int JTextField
    By n00b in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2011, 09:57 AM
  4. how to delete a JTextField?
    By A4Andy in forum AWT / Java Swing
    Replies: 10
    Last Post: August 31st, 2011, 11:33 AM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM

Tags for this Thread