aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes gridlayout not working with custom jpanel and jscrollpane; my jpanels get cut off Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "gridlayout not working with custom jpanel and jscrollpane; my jpanels get cut off" Watch "gridlayout not working with custom jpanel and jscrollpane; my jpanels get cut off" New topic
Author

gridlayout not working with custom jpanel and jscrollpane; my jpanels get cut off

Mark Brunson
Greenhorn

Joined: Apr 29, 2012
Posts: 14
I'm trying to add my own implementation of jpanel to another jpanel, in a scrollpane, so that as it adds more it will make a list of them down the screen. Unfortunately I can't seem to put space between my jpanels; when I set the second two gridlayout parameters, parts of them get cut off (so that they appear shorter vertically). Any idea why this is happening or how to get around it?

(called from a method in a jframe class)

custom drawing panel
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
as CustomDrawing() has no components, it possibly needs to have a preferredSize set
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Welcome to the Ranch, Mark!

Your CustomDrawing instance never paints itself on the screen. You've overridden the paint method to draw to the BufferedImage's graphics object instead. That object will never be null though, as it's created in the CustomDrawing constructor and never set to null afterwards.

I suggest you change your code:
1) Call drawStuff() in the constructor. There is no need to do it in any painting method, as the painting methods may be called several times a minute. The contents will never change though, so once is enough.

2) Do not override update(Graphics).

3) Do not override paint(Graphics).

4) Override paintComponent(Graphics) like this:
The preferred size issue probably still applies as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mark Brunson
Greenhorn

Joined: Apr 29, 2012
Posts: 14
setPreferredSize does fix my issue, thanks! The reason for all the overridden methods in CustomDrawing is that I'm actually using a much larger, more complicated class that frequently updates instead, I just wrote this one to have something simple to represent the problem I was having without tons of irrelevant code.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: gridlayout not working with custom jpanel and jscrollpane; my jpanels get cut off