• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mark Brunson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as CustomDrawing() has no components, it possibly needs to have a preferredSize set
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Brunson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic