S
stan k.
Hi,
First of all I have checked some examples and implemented
multithreading.
What I am doing is i start the busy process, click on any other window
(within the windows environment) so that the java app window becomes
partially
hidden, then click again on the java app to bring it back to the
foreground -- what happens is the java app gui doesn't finish
repainting until the busy thread is done. This is even though I've
tried invokeLater() and SwingWorker()...
There is a project called 'spin' on sourceforge but it didn't include
an demo and I couldn't figure out how to use it...
I'm ready to give up here and just leave the gui app being unpainted
until the busy thread is done....
here's my sample code:
---------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.LineBorder;
public class MyApplication {
public static void main(String[] argv) {
final JFrame f = new JFrame("Test SwingWorker");
final SwingWorker worker = new SwingWorker() {
public Object construct() {
return new ExpensiveDialogComponent();
}
};
worker.start(); //new for SwingWorker 3
final SwingWorker worker2 = new SwingWorker() {
public Object construct() {
return new GuiRefresh();
}
public void finished(){
f.setVisible(false);
f.repaint();
f.validate();
f.setVisible(true);
}
};
/////////////////////////////////////////////////////////////////////
class jfrmSplashScreen extends JWindow {
public jfrmSplashScreen(String filename) {
JLabel jlbl = new JLabel(new ImageIcon(filename));
Color clrBlack = new Color(0, 0, 0);
jlbl.setBorder(new LineBorder(clrBlack, 1));
getContentPane().add(jlbl, BorderLayout.CENTER);
pack();
Dimension dim_scrSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim_lblSize = jlbl.getPreferredSize();
setLocation(dim_scrSize.width/2 - (dim_lblSize.width/2),
dim_scrSize.height/2 - (dim_lblSize.height/2));
}
public void mtd_Close() {// Close the screen
setVisible(false);
dispose();
}
}///////////////////////////////////////////////////////////
jfrmSplashScreen objSplash = new jfrmSplashScreen("pic.jpg");
objSplash.setVisible(true);
try {
Thread.sleep(2000); //sleep for 2 seconds
} catch (InterruptedException e) {
System.out.println("interrrupted exception");
}//try-catch
objSplash.mtd_Close();
worker2.start();
ActionListener showSwingWorkerDialog = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f,worker.get());
}//actionperformed
};//actionlistener
JButton b = new JButton("Click here to show component constructed by
SwingWorker");
b.addActionListener(showSwingWorkerDialog);
f.getContentPane().add(b);
f.pack();
f.show();
//The following is safe because adding a listener is always safe.
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
/////////////////////////////////////////////////////////////////////////////////////
class ExpensiveDialogComponent extends JLabel {
public ExpensiveDialogComponent() {
super("This is the world's most expensive label.");
System.out.println("ExpensiveDialogComponent"+1);
try {
System.out.println("ExpensiveDialogComponent"+2);
Thread.sleep(15000); //sleep for 15 seconds
System.out.println("ExpensiveDialogComponent"+3);
} catch (InterruptedException e) {
System.out.println("interrrupted exception");
}//try-catch
}//ExpensiveDialogComponent()
}//cls ExpensiveDialogComponent
class GuiRefresh extends JLabel {
public GuiRefresh() {
for (int i=0;i<3;i++){
System.out.println("GuiRefresh"+i);
this.repaint();
this.revalidate();
try {
Thread.sleep(2000); //sleep for 2 seconds
} catch (InterruptedException e) {
System.out.println("interrrupted exception");
}//try-catch
}//for()
}//GuiRefresh()
}//cls GuiRefresh
First of all I have checked some examples and implemented
multithreading.
What I am doing is i start the busy process, click on any other window
(within the windows environment) so that the java app window becomes
partially
hidden, then click again on the java app to bring it back to the
foreground -- what happens is the java app gui doesn't finish
repainting until the busy thread is done. This is even though I've
tried invokeLater() and SwingWorker()...
There is a project called 'spin' on sourceforge but it didn't include
an demo and I couldn't figure out how to use it...
I'm ready to give up here and just leave the gui app being unpainted
until the busy thread is done....
here's my sample code:
---------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.LineBorder;
public class MyApplication {
public static void main(String[] argv) {
final JFrame f = new JFrame("Test SwingWorker");
final SwingWorker worker = new SwingWorker() {
public Object construct() {
return new ExpensiveDialogComponent();
}
};
worker.start(); //new for SwingWorker 3
final SwingWorker worker2 = new SwingWorker() {
public Object construct() {
return new GuiRefresh();
}
public void finished(){
f.setVisible(false);
f.repaint();
f.validate();
f.setVisible(true);
}
};
/////////////////////////////////////////////////////////////////////
class jfrmSplashScreen extends JWindow {
public jfrmSplashScreen(String filename) {
JLabel jlbl = new JLabel(new ImageIcon(filename));
Color clrBlack = new Color(0, 0, 0);
jlbl.setBorder(new LineBorder(clrBlack, 1));
getContentPane().add(jlbl, BorderLayout.CENTER);
pack();
Dimension dim_scrSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim_lblSize = jlbl.getPreferredSize();
setLocation(dim_scrSize.width/2 - (dim_lblSize.width/2),
dim_scrSize.height/2 - (dim_lblSize.height/2));
}
public void mtd_Close() {// Close the screen
setVisible(false);
dispose();
}
}///////////////////////////////////////////////////////////
jfrmSplashScreen objSplash = new jfrmSplashScreen("pic.jpg");
objSplash.setVisible(true);
try {
Thread.sleep(2000); //sleep for 2 seconds
} catch (InterruptedException e) {
System.out.println("interrrupted exception");
}//try-catch
objSplash.mtd_Close();
worker2.start();
ActionListener showSwingWorkerDialog = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f,worker.get());
}//actionperformed
};//actionlistener
JButton b = new JButton("Click here to show component constructed by
SwingWorker");
b.addActionListener(showSwingWorkerDialog);
f.getContentPane().add(b);
f.pack();
f.show();
//The following is safe because adding a listener is always safe.
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
/////////////////////////////////////////////////////////////////////////////////////
class ExpensiveDialogComponent extends JLabel {
public ExpensiveDialogComponent() {
super("This is the world's most expensive label.");
System.out.println("ExpensiveDialogComponent"+1);
try {
System.out.println("ExpensiveDialogComponent"+2);
Thread.sleep(15000); //sleep for 15 seconds
System.out.println("ExpensiveDialogComponent"+3);
} catch (InterruptedException e) {
System.out.println("interrrupted exception");
}//try-catch
}//ExpensiveDialogComponent()
}//cls ExpensiveDialogComponent
class GuiRefresh extends JLabel {
public GuiRefresh() {
for (int i=0;i<3;i++){
System.out.println("GuiRefresh"+i);
this.repaint();
this.revalidate();
try {
Thread.sleep(2000); //sleep for 2 seconds
} catch (InterruptedException e) {
System.out.println("interrrupted exception");
}//try-catch
}//for()
}//GuiRefresh()
}//cls GuiRefresh