aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane Frustrations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane Frustrations" Watch "JScrollPane Frustrations" New topic
Author

JScrollPane Frustrations

Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

Ok, I'm trying to create a JScrollPane that will hold custom visual classes. The way I'm currently doing it is


then when a button is pressed



What I would like to have happen is when I press the button, it will add an instance of the RandomTestBar to the changesPanel, and since the RandomTestBar is bigger than the space I've given to the scroll pane, I would want the scroll bar to appear. Right now, the RandomTestBar is just fitting itself inside the scroll pane. Is this a scroll pane problem or a layout problem, and how would you suggest a fix for this.

Thanks much,
Chris

Edit: What I want it to look like

[ February 07, 2008: Message edited by: Chris Waguespack ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
I once had to do something similar, and I ended up using a JList with a custom renderer. One motivation for this is I wanted to be able to drag/drop the panels, so it was handy to use JList's TransferHandler support. But also JList was built to be placed in a JScrollPane and for items to be added/deleted dynamically.

It should be possible to get your approach working (try calling revalidate() not only on the changesPanel but also on the changesScrollPane and see if that helps) but if your RandomTestBar objects are simple enough you might want to consider using a JList.


bitguru blog
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
To show what I mean, here's some code that approximates the look of your diagram.

Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

Ah, ok. That fixes part of my issues. Thanks. So, if I wanted to add a button, or other dynamic component to the bar, would I need to use a table with a custom editor instead? If so, I've tried that but can't seem to get a grasp on the correct way to implement it. Any advise or examples that may be useful?

Thanks,
Chris
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Chris Waguespack:
So, if I wanted to add a button, or other dynamic component to the bar, would I need to use a table with a custom editor instead?


JList doesn't have editors at all so, yes, you could use a one-column JTable instead and set a custom cell editor if you want to go with an approach like this. If there components that the user needs to manipulate (perhaps even a single button) I'm not sure this approach is worth it.

Are these test bars related to the panels in this older post of yours? If so I think my advice in that thread is better than my advice in this thread.
Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

Ah! I completely forgot about that.
Yes, that one does great, but I need the custom bars to stay a fixed size, so that they will not try to take up the entire viewing area. Setting the minimum/preferred size of the panels doesn't seem to help. Thanks for all of your help and patience.

-Chris
Chris Waguespack
Ranch Hand

Joined: Jul 26, 2007
Posts: 36

Ok, I got it this far, but the scroll bars won't update until I resize the window. How do I fix this?



Thanks,
Chris

EDIT: Found what was wrong, needed to validate the changesScrollPane instead of the changesScrollPanel. Thanks for your help, I'm finally done
[ February 27, 2008: Message edited by: Chris Waguespack ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Chris Waguespack:
the scroll bars won't update until I resize the window. How do I fix this?


If the JPanel doesn't realize it needs to change size then its enclosing JScrollPane won't adjust its scroll bars.

It should work if you replace changesScrollPanel.validate() with changesScrollPane.validate(). Or actually, I would instead call changesScrollPane.revalidate() but the result is the same.

[edit: I see you figured this out as I was typing. cool.]
[ February 27, 2008: Message edited by: Brian Cole ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JScrollPane Frustrations
 
Similar Threads
refresh a jtable
Adding JScrollPane to JPanel?
JDialog memory leak with code example
Drawing with mouse
Problem with visual editor/visual class