0

What I am trying to accomplish is setting a text field from another thread. my program has a button and when i click it a new thread start running and gets something from a socket afterwards I want to set three text fields according to the data but I can't do it. I've written a method in my mainwindow class to do the .settext() calls but i cant call the method because I don't even have a reference to my mainwindow instance. how can I properly make updating to my textboxes.

Also a minor question do I have to make my comboboxes final because the compiler complained like that.

Cannot refer to a non-final variable comboBox inside an inner class defined in a different method MainWindow.java

What causes this?

public void run()
{
    os.print("INIT {ClassName USARBot."+type.name +"} {Location "+firstPos.x+" , "+firstPos.y+" ,"+firstPos.z+" } {Name "+robotName+"}\r\n");
    while (true)
        try
        {
            String str=is.readLine();
            String[] substr1=null;
            Position p = new Position();
            Scanner s=null;
            if(str.contains("{Type GroundTruth}"))
                substr1=str.split(" ");
                s=new Scanner(substr1[8]);
                p.x=s.nextDouble();
                p.y=s.nextDouble();
                p.z=s.nextDouble();
                s.close();
                //Here I want to set Textfields in MainWindow
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
user1180619
  • 338
  • 1
  • 4
  • 11
  • if you are asking GUI toolkit i am using Swing – user1180619 May 25 '13 at 18:55
  • can you post some code ? – Arpit May 25 '13 at 18:56
  • java I also added the code – user1180619 May 25 '13 at 19:08
  • 6
    Swing components should only be accessed through the [Event Dispatch Thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). Depending on your program, you may want to use a [`SwingWorker`](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html) instead. A less elegant approach would be to call [`SwingUtilities.invokeLater`](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)) from within your other thread whenever you need to access a Swing component (usually not recommended). – Lone nebula May 25 '13 at 19:21
  • How can I use event dispatch thread to update gui can you give me an example. – user1180619 May 25 '13 at 19:28
  • This tutorial shows how to create a GUI on the Event Dispatch Thread: http://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html – Lone nebula May 25 '13 at 19:49
  • See also this [example](http://stackoverflow.com/a/3245805/230513). – trashgod May 25 '13 at 19:57
  • Don't make the JComboBoxes final. Instead make them class fields. – Hovercraft Full Of Eels May 25 '13 at 20:19
  • OK but do you know why windowbuilder made only the textfields class field – user1180619 May 25 '13 at 20:28

2 Answers2

1

The right answer it that from "Lone nebula" (cannot vote up your comment or put an comment here).

Here you find a good explanation of SwingWorker with an example: SwingWork example and explanation.
Its a bit more complicated i thought last week when i had to solve the same thing as you.

Gizzmo
  • 691
  • 8
  • 21
0

But the below code where you comment is:

SwingUtilities.invokeLater(new Runnable(){

    public void run(){
        someTextField1.setText("sometext1");
        someTextField2.setText("sometext2");
        someTextField3.setText("sometext3");
    }
});
jdb1015
  • 127
  • 6