All, I'm having some funny business creating instances of a Swing GUI app.
My objective is to provide my user with a message while a process is busy doing some work. The controlling program is a created instance of a class ( Call it class1 ). This instance creates an instance of another class ( class2 ) where the constructor takes some time to do its work. While that constructor is busy, I want a message like "Listing contents of directory . . . . ", where the dots are slowly added as time goes by to show the user there is no lockup.
In class1 I create a frame, panel and label. I add label to panel to frame. I pass the label to a threaded instance of a method that adds the message text to the label and slowly adds the dots. While that thread is running I instantiate class2, unthreaded. When that is finished, I interrupt the messaging thread and all is well.
It works for the first instance of class 1. When I create a second instance of class1, the label does not show.
I assume there is something fundamental that I am missing here. It does exactly what I want for the first instance of class1, but in following instances it does not show the label.
The threaded method does run because I can see it in the standard output. It's just as if the label isn't there.
NOW: My first instance of class1 is created by the static main method. When I create multiple instances of class 1 in the main method, it works. So I created another method in the main class, static, to start another instance of class 1. It fails as well.
I'm sure someone knowledgeable can point me in the right direction.
I reduced my code to the bare minimum to illustrate the problem. SHould run fine.
Thanks all,
Jeff C. New user.
public class main { public static void main (String[] args) { new FunnyJFrameBusiness(); } public static void startAnother() { new FunnyJFrameBusiness(); } } import java.lang.Thread; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.FlowLayout; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public class FunnyJFrameBusiness { public Init init; public JFrame j; public JPanel framePanel; public JLabel tempLabel; public ClickHandler click = new ClickHandler(); // CONSTRUCTOR **************************************************************** public FunnyJFrameBusiness( ) { // Frame prep: j = new JFrame("FunnyBizTest"); j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); j.setLocation(100, 100); j.setSize(500,100); framePanel = new JPanel(new FlowLayout()); j.setContentPane(framePanel); tempLabel = new JLabel(); framePanel.add(tempLabel); j.setVisible(true); Thread delayMsg = new Thread(new delayMessage( tempLabel, " Waiting for initializer " )); delayMsg.start(); init = new Init(); delayMsg.interrupt(); framePanel.remove(tempLabel); framePanel.repaint(); JButton buttonClone = new JButton("Clone"); buttonClone.setSize(new Dimension(10,10)); buttonClone.addActionListener(click); framePanel.add(buttonClone); j.setVisible(true); } // END FunnyBiz CONSTRUCTOR // CLICKHANDLER **************************************************************** public class ClickHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if ( e.getActionCommand().equals("Clone") ) { main.startAnother(); } } // END action performed method } // END of ClickHandler class public class delayMessage implements Runnable { public JLabel label; public String msg, s1; public long sleepTime = 250; public delayMessage( JLabel l, String s) { label = l; msg = s; } public void run() { System.out.println("In Threaded Labeler: "); try { while ( true ) { s1 = msg; for ( int i = 1; i <= 7; i++ ) { label.setText(s1); System.out.println(s1); Thread.sleep(sleepTime); s1 = s1+" ."; } } } catch ( InterruptedException e ) { System.out.println("EXCEPTION: "+e); System.out.println("Exiting threaded labeler"); } }// END run() }// END runnable class public class Init { // Simply waits two seconds to emulate an initialization. public Init() { try { Thread.sleep(2000); } catch (InterruptedException e) { System.out.println("Initialized"); } } // END Init constructor } // END init class }// END FunnyJFrameBusiness class