Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JFrame visibility problem

Status
Not open for further replies.

tom62

Programmer
Nov 12, 2002
152
DE
Hi,

I've developed an application that uses two classes. The first class displays a very simple GUI with a start button. When the use presses the button an instance of the second class is created that reads the contents of a database and writes this to a file. I would like the second class to show the progress of the database read, with the help of a simple dialog that contains a JProgressBar.

My problem however is that what ever I tried to do so far), I only managed to display an empty frame that is only repainted when my database read is finished.

Thanks in advance for your help,

Tom


Pseudocode example:

Class 1:

public static void main(String[] args)
{
//Create and set up the window.
frame = new JFrame("GUI for Class1");

//Create and set up the content pane.
JPanel newTest = new JPanel();
newTest.add(StartButton);
frame.setContentPane(newTest);

//Display the window.
frame.pack();
frame.setVisible(true);
}

// ActionListener for start button
public void actionPerformed(ActionEvent e)
{
frame.setVisible(false);
Class2 databaseReader = new Class2();
databaseReader.start();
frame.setVisible(true);
}

Class 2:

private JFrame frame = null;
private int currentRecord = 0;
public JProgressBar progressBar = null;

public Class2()
{
openDatabase();
int myTotalRecords = getTotalDatabaseRecords();

frame = new JFrame("Processing.....");
ProcessMonitor monitor = new ProcessMonitor();
progressBar.setMaximum(myTotalRecords);
progressBar.setValue(0);
monitor.setOpaque(true);
frame.setContentPane(monitor);
frame.pack();
frame.setVisible(true); // shows empty frame!!!
}

public start()
{
while (not databaseEOF)
{
readNextDatabaseRecord();
currentRecord++;
progressBar.setValue(currentRecord);
}
}

class ProcessMonitor extends JPanel
{
public ProcessMonitor()
{
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
add(progressBar);
}
}
 
Hi,

I've found the cause of the problem on the Sun developers webside (
Problem is when you are in actionPerformed() you are in the Swing UI Event thread which controls screen painting. NO REPAINTING can occur until you return from that. This means that I have to run my second Class process in another Thread to make it work

instead of

// ActionListener for start button
public void actionPerformed(ActionEvent e)
{
frame.setVisible(false);
Class2 databaseReader = new Class2();
databaseReader.start();
frame.setVisible(true);
}

I now use

public void actionPerformed(ActionEvent e)
{
frame.setVisible(false);
final SwingWorker worker = new SwingWorker()
{
public Object construct()
{
Class2 databaseReader = new Class2();
databaseReader.start();
return null;
}
};
worker.start(); //required for SwingWorker 3
frame.setVisible(true);
}

And that seems to work,

Tom.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top