• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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
 
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
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Onion rings are vegetable donuts. Taste this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic