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.