Article View: borland.public.jbuilder.java.swing
Article #13058Re: Panel won't refresh on demand
From: Rod Magnuson
Date: Thu, 01 Oct 1998 00:00
Date: Thu, 01 Oct 1998 00:00
105 lines
4191 bytes
4191 bytes
Alain,
When adding or removing Components from a Container you must validate the container.
Rod
Alain M. Gaudrault wrote:
>
> I have a simple little bean with two buttons on one side, and a label on
> the other. When I click on a button, the previous label is removed, and
> another is added in its place. The problem is that nothing changes when
> I click the buttons unless I resize the frame. And when I uncomment the
> revalidate() lines, it works, but only for one or two clicks, then I get
> the same behaviour where I need to resize the frame. You'd think that
> adding a component would be enough to cause it to draw itself, but it
> doesn't seem like that's happening.
>
> Am I missing something fundamental, here?
>
> ==========
> 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);
> }
>
> void jButton1_actionPerformed(ActionEvent e) {
> jScrollPane1.getViewport().removeAll();
> jScrollPane1.getViewport().add(jLabel1, null);
> // jLabel1.revalidate();
> }
>
> void jButton2_actionPerformed(ActionEvent e) {
> jScrollPane1.getViewport().removeAll();
> jScrollPane1.getViewport().add(jLabel2, null);
> // jLabel2.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:
<36134763.B010CDB4@Symantec.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<36132576.EF03E369@gaudrault.net>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: Rod Magnuson <RMagnuson@Symantec.com> Subject: Re: Panel won't refresh on demand Date: 1998/10/01 Message-ID: <36134763.B010CDB4@Symantec.com> X-Deja-AN: 396634217 Content-Transfer-Encoding: 7bit References: <36132576.EF03E369@gaudrault.net> Content-Type: text/plain; charset=us-ascii Organization: Symantec Mime-Version: 1.0 Reply-To: RMagnuson@Symantec.com Newsgroups: borland.public.jbuilder.java.swing,borland.public.jbuilder.javabeans.writing