Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JPanel component only repainted after resizing the frame

1,335 views
Skip to first unread message

Klaas van Gelder

unread,
Aug 14, 2002, 8:06:44 AM8/14/02
to
Hi people :-)

In a part of my app (a game), I draw a playfield which consists of a
grid of "cells".
The resolution of the playfield can be made twice as big by pressing
on the "x2" button.
But... when the playfield (GraphicalViewPanel) is painted, the cells,
which are themselves
panels added to the playfield, are not repainted UNTIL the playfield
is resized !!
I already have figured out using println's that the paintComponent()
method of the "cells"
is only called after resizing the frame.

The cells (here of type JPanel but a subclass of JPanel in the real
app where they contain
data) are saved in the Map celPanels.
The method fillCellPanels fills this map according to the private
members nrOfRows and nrOfCols.
The method addCellPanelsToViewPanel adds all cells in this map to the
playfield.

But WHY must i resize the main application frame after pressing "x2"
to see the effect???
PLEEZ HELP, this problem bothers me already 2 months or so.......
Greetz Klaas

The app consists of 2 classes which are listed below... They're not
too big :-)


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

public class TestFrame extends JFrame {


public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.getContentPane().setLayout(new BorderLayout());
final GraphicViewPanel gvp = new GraphicViewPanel();
frame.getContentPane().add(gvp, BorderLayout.CENTER);

JPanel knoppenPanel = new JPanel();

JButton groterButton = new JButton("x2");
groterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
gvp.doubleResolution();
gvp.repaint(); //not enough, playfield must be resized
}
});

knoppenPanel.add(groterButton);
frame.getContentPane().add(knoppenPanel, BorderLayout.SOUTH);

frame.show();
}
}

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

public class GraphicViewPanel extends JPanel {

private int nrOfRows = 3;
private int nrOfCols = 3;
private int offsetRows = 0;
private int offsetCols = 0;

private Map celPanels = new HashMap();
private MouseListener mouseListener;

public GraphicViewPanel() {
setBackground(new Color(0, 200, 30));
setMinimumSize(new Dimension(200, 0));

fillCellPanels();
addCellPanelsToViewPanel();
} //constructor

public void fillCellPanels() {
System.out.println("fillCellPanels");
celPanels.clear(); //en maak de map met verwijzingen naar de
panels leeg
for (int row = offsetRows; row < offsetRows + nrOfRows; row++) {
for (int col = offsetCols; col < offsetCols + nrOfCols; col++) {
Point key = new Point(col, row);
JPanel celPanel = new JPanel();
//vul celpanel met levende cellen
celPanels.put(key, celPanel);
}
}
}

public void addCellPanelsToViewPanel() {
System.out.println("addCellPanelsToViewPanel");
this.removeAll();
this.setLayout(new GridLayout(nrOfRows, nrOfCols, 1, 1));
JPanel celPanel;
for (Iterator iterator = celPanels.entrySet().iterator();
iterator.hasNext(); ) {
//aanpassen
celPanel = (JPanel)((Map.Entry)iterator.next()).getValue();
//System.out.println("Celpanel wordt toegevoegd");
this.add(celPanel);
}
}

public void doubleResolution() {
nrOfRows = nrOfRows*2;
nrOfCols = nrOfCols*2;
fillCellPanels();
addCellPanelsToViewPanel();
}


public void paintComponent(Graphics g) {
/**@todo: Override this javax.swing.JComponent method*/
super.paintComponent( g);
System.out.println("GraphicViewPanel.paintComponent()");
}
} //GraphicViewPanel

Jason Teagle

unread,
Aug 14, 2002, 8:22:14 AM8/14/02
to

"Klaas van Gelder" <k.van...@genimen.com> wrote in message
news:5f4a4c93.02081...@posting.google.com...

> But WHY must i resize the main application frame after pressing "x2"
> to see the effect???
> PLEEZ HELP, this problem bothers me already 2 months or so.......
> Greetz Klaas

Whenever you add or hide components, or resize (in code) their container,
you should usually call validate() on their container to get them
re-layed-out. This might solve your problem...


--
--
Jason Teagle
ja...@teagster.co.uk
-----------------------------------------------------------
A list of programming resources I use:
ML: www.windev.org, www.codecipher.com, www.beginthread.com
MB: www.codeguru.com, www.codeproject.com
NG: comp.lang.java.*
OI: www.php.net
-----------------------------------------------------------

Babu Kalakrishnan

unread,
Aug 14, 2002, 10:32:01 AM8/14/02
to
On Wed, 14 Aug 2002 13:22:14 +0100, Jason Teagle <ja...@teagster.co.uk> wrote:
>
> "Klaas van Gelder" <k.van...@genimen.com> wrote in message
> news:5f4a4c93.02081...@posting.google.com...
>
>> But WHY must i resize the main application frame after pressing "x2"
>> to see the effect???
>> PLEEZ HELP, this problem bothers me already 2 months or so.......
>> Greetz Klaas
>
> Whenever you add or hide components, or resize (in code) their container,
> you should usually call validate() on their container to get them
> re-layed-out. This might solve your problem...
>

revalidate() is the more appropriate call on Swing components. Figuring
out the validate() / invalidate() combinations required to display a
particular mutation to the GUI structure is a nightmare for new AWT
users, and Swing simplifies this quite a bit by providing the revalidate
method which performs all the required magic internally.

BK

Jason Teagle

unread,
Aug 14, 2002, 10:38:34 AM8/14/02
to

"Babu Kalakrishnan" <ka...@sankya.com> wrote in message
news:slrnalkqf...@ganga.sankya.com...

> revalidate() is the more appropriate call on Swing components. Figuring
> out the validate() / invalidate() combinations required to display a
> particular mutation to the GUI structure is a nightmare for new AWT
> users, and Swing simplifies this quite a bit by providing the revalidate
> method which performs all the required magic internally.

OK, I'll bear this in mind.

Klaas

unread,
Aug 14, 2002, 12:42:11 PM8/14/02
to
Although I do not resize the container, the call gvp.revalidate() solves
the problem !
Maybe the fact that the panels are re-added to the container after pressing
x2 requires also that the container is laid out again...

Thanx anyway !
Klaas


Jason Teagle

unread,
Aug 15, 2002, 5:37:59 AM8/15/02
to

"Klaas" <klaa...@yahoo.com> wrote in message
news:3d5a886b$0$5477$8fcf...@news.wanadoo.nl...

Yes, that's why - as I said, whenever you hide (or show) items it needs
revalidating.

0 new messages