Skip to main content

SwingUtilities.invokeAndWait

1 reply [Last post]
musiigedeo
Offline
Joined: 2007-07-23
Points: 0

I have modifeid the code below to use SwingUtilities.invokeAndWait to update the GUI whenever data is detected/read from the serial port. But It just does't work... what am I doing wrong guys? I am also open to other proposials

[CODE]

public class SimpleRead extends Thread implements SerialPortEventListener {
public SimpleRead(GUI parent){

//INITIALIZATION

}
public void serialEvent(final SerialPortEvent event) { //gets data from the serial port whenever there is an event

switch(event.getEventType()) {
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[1];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
data = new String(readBuffer);
ttdata = data;
publishData();
nothing(numBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try{
inputStream.close();
}catch (IOException e){

}

}
int nothing(int ui){
return ui;
}

public void publishData(){ //sets the data in the gui
final Runnable doHelloWorld = new Runnable() {
public void run() {
parent.setData(ttdata);

}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doHelloWorld);
}
catch (Exception e) {
e.printStackTrace();
}
//System.out.println("Finished on " + Thread.currentThread());
}
};
appThread.start();
System.out.print("test reached");
}

public void writeToport(){ //writes to the serial port
String sfile = browse.onBrowseSwing();//gets the method that returns the file name
if(sfile == null){
System.out.println("sfile er null");
}
File file = new File(sfile);//get the file to be sent... this is actually supposed to be from browse
if(file == null){
System.out.println("file er null");
}

try{
dataVector = contents_of_afile.getBytesFromFile(file);//the file is loaded in the dataVector
} catch(IOException e){

}
try{
int len = dataVector.length;

for(int i = 0; i < len; i++ ) {
byte sent_byte = dataVector[i];
if(dataVector[i] == 0x0D && dataVector[i+1] == 0x0A){
try{
Thread.sleep(190);
}
catch(InterruptedException e){
}
if(dataVector == null){
System.out.println("dataVector er null");
}
}else{
outputStream.write(sent_byte);
}

}

}catch(IOException e){

}
}
}
[\CODE]

Message was edited by: musiigedeo

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
musiigedeo
Offline
Joined: 2007-07-23
Points: 0

I have modified the question to concurrent threads (swingworker)