Results 1 to 6 of 6
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Updating a Jpanel

    I have some code that goes like this:

    Java Code:
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    
    public class FlashExecute {
    
    	
    	private static String [] Phrases = {"The dog chased", "the cat",".",
    			"The cat ran up a tree", ".", "The dog stopped chasing the cat", "."};
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		JFrame frame = new JFrame ("Flash ( Proof of Concept)");
    		
    		Flash panel = new Flash();
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(350, 100);
    		frame.setVisible(true);
    		
    		for (int i = 0; i < Phrases.length; i++){
    			frame.add(panel.show(Phrases[i]));
    			frame.repaint();
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    
    	}
    
    }
    and here is method show:
    Java Code:
    import javax.swing.JPanel;
    import java.awt.Font;
    import java.awt.Graphics;
    import javax.swing.JLabel;
    
    
    public class Flash extends JPanel {
    
    	private Font display_font = new Font ("Serif",Font.BOLD,12);
    	
    	/**
    	 * @param args
    	 */
    	
    	private JLabel label1;
    	private JPanel panel = new JPanel();
    	
    	public JPanel show (String text){
    		
    		label1 = new JLabel(text);
    		label1.setFont(display_font);
    		panel.add(label1);
    		
    		return panel;
    		
    	}
    	
    
    
    }
    Currently my result is semi working, the jframe has "the dog chased" but it does not change to "the cat" after 1 second. I'm stumped as to why repaint isn't refreshing the frame. So what am I doing wrong here?

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Updating a Jpanel

    JPanel have repaint method too. And you might want to call this.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Updating a Jpanel

    Don't create new panels all the time. Just create a method on the panel to change the text of the label and the label will repaint itself.

    Adding/removing components all the time is not a good idea. It can be done and if you really want to do it this way then you should check out: How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  4. #4
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Re: Updating a Jpanel

    thanks for the reply, so I changed my code like so:

    Java Code:
    for (int i = 0; i < Phrases.length; i++){
    			panel.updateText(Phrases[i]);
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    and

    Java Code:
    public class Flash extends JPanel {
    
    	private Font display_font = new Font ("Serif",Font.BOLD,36);
    	
    	/**
    	 * @param args
    	 */
    	
    	private JLabel label1 = new JLabel("test");
    	private JPanel panel = new JPanel();
    	
    	public Flash (){
    		label1.setFont(display_font);
    		panel.add(label1);
    	}
    	
    	
    	public void updateText (String text){
    		
    		label1 = new JLabel(text);
    		this.repaint();
    		
    	}
    	
    
    
    }
    now I'm not creating JPanels for no reason, but, now I don't see any text at all :(

    any ideas?

    Thanks in advance!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Updating a Jpanel

    Never call Thread.sleep on the main Swing thread, known as the event dispatch thread or EDT, not unless you want to put your entire application to sleep and become completely unresponsive. You're much better off using a Swing Timer. Please check the Oracle Swing tutorials to see how to use these.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Updating a Jpanel

    1) I suggested you change the text of the label, NOT create a new label.

    2) The Flash class "is a " JPanel so you just add the label directly to it. There is no need for you to create another JPanel within your Flash class.

Similar Threads

  1. JPanel Contents not updating after adding Components
    By mkjp2011 in forum AWT / Swing
    Replies: 8
    Last Post: 07-15-2011, 08:05 PM
  2. updating a gameBoard (JPanel filled with JLabels)
    By Bloitz in forum AWT / Swing
    Replies: 3
    Last Post: 07-07-2011, 06:31 PM
  3. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  4. Setting/Updating a tabbed JPanel's name
    By dave_gardner in forum AWT / Swing
    Replies: 3
    Last Post: 03-03-2010, 12:49 PM
  5. Replies: 3
    Last Post: 04-20-2009, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •