Results 1 to 4 of 4
  1. #1
    Walter is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Problem using Threads and Swing in Java

    Hello everyone,

    I'm facing a problem while developing a small "converter" program,

    In the GUI, I added a JTextField called text and a JButton called button, I wanted to run a thread when someone clicks on "button", basically using the value that the user has put in the "text" field...

    GUI
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    public class GUI extends JFrame implements ActionListener,WindowListener {
    
    private static final long serialVersionUID = 1L;
    private JButton button = new JButton("Go");
    private JTextField text = new JTextField("");
    
    ....
    
    
    }
    If the user clicks on the button, a thread is created..

    Convert class
    Java Code:
    public class Converter extends GUI implements Runnable
    {
    	private static final long serialVersionUID = 1L;
    	public String calculate(int a)
    	{
    		a += 10;
    	}
    	public void run()
    	{
    		if(text.getText().compareTo("") != 0)
    		{
    			text.setText(calculate(text.getText()));
    		}
    	}
    }
    I just need to run that thread so that it recovers the value typed in the text bar, and runs the method "calculate", and then sets the text of the text field to the result...

    The problem: text.getText() doesn't return what the user has actually typed in the text bar, but the default value (null)..

    So if I change the default value to "0", text.getText() will return "0", maybe because it's called externally? How can I fix that problem? I want to get the value that the user has entered..

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem using Threads and Swing in Java

    You're using inheritance incorrectly and inappropriately. Yes, if Converter extends the GUI class it will have access to all of the GUI fields and methods, but not the ones of the GUI object that you're actually interested in, the one that is displayed. The solution is to not have Converter extend GUI, but rather have it hold a GUI variable, say named gui, that holds a reference to the actual displayed GUI object, and then call the methods of interest on the gui variable. Be sure to pass in a valid reference, perhaps using a constructor parameter for Converter.

    Something like...

    Java Code:
    class Converter {
       private GUI gui;
    
       private Converter(GUI gui) {
          this.gui = gui;
       }
    
       //.... etc
       
    }
    Last edited by Fubarable; 11-25-2011 at 11:08 PM.

  3. #3
    Walter is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Problem using Threads and Swing in Java

    ^
    Oh my god thank you very much I got it, I added a setGUI() method to set the GUI variable in the Converter, and removed the inheritence... Now it works fantastic, thank you :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem using Threads and Swing in Java

    Quote Originally Posted by Walter View Post
    ^
    Oh my god thank you very much I got it, I added a setGUI() method to set the GUI variable in the Converter, and removed the inheritence...
    That's the other way to do it that I didn't mention -- good show!

    Now it works fantastic, thank you :)
    You're quite welcome!

Similar Threads

  1. Crazy threads problem with Java on WINE
    By kjkrum in forum Advanced Java
    Replies: 7
    Last Post: 08-23-2011, 11:53 PM
  2. Threads and Swing!
    By rameshraj in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-17-2010, 07:27 PM
  3. Java Swing Problem
    By mohitgupta1989 in forum Advanced Java
    Replies: 7
    Last Post: 06-23-2010, 07:46 AM
  4. Progress bar, threads and swing horror
    By taptapthat in forum AWT / Swing
    Replies: 0
    Last Post: 11-06-2009, 10:40 AM
  5. Java Swing GridBagLayout Problem
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2008, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •