6 Replies - 4492 Views - Last Post: 14 May 2008 - 09:37 AM Rate Topic: -----

#1 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

JFrame repaint problem after adding a JComponent

Posted 14 May 2008 - 08:27 AM

Hello

I have a repaint() problem. I nailed it down the minimum code.

A JFrame with a BorderLayout.
In the center a JPanel panel whith a GridLayout filled with an array of JLabel.
The size of the JFrame is determined by the number of rows/columns
In the real life the program decides that it needs more JLabel, here I simulate the situation with the JButton "GO" in BorderLayout South.

When the button is clicked
The JPanel is removed from the JFrame
I increment the number of rows and columns by 10
Create a new JPanel panel with gridLayout +10
fill it with a new array of JLabel
add it BorderLayout.CENTER
size of the JFrame is set accordingly
then
repaint();

What happens is that the JFrame size is refreshed but not the the JLabel and the JPanel don't show.
Je JButton stays where it used to be.

If I resize, with the mouse, by 1 pixel the JFrame... everyting is displayed correctly.

Do I have a repaint() missing somewhere.

Thanks

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Problem extends JFrame implements ActionListener {

	// Size of the initial Grid
	private int nbRows = 10, nbColumns = 10;
	// label width and height
	private int labelWH = 20;
	// the JLabel inthe grid
	JLabel[][] label;
	JButton button = new JButton("GO");
	// will hold the GridLayout
	JPanel panel;

	// constructor
	Problem() {
		// frame stuff
		super("V1.0");
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  
		// build Panel and its labels
		buildPanelAndLabels();
		
		add(panel, BorderLayout.CENTER);
		// the JButton
		button.addActionListener(this);
		add(button, BorderLayout.SOUTH);
		// set size of frame according to labelWH
		setSize(nbColumns*labelWH, nbRows*labelWH + 24);
		setVisible(true);
	}

	// build the JPanel panel
	void buildPanelAndLabels() {
		panel = new JPanel(new GridLayout(nbRows, nbColumns, 1, 1));
		panel.setBackground(Color.BLUE);
		// and fill its grid with JLabel
		label = new JLabel[nbRows][nbColumns];
		for(int i = 0; i < nbRows; i++) {
			for(int j = 0; j < nbColumns; j++) {
				label[i][j] = new JLabel("");
				label[i][j].setOpaque(true);
				label[i][j].setBackground(Color.WHITE);
				panel.add(label[i][j]);
			}
		}
	}

	public void actionPerformed(ActionEvent arg0) {
		// we create a new  the JPanel
		nbRows += 10;
		nbColumns += 10;
		// remove old panel
		getContentPane().remove(panel);
		buildPanelAndLabels();
		// put the new Panel in the Center
		add(panel, BorderLayout.CENTER);
		setSize(nbColumns*labelWH, nbRows * labelWH + 24);	
		repaint();
	}
	
    // an to test all that
	public static final void main(String[] arg) {
		new Problem();
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: JFrame repaint problem after adding a JComponent

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: JFrame repaint problem after adding a JComponent

Posted 14 May 2008 - 08:44 AM

I could not reproduce your problem, it works fine on my system.
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: JFrame repaint problem after adding a JComponent

Posted 14 May 2008 - 08:50 AM

Maybe the validate or pack functions of the container can help. (I am not sure right now which one is the preferred way to do it)
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

Re: JFrame repaint problem after adding a JComponent

Posted 14 May 2008 - 08:54 AM

View Post1lacca, on 14 May, 2008 - 08:44 AM, said:

I could not reproduce your problem, it works fine on my system.

Thanks
I was using Eclipse
I tried to java it from the console prompt... same problem
JRE 1.5 or 1.6 ? I am using 1.5.
Was This Post Helpful? 0
  • +
  • -

#5 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: JFrame repaint problem after adding a JComponent

Posted 14 May 2008 - 09:00 AM

The validate bit might do it - try panel.revalidate(); before you repaint.
Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: JFrame repaint problem after adding a JComponent

Posted 14 May 2008 - 09:12 AM

Probably 1.6(got a bunch of JREs installed).
The IDE is NetBeans 6.1, but I don't think that it would make any difference.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,182
  • Joined: 06-March 08

Re: JFrame repaint problem after adding a JComponent

Posted 14 May 2008 - 09:37 AM

View PostEllie, on 14 May, 2008 - 09:00 AM, said:

The validate bit might do it - try panel.revalidate(); before you repaint.

Thanks Ellie

Always RTFM

public void validate()
   Validates this container and all of its subcomponents. 

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified [color=#FF0000](added to or removed from the container, or layout-related information changed) after the container has been displayed. [/color] 



Isn't that exactly what I am doing ? :)

Put the validate() to the JFrame though not the JPanel.

It worked, even remove the call to repaint()

Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1