• Post Reply Bookmark Topic Watch Topic
  • New Topic

Refreshing My GUI  RSS feed

 
Brendan Crisler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have created an app that builds a gui and takes in user entries, which then passes these entries to another class file that does all the number crunching.

What I want is for the number crunching to spit out a status on the gui instead of the normal System.out.prinln("stuff").

My gui code is in one .java file and gets kicked off in the main method. The number crunching is started when I mash the "Run" button I created in the GUI.

I've been trying to use threads to the best of my knowledge, but with no success. I know I'm missing something, but I'm just not sure what it is.

It sort of works, because I've seen the status messages get pushed to the GUI, but not until it's done running.

Help!!!

B
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't show us any code so it's hard to tell exactly what the problem is. It does sound like you are keeping the Swing event thread busy so it can't update your gui. Have a look at this FAQ entry for an example and a solution.
[ November 28, 2007: Message edited by: Joe Ess ]
 
Brendan Crisler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some snips of my code:

---- StkClientGui.java ----------

public class StkClientGui extends JFrame {

public static void main(String[] args) {

StkClientGui gui = new StkClientGui();
gui.buildGui();
}

public void buildGui() {

JFrame frame = new JFrame("STK Connect Client");
JButton runButton = new JButton("RUN IT!");
runButton.addActionListener(new RunButtonListener());
mainPanel.add();

frame.add(mainPanel);
frame.setSize(800,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
frame.setVisible(true);
}

public class RunButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
StkConnectClient client = new StkConnectClient();
client.setScenarioName(scenarioTextField.getText());
client.setStartTime(startTimeTextField.getText());
client.setEndTime(endTimeTextField.getText());
client.setEpoch(startTimeTextField.getText());
client.setDetectConstellation(detectConstTextField.getText());
client.setResponseConstellation(respConstTextField.getText());
client.setChain(chainTextField.getText());
client.setEphFileDir(ephTextField.getText() + "\\");
client.setSensorFileDir(sensorTextField.getText() + "\\");
client.setIntFileDir(intTextField.getText() + "\\");
client.setDbFile(dbTextField.getText());
client.go();
}
}


---- StkConnectClient.java -------

public class StkConnectClient {
public void go() {
try {
setupNetwork();
sendCommand("blah blah");

Class.forName("org.sqlite.JDBC");
Connection conn;

conn = DriverManager.getConnection("jdbc:sqlite:" + dbFile);

Statement stmt = conn.createStatement();
ResultSet shipRS = stmt.executeQuery("SELECT * FROM ships");

while (shipRS.next()) {
String shipName = shipRS.getString("field1");

sendCommand("New / */Ship " + shipName);

}
conn.close();

} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

System.out.println("No more data!");
}

private void setupNetwork() {
try {
stkCon = new StkCon("localhost", 5001);
stkCon.connect();
System.out.println("Network established");
} catch (NumberFormatException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}

public void sendCommand(String command) {
try {
stkCon.sendConCommand(command);
System.out.println(command);
} catch(Exception e) {
e.printStackTrace();
}
}

public void setEphFileDir(String ephFileDir) {
this.ephFileDir = ephFileDir;
}

public void setSensorFileDir(String sensorFileDir) {
this.sensorFileDir = sensorFileDir;
}

public void setIntFileDir(String intFileDir) {
this.intFileDir = intFileDir;
}

public void setDbFile(String dbFile) {
this.dbFile = dbFile;
}

public void setScenarioName(String scenario) {
this.scenario = scenario;
}

public void setStartTime(String startTime) {
this.startTime = startTime;
}

public void setEndTime(String endTime) {
this.endTime = endTime;
}

public void setEpoch(String epoch) {
this.epoch = epoch;
}

public void setDetectConstellation(String detectConst) {
this.constDetect = detectConst;
}

public void setResponseConstellation(String responseConst) {
this.constResponse = responseConst;
}

public void setChain(String chain) {
this.chain = chain;
}
}

-------------------

So........... what I want to do is when, the run button is smashed, i want it to do what it needs to do, but, I want the status (every System.out. [bold]) to print to some a TextArea on the GUI (not shown in the code).

Sorry for my ignorance, but Threads are a real weakness of mine!

Thanks again.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the FAQ entry I pointed to? It talks about a situation very similar to yours.
There's also examples in the Java Tutorial chapter on concurrency in Swing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!