Help on SwingUtilities

S

salmec

Hi All,
I'm a newbie in java programming
I've a problem:
I'm using Java(TM) 2 SDK, Standard Edition Version 1.4.2 because the
libraries comm.jar works good (in the latest version is non true).
In this version SwingWorker does not work very well.
I'm creating a Swing Interface that have to refresh every time that a
file was modified, I know that i've to use SwingUtilities.invokeLater()
but i dont know how and where i've to put it

This is a part of the code:

Principal (Main Class)
Class1 (Graphic Interface)
Class2 (Check if a File was modified)
Class3 (Find a String in that File)

//***PSEUDO CODE ***
public class Principal {
public static void main(String[] args) {

final Class1 mov = new Class1();
final Class2 FWT = new Class2();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mov.jTextField1.setText(FWT.misura_letta);
}
});
}
}

public class Class1 extends JPanel {

JTextField jTextField1;
Class1(){
jTextField1 = new javax.swing.JTextField();
JPanel Panel2 = new JPanel();
Panel2.add(jTextField1);
JFrame f = new JFrame("Graphics");
f.getContentPane().add(Panel2);
f.pack();
f.setVisible(true);
}
}



class Class2 implements Runnable {
Thread readThread;
public String misura_letta ="";

public Class2(){

readThread = new Thread(this);
readThread.start();

}
public void run() {
try {
Thread.sleep(20000);
TimerTask task = new FileWatcher( new
File("DaSerialeAll_prova.txt") ) {
protected void onChange( File file ) {
System.out.println( "File "+ file.getName() +" have change !"
);
Class3 CTDF = new Class3();
misura_letta = CTDF.str;
System.out.println("misura letta: " +misura_letta);
}
};

java.util.Timer timer = new java.util.Timer();
timer.schedule( task , new Date(), 10 );

} catch (InterruptedException e) {}
}
}


public class Class3 {
String str ="";
public Class3 () {
// (...) Read the File and find a string that i want to put in the
graphical interface
str = str.trim();

}
}
//*** end PSEUDO CODE ***


Thanks to all
Sal
 
K

Knute Johnson

Hi All,
I'm a newbie in java programming
I've a problem:
I'm using Java(TM) 2 SDK, Standard Edition Version 1.4.2 because the
libraries comm.jar works good (in the latest version is non true).

I'm using 1.6 and the Java Comm API works just fine. I have a dozen
computers in the field right now reading megabytes everyday from a
serial port.
In this version SwingWorker does not work very well.

Who told you these silly things?
I'm creating a Swing Interface that have to refresh every time that a
file was modified, I know that i've to use SwingUtilities.invokeLater()
but i dont know how and where i've to put it

This is a part of the code:

Principal (Main Class)
Class1 (Graphic Interface)
Class2 (Check if a File was modified)
Class3 (Find a String in that File)

//***PSEUDO CODE ***

This much pseudo code is a waste of everybody's time. Yours to write
and ours to read it. In any case best I can tell from the 'pseudo code'
you've got too many classes and you don't need
SwingUtilities.invokeLater(). JTextField.setText() is thread safe and
doesn't need to be run on the event dispatch thread.

Why don't you explain what you are really trying to do and provide a
short, compilable, and runnable piece of test code to demonstrate your
problem.
 
S

salmec

Hi Knute
Thanks for your interesting

Knute said:
I'm using 1.6 and the Java Comm API works just fine. I have a dozen
computers in the field right now reading megabytes everyday from a
serial port.


Who told you these silly things?

How I say before i'm a newbie in java programming, but i've tried to
use this example:
http://java.sun.com/developer/releases/javacomm/SimpleRead.java
to read from serial port but with the it didn't work, but with this
version is all ok.

Sorry for my Pseudo code, but i can't explain very well my problem...

Anyway i resolved it adding a repaint to the graphical interface.(Why I
cant try before?)

But i want to know more about threads and SwingUtilities, so if you or
anyone can give me an help on it, code examples are welcome! (a main
class with a GUI and another class that wants to refresh the GUI)

Thanks a lot
Sal
 
K

Knute Johnson

Hi Knute
Thanks for your interesting



How I say before i'm a newbie in java programming, but i've tried to
use this example:
http://java.sun.com/developer/releases/javacomm/SimpleRead.java
to read from serial port but with the it didn't work, but with this
version is all ok.

Sorry for my Pseudo code, but i can't explain very well my problem...

Anyway i resolved it adding a repaint to the graphical interface.(Why I
cant try before?)

I'm sure that that did not resolve your underlying problem.
But i want to know more about threads and SwingUtilities, so if you or
anyone can give me an help on it, code examples are welcome! (a main
class with a GUI and another class that wants to refresh the GUI)

Look at the Java Tutorial, specifically creating Swing GUIs.

http://java.sun.com/docs/books/tutorial/uiswing/index.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top