简体   繁体   中英

Java JApplet with/without validate()

I have this piece of code and read that validate can refer to laying out a container's subcomponents. "Layout-related changes, such as setting the bounds of a component, or adding a component to the container , invalidate the container automatically ." (source: javadoc).

However, I see no difference whatsoever between keeping validate() or removing it from this little piece of code. Can you show me a convincing example where you can see distinct behaviour in two cases (with or without validate) to prove a point? Any other comments/advice appreciated.

public class Sw1 
extends JApplet
{
    JLabel lbl;

    public void init() 
    {
        lbl = new JLabel ("a label");  
        JPanel pan = (JPanel) getContentPane ();
        pan.add(lbl);
        validate();
    }
}

Here is the program after I intended the push of a button to add a label. It renders an exception when I push the button:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Sw_test 
extends JApplet
implements ActionListener
{
    JLabel lbl;
    JButton bt ;
    JPanel pan ;
    JLabel l;

    public void init() 
    {
        lbl = new JLabel ("label 1");  
        bt = new JButton ("go ahead, press me");
        bt.addActionListener(this);

        JPanel pan = (JPanel) getContentPane ();
        pan.setLayout(new FlowLayout());
        pan.add(lbl);
        pan.add(bt);

        validate();
    }

    public void actionPerformed(ActionEvent ev)
    {
        l = new JLabel("new label");
               pan.add(l);
    }
}

You would need to call it if you add a component to a panel after it has been initialized and made visible.

Try adding a button to your applet, and on the click of the button, add a new label to the applet.

i will quote the API:

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

so as you see, it is important if you modify your layout, AFTER it has been initialized. That is the reason why you don´t see any difference

btw: here is your example :

public class TestFrame extends JFrame{

private JButton b = new JButton();

public TestFrame() {
    this.setLayout(new GridLayout(5,5));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(b);
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            TestFrame.this.add(new JLabel("whatever"));
            //try it with and without
            //validate();
        }
    });
    this.setSize(300, 300);
    this.setVisible(true);
}

public static void main(String[] args) {
    new TestFrame();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM