I have been pulling my hair out over a JTextField I am using as a status bar which just doesn't seem to respond when I set the text. Here is the method the constructor calls to make the status bar:
And here is part of my actionPerformed method which makes use of the status bar:
statusField.setText(" Connecting with database ...");
Data[] temp = database.search(queryFieldText);
statusField.setText("");
The database.search method is an invocation of a remote method, and while testing I have deliberately put a two second delay in the search() method to simulate a network delay. But my status panel remains stubbornly blank.
Does a separate thread look after the text setting and is it possible that before it gets a look in the next line is being executed?
I've checked out this forum and have seen advice on SwingUtilities.invokeAndWait(), but this seems very complicated and so far my experiments have ended in failure!
The problem, as I understand how swing works, is that repainting of components is only done when the event thread is free. Therefore, Swing doesn't get a chance to paint the status bar before the text is reset to "". Here is a solution that uses the paintImmediately() method.
publicvoid actionPerformed(ActionEvent ev) {
statusField.setText(" Connecting with database ...");
final queryFieldText = queryField.getText();
new SwingWorker() {
public Object construct() {
return database.search(queryFieldText);
}
publicvoid finished() {
Data[] temp = (Data[])getValue();
// show data on the GUI
statusField.setText("");
}
}).start();
}
just a thought. i think that you should try using a JLabel, instead of an uneditable JTextField. you might find that it looks better. it's probably better form anyway.