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 5 of 5

Thread: A question about updates. (Program dosent update until refreshed)

  1. #1
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default A question about updates. (Program dosent update until refreshed)

    Hey guys I have a simple question. Do I have to manually tell a panel to update or something? And if so how?

    Right now using this code
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class AdvTimeGame extends JFrame implements ActionListener
    {
    	public static void main(String[] args)
    	{
    	new AdvTimeGame();
    	}
     
    	private JButton b1;
    	private JTextField field1;
    	private JLabel label1;
    	private JLabel label2;
     
     
    	JPanel panel1 = new JPanel();
     
     
     
    	ImageIcon pic1 = new ImageIcon(AdvTimeGame.class.getResource("FinnHead.png"));
     
    	public AdvTimeGame()
    	{
     
     
     
     
    		label1 = new JLabel("Choose your name:");
    		label2 = new JLabel();
    		field1 = new JTextField(15);
    		field1.setToolTipText("Choose your name");
     
    		b1 = new JButton("Begin Game");
     
     
    		field1.addActionListener(this);
    		b1.addActionListener(this);
     
     
    		panel1.add(new JLabel(pic1));
    		panel1.add(label1);
    		panel1.add(field1);
    		panel1.add(b1);
    		panel1.add(label2);
     
    		this.add(panel1);
    		this.setTitle("AdventureTime");
    		this.setVisible(true);
    		this.setSize(300,400);
    		this.setIconImage(Toolkit.getDefaultToolkit().getImage("FinnHead.png"));
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getSource() == b1)
    		{
     
    		}		
    	addMouseListener(new MouseAdapter()
    	{
    		public void mouseClicked(MouseEvent e)
    		{
    			panel1.add(new JLabel(pic1));
    			}
    		});
    	}	
    }
    (Note this is just to test concepts)

    It will do what I want, but the frame dosent add the new pictures until your minimize and then reopen the program.

    Please Help.

    -Duster
    Win is: That epic moment when you make a reference to coding in Class, and everyone stares at you like you are crazy.


  2. #2
    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: A question about updates. (Program dosent update until refreshed)

    See the API for container (which a JPanel extends): Container (Java Platform SE 6)

    Quoted from the add method:

    Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.
    There are alternatives to require calling validate or revalidate, for instance add the JLabel upon construction and call setIcon, use an appropriate LayoutManager that can dynamically change the component it dislays (for instance CardLayout), etc...
    Last edited by copeg; January 31st, 2012 at 11:49 PM.

  3. #3
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: A question about updates. (Program dosent update until refreshed)

    Link not working for me. (4o4)

    Thanks for the helping.

    -Duster
    Last edited by DusteroftheCentury; January 31st, 2012 at 10:54 PM.
    Win is: That epic moment when you make a reference to coding in Class, and everyone stares at you like you are crazy.

  4. #4
    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: A question about updates. (Program dosent update until refreshed)

    Quote Originally Posted by DusteroftheCentury View Post
    Link not working for me. (4o4)
    The quote describes the API. Remake of the link: Container (Java Platform SE 6)

  5. The Following User Says Thank You to copeg For This Useful Post:

    DusteroftheCentury (February 1st, 2012)

  6. #5
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: A question about updates. (Program dosent update until refreshed)

    Thanks, works perfectly now

    -Duster
    Win is: That epic moment when you make a reference to coding in Class, and everyone stares at you like you are crazy.

Similar Threads

  1. Variable updates after loop
    By tecno40 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 9th, 2011, 05:34 AM
  2. Question About Program
    By metaleddie13 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 14th, 2011, 06:19 PM
  3. Question About A Program
    By torres9 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 15th, 2011, 08:56 PM
  4. [SOLVED] Trojans with last few updates of NetBeans
    By Melawe in forum Java IDEs
    Replies: 11
    Last Post: May 22nd, 2010, 08:33 AM
  5. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM