🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #13066

Re: Panel won't refresh on demand

#13066
From: Alain M. Gaudrault
Date: Thu, 01 Oct 1998 00:00
120 lines
4448 bytes
Rod Magnuson wrote:
> 
> Alain,
> 
> When adding or removing Components from a Container you must validate the container.

I'd tried a number of the validate() and revalidate() methods, and in
all cases, the label gets refreshed the first time I click on each
button, but not after any subsequent clicks.  In the included code
below, I even try to make sure to only call validate() the first time,
and then to call revalidate() after that, but it still doesn't work. 
Any ideas?

==========
import java.awt.*;
import com.sun.java.swing.*;
import java.awt.event.*;

public class NoRepaint extends JPanel {
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JScrollPane jScrollPane1 = new JScrollPane();
    GridLayout gridLayout1 = new GridLayout();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();


    public NoRepaint() {
        try  {
            jbInit();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        jPanel1.setLayout(gridLayout1);
        this.setLayout(borderLayout1);
        gridLayout1.setRows(2);
        gridLayout1.setColumns(1);
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }
        });
        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton2_actionPerformed(e);
            }
        });
        jLabel1.setText("jLabel1");
        jLabel2.setText("jLabel2");
        this.add(jPanel1, BorderLayout.WEST);
        jPanel1.add(jButton1, null);
        jPanel1.add(jButton2, null);
        this.add(jScrollPane1, BorderLayout.CENTER);
    }

    public static boolean done1 = false;
    void jButton1_actionPerformed(ActionEvent e) {
        jScrollPane1.getViewport().removeAll();
        jScrollPane1.getViewport().add(jLabel1, null);
        if(!done1) {
            jScrollPane1.validate();
            jScrollPane1.getViewport().validate();
            jLabel1.validate();
            done1 = true;
        } else {
            jScrollPane1.revalidate();
            jScrollPane1.getViewport().revalidate();
            jLabel1.revalidate();
        }
    }

    public static boolean done2 = false;
    void jButton2_actionPerformed(ActionEvent e) {
        jScrollPane1.getViewport().removeAll();
        jScrollPane1.getViewport().add(jLabel2, null);
        if(!done2) {
            jScrollPane1.validate();
            jScrollPane1.getViewport().validate();
            jLabel1.validate();
            done2 = true;
        } else {
            jScrollPane1.revalidate();
            jScrollPane1.getViewport().revalidate();
            jLabel1.revalidate();
        }
    }

    public static void main(String[] args) {
        NoRepaint nr = new NoRepaint();
        JFrame frame = new JFrame("NoRepaint test");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.getContentPane().add(nr, BorderLayout.CENTER);
        frame.setSize(200, 200);
        frame.setVisible(true);

        try {
            nr.wait();
        }
        catch(Exception e) {}
    }
}
==========

-- 
A tantot.

http://www.gaudrault.net/
quality costs *less*
Technical Details
Message-ID: <3613E855.2ED9BE00@gaudrault.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36132576.EF03E369@gaudrault.net> <36134763.B010CDB4@Symantec.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,c7e83d79a36a0b59
X-Google-Attributes: gid111d45,public
X-Google-Thread: 101c4c,c7e83d79a36a0b59
X-Google-Attributes: gid101c4c,public
From: "Alain M. Gaudrault" <alain@gaudrault.net>
Subject: Re: Panel won't refresh on demand
Date: 1998/10/01
Message-ID: <3613E855.2ED9BE00@gaudrault.net>
X-Deja-AN: 396811639
Content-Transfer-Encoding: 7bit
References: <36132576.EF03E369@gaudrault.net> <36134763.B010CDB4@Symantec.com>
Content-Type: text/plain; charset=us-ascii
Organization: @Home Network
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing,borland.public.jbuilder.javabeans.writing