Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

JPanel doesn't update fields.. Help

Thread view: 
Lobang Trader - 14 Apr 2004 05:19 GMT
I have been stumped with this problem for days.
The JPanel does not update the JLabel component into the new JButton.
Even if the JButton is a new JLabel, the panel doesn't update.
Any help a million times appreciated.  thanks

import java.awt.BorderLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class Test extends JPanel {

    private JComponent display;

    public Test() {
        setLayout(new BorderLayout());

        display = createDisplay();
        if(display != null)
            add(display, BorderLayout.WEST);
    }

    public void setDisplay(JComponent display) {
        this.display = display;
        revalidate();
        repaint();
    }

    protected JComponent createDisplay() {
        return new javax.swing.JLabel("JLabel");
    }

    // Test code.

    public static void main(String[] args) {
        javax.swing.JFrame frame = new javax.swing.JFrame();

frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
        frame.setVisible(true);

        Test test = new Test();
        frame.add(test, BorderLayout.SOUTH);

        test.setDisplay(new javax.swing.JButton("JButton"));
    }
}
Karl von Laudermann - 14 Apr 2004 16:45 GMT
> I have been stumped with this problem for days.
> The JPanel does not update the JLabel component into the new JButton.
> Even if the JButton is a new JLabel, the panel doesn't update.
> Any help a million times appreciated.  thanks

First of all, because of the way JFrames work, you need to change the
line:
    frame.add(test, BorderLayout.SOUTH);
to:
    frame.getContentPane().add(test, BorderLayout.SOUTH);

Second, all your setDisplay method does is store the new component in
the display member variable of your Test object. It doesn't actually
add the new component to the window. Try this:

    public void setDisplay(JComponent display) {
        this.display = display;
        add(display, BorderLayout.WEST);
        revalidate();
    }
Lobang Trader - 15 Apr 2004 03:17 GMT
> > I have been stumped with this problem for days.
> > The JPanel does not update the JLabel component into the new JButton.
[quoted text clipped - 6 lines]
> to:
>      frame.getContentPane().add(test, BorderLayout.SOUTH);

I'm using JDK 1.5 and therefore using frame.add() is similar to using
frame.getContentPane().

> Second, all your setDisplay method does is store the new component in
> the display member variable of your Test object. It doesn't actually
[quoted text clipped - 5 lines]
>          revalidate();
>      }

Actually, if you re-add the component, revalidate() is redundant.

I have tried re-adding it but to me it is ugly code, which I could be
wrong.  Is there anyway to solve it without re-adding?
Roedy Green - 16 Apr 2004 00:29 GMT
>import java.awt.BorderLayout;
>
[quoted text clipped - 10 lines]
>         display = createDisplay();
>         if(display != null)
//          you add the JLabel object here.  The JPanel has its OWN
//          pointer to that object from now on.  It won't change
//          unless
//          you remove/add.
//          You are passing a pointer to your JLabel object not the
//          indirect address of the display variable.  add does not
//          know about display, just the JLabel object.
>             add(display, BorderLayout.WEST);
>     }
>
>     public void setDisplay(JComponent display) {
// all this does is change your reference to the display object,
// the JPanel is still pointing to the old JLabel.
>         this.display = display;
>         revalidate();
[quoted text clipped - 19 lines]
>     }
>}

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.