Simple repaint problem: JFrame is not repainting

S

Scott Steiner

Hi,

the following simple problem: I have a JFrame with a borderlayout. In
that JFrame I have a main JPanel in the CENTER and a footer JPanel in
the SOUTH. There is a JButton on the footer panel which when clicked
should simply resize the footer panel.

My problem is that the footer panel is not resizing unless I enforce a
repaint by say enlarging the JFrame with the mouse i.e make the window
larger, or smaller, or anything which will enforce a repaint. A
programatic repaint i.e this.repaint() is not producing the desired
results.

Here's the short sample code if anyone is interested, any help
appreciated.

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

public class Test extends JFrame implements ActionListener
{
private JPanel panelMain, panelFooter;
private JButton btn;

public Test()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());

panelMain = new JPanel();
panelMain.setBackground(Color.CYAN);
c.add(panelMain, BorderLayout.CENTER);

panelFooter = new JPanel();
panelFooter.setPreferredSize(new Dimension(0,100));
panelFooter.setBackground(Color.GREEN);
c.add(panelFooter, BorderLayout.SOUTH);

JButton btn = new JButton("Resize");
btn.setActionCommand("resize");
btn.addActionListener(this);
panelFooter.add(btn);

this.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
String cmd = ae.getActionCommand();
if (cmd.equals("resize"))
{
panelFooter.setPreferredSize(new Dimension(0,50));
this.repaint();
}
}

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

Arnaud Berger

Hi,

try this :

panelFooter.setPreferredSize(new Dimension(0,50));
panelFooter.revalidate();

P.S : this would better fit to comp.lang.java.gui

Cheers,

Arnaud
 
C

Christian Kaufhold

Scott Steiner said:
the following simple problem: I have a JFrame with a borderlayout. In
that JFrame I have a main JPanel in the CENTER and a footer JPanel in
the SOUTH. There is a JButton on the footer panel which when clicked
should simply resize the footer panel.

My problem is that the footer panel is not resizing unless I enforce a
repaint by say enlarging the JFrame with the mouse i.e make the window
larger, or smaller, or anything which will enforce a repaint. A
programatic repaint i.e this.repaint() is not producing the desired
results.


revalidate()!



Christian
 
T

Thomas G. Marshall

Scott Steiner coughed up:
Hi,

the following simple problem: I have a JFrame with a borderlayout. In
that JFrame I have a main JPanel in the CENTER and a footer JPanel in
the SOUTH. There is a JButton on the footer panel which when clicked
should simply resize the footer panel.

My problem is that the footer panel is not resizing unless I enforce a
repaint by say enlarging the JFrame with the mouse i.e make the window
larger, or smaller, or anything which will enforce a repaint. A
programatic repaint i.e this.repaint() is not producing the desired
results.

....[rip]...

Steiner and Kaufhold have given you the correct answer, but you need to
understand something that confuses nearly everyone getting started with the
java GUI and graphics facilities.

Swing components are laid out according to swing layout rules. No repaint()
to call here. revalidate() is the call that signals to swing that things
need to be re-layed out visually, because something has changed.

Each swing component itself is just like any customized component you would
make. You would extend, say, JComponent, and then override paintComponent()
and then place within that method all the graphics drawing you would need to
draw your component out. lines, circles, colors, text, whatever. Here, if
something changes for your component that requires it to be redrawn, then
repaint() is called, which sets a flag to have paintComponent() called at an
appropriate time.

So when you are using only swing components, there is no call to repaint()
for you to make. Just revalidate() should you need it (!), among the many
other calls to establish the layout manager, etc. If you are making a
component of your own, and you need custom drawing within it, you have your
own paintComponent(), which may or may not need you to call repaint() at
particular places. Swing will call repaint() for you in some cases, but not
in all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top