Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Funny business with JFrame, JPanel and JLabel

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Funny business with JFrame, JPanel and JLabel

    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
    Last edited by helloworld922; February 20th, 2010 at 03:53 PM. Reason: code tags use [], not {}


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down Re: Funny business with JFrame, JPanel and JLabel

    What a shame. Spam in the replies. Another internet resource corrupted.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Funny business with JFrame, JPanel and JLabel

    Quote Originally Posted by JeffC View Post
    What a shame. Spam in the replies. Another internet resource corrupted.
    Indeed. If I had the power I would

    Back to the problem at hand, something funny is going on with your threading. At least in the case of your init to simulate initialization (waiting two seconds), you are essentially freezing the gui by putting the EDT to sleep. This does not happen initially because you are initializing on the main thread, and sleeping the main thread. However afterwards you create a new frame from the EDT. To demonstrate, change you main method to:

    	public static void main (String[] args) { 
    		SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				new FunnyJFrameBusiness(); 
    			}
    		});
    		}
    And this first frame will behave similar to the second in your example. As a side note, you should place all calls to Swing updates from other threads onto the EDT using SwingUtilities (unless noted as thread safe in the documentation)

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Funny business with JFrame, JPanel and JLabel

    Dear copeg,

    Thanks for your genuine reply. This is somewhat over my head but I now have a direction to go in and something new to learn. Many thanks.

    Note that in the real init statement I created a File object and listed the directory. The sleep statement was only for demonstration, and also to distill the problem down to it's fundamentals.

    Regardless, I think you've put me on the right path.

    Thanks again,
    Jeff C.

Similar Threads

  1. Imitating a JFrame extended program with JPanel; help needed...
    By emigrant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 02:30 PM
  2. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  3. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM
  4. how to output using JLabel?
    By qaromi in forum AWT / Java Swing
    Replies: 1
    Last Post: August 30th, 2009, 02:09 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM