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
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
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:
Quote:
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...
Re: A question about updates. (Program dosent update until refreshed)
Link not working for me. (4o4)
Thanks for the helping.
-Duster
Re: A question about updates. (Program dosent update until refreshed)
Quote:
Originally Posted by
DusteroftheCentury
Link not working for me. (4o4)
The quote describes the API. Remake of the link: Container (Java Platform SE 6)
Re: A question about updates. (Program dosent update until refreshed)
Thanks, works perfectly now :)
-Duster