Java Forum / GUI / January 2004
Custom Component won't fit in a JScrollPane
newsfan - 12 Jan 2004 10:42 GMT Hi, I have a custom component ... actually it's just a class that extends JPanel and in that class I have a same amount of JCheckBoxes and JLabels..
My CustomClass must be added to another panel, which is a layout of JTables, another panel with buttons etc .. some other panels that are added to my contentpane, but the problem with my CustomClass is that it has a fixed size, thus if the amount of labels and checkboxes being put (size varies under runtime) in the panel exceeds the bounds of the panel they don't show ... only the labels and checkboxes within the bounds of the size of the panel, they show... natural thing is to put my panel in a scrollpane, right?
Well I did that, but the scrollbar doesn't appear when the extra checkboxes and labels appear. So I think could it be because I have set a limit for the panel .... with regards to bounds?
Please help, I can give more info, if necessary of course.
Carl
Adam - 12 Jan 2004 13:01 GMT > Hi, > I have a custom component ... actually it's just a class that extends JPanel [quoted text clipped - 12 lines] > checkboxes and labels appear. So I think could it be because > I have set a limit for the panel .... with regards to bounds? Does your component implement Scrollable interface?
Adam
newsfan - 12 Jan 2004 14:41 GMT Ah, no, it doesn't .. Is it not necessary to put the JPanel inside a JScrollPane, and add this JScrollPane to the main panel?
> > Hi, > > I have a custom component ... actually it's just a class that extends [quoted text clipped - 17 lines] > > Adam Adam - 12 Jan 2004 15:44 GMT > Ah, no, it doesn't .. Is it not necessary to put the JPanel inside a > JScrollPane, > and add this JScrollPane to the main panel? It is necessary to do so but it is not enough. Your component has to implement Scrollable. Read javadocs on the Scrollable interface to know more. I'll bet there are some tutorials on java.sun.com concerning this issue.
And please do not top-post.
Adam
Christian Kaufhold - 12 Jan 2004 15:48 GMT >> Ah, no, it doesn't .. Is it not necessary to put the JPanel inside a >> JScrollPane, >> and add this JScrollPane to the main panel? > > It is necessary to do so but it is not enough. > Your component has to implement Scrollable. Not true.
Christian
Adam - 13 Jan 2004 07:20 GMT > >> Ah, no, it doesn't .. Is it not necessary to put the JPanel inside a > >> JScrollPane, [quoted text clipped - 4 lines] > > Not true. Well, one can use AWT, or swing.JScrollBar instead, or even implement his own scrollbars, or have thousand other ideas.
But OP suggested he wanted to use JScrollPane and in that case his component must implement Scrollable. If you don't agree here, please post a code sample with a Component NOT implementing Scrollable, embedded in a JScrollPane that would be scrolled nicely.
regards, Adam
ak - 13 Jan 2004 08:20 GMT > But OP suggested he wanted to use JScrollPane > and in that case his component must implement > Scrollable. thats not true!
It enough if component just set properly his preferredSize. Scrollable is for _complex_ components which need _fine_grained_scroll_control_.
> If you don't agree here, please > post a code sample with a Component NOT > implementing Scrollable, embedded in a JScrollPane > that would be scrolled nicely. example below:
public class ImagePanel extends JPanel {
Image img;
public ImagePanel(Image img) { this.img = img; }
public Dimension getPreferredSize() { if(img == null) return super.getPreferredSize(); int w = img.getWidth(null); int h = img.getHeight(null); return new Dimension(w, h); }
public void paintComponent(Graphics g) { g.setColor(getBackground()); g.fillRect(0,0,getWidth(), getHeight()); if(img != null) { g.drawImage(img, 0, 0, null); } }
public static void main(String [] args) { JFrame frame = new JFrame(); Image img = Toolkit.getDefaultToolkit().getImage(...); ImagePanel panel = new ImagePanel(img); frame.getContentpane().setLayout(new BorderLayout()); frame.getContentPane().add(panel); frame.pack(); frame.show(); } }
____________
http://reader.imagero.com the best java image reader.
Adam - 13 Jan 2004 09:29 GMT > It is enough if component just sets properly his preferredSize. > Scrollable is for _complex_ components which need > _fine_grained_scroll_control_. Well, I have to agree, I was wrong, you guys are right. I just could not make it work that way...
By the way, you omitted the JScrollPane in your code:
> frame.getContentPane().add(panel); of course should be
> frame.getContentPane().add(new JScrollPane(panel)); Adam
ak - 13 Jan 2004 10:00 GMT > By the way, you omitted the JScrollPane in your code: > > frame.getContentPane().add(panel); > > of course should be > > frame.getContentPane().add(new JScrollPane(panel)); That's right ;-) --
____________
http://reader.imagero.com the best java image reader.
Andrew Thompson - 12 Jan 2004 20:45 GMT | > > Hi, | > > I have a custom component ... actually it's just a class that extends | > JPanel | > > and in that class I have a same amount of JCheckBoxes and JLabels.. ...
| > Does your component implement Scrollable interface? | Ah, no, it doesn't .. Is it not necessary to put the JPanel inside a | JScrollPane, | and add this JScrollPane to the main panel? I read your initial post and was so thoroughly confused by it that I left it. Would you consider submitting an example? http://www.physci.org/codes/sscce.jsp
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Walter Probst - 12 Jan 2004 16:50 GMT Not sure about this, but try invalidate(); and validate(); on the custom panel after insertion of those labels and checkboxes. Maybe the scrollpane don't get the change 'cause the size of your panel was not really changed? The size of the panel shouldn't be fixed to - it changes with every additional widget. The scrollpane should take care of the size changes and show necessary scroll bars.
Walter
newsfan - 13 Jan 2004 00:02 GMT I just tried that like you said, but it is the same. I don't know what these two methods do, but they won't put the labels/checkboxes that exceed the bounds of the JPanel in a Scrollpane. I'm gonna try to implement the Scrollable interface on my custom component, but I'm not sure how to use it.. would you know? Thanks Carl
> Not sure about this, but try invalidate(); and validate(); on the custom > panel after insertion of those labels and checkboxes. Maybe the scrollpane [quoted text clipped - 4 lines] > > Walter Andrew Thompson - 13 Jan 2004 06:34 GMT | > Not sure about this, but try invalidate(); and validate(); | | I just tried that like you said, but it is the same. I don't know what these | two methods do, Go directly here, do _not_ pass 'Go', do _not_ collect $200.. You apparently need some basic GUI tutorials, go straight to Sun.. http://java.sun.com/docs/books/tutorial/uiswing/index.html
[ You might also need to pay a little more attention to what posters such as Christian Kaufhold over advice that starts with 'Not sure about this..' (no offense intended) ]
newsfan - 13 Jan 2004 10:36 GMT I'm a little bit insulted that you direct me to a basic GUI tutorial. I know how to build panels with components in them and place them on contentpane, I might not be able to explain how to do it myself, that must be teaching skills, and to program it is not required to teach how to yourself, just to do.
I need advanced tutorials, dude! Carl
> Go directly here, do _not_ pass > 'Go', do _not_ collect $200.. [quoted text clipped - 6 lines] > Kaufhold over advice that starts with > 'Not sure about this..' (no offense intended) ] Andrew Thompson - 14 Jan 2004 02:52 GMT | I'm a little bit insulted that you direct me to a basic GUI | tutorial. I know how to build panels with components in them | and place them on contentpane, The rest of the conversation above blows that theory out of the water.
Forgot the JScrollPane, ...sheeesh!
It's also important to point out that you'd have got a solution a lot faster if you had a) questioned Christian further, or challenged his advice (rather than, seemingly, ignore it). b) submitted the SSCCE as I suggested..
errr.. dude. ;-)
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Walter Probst - 13 Jan 2004 07:48 GMT Sorry, you where right. I tried it out and what you need to do is not one of those validate-calls. Just set the size of your panel after widget insertion to the preferredSize (assumed "this" is the panel: this.setSize(this.getPreferredSize()) ). It's necessary because the panel is don't update it's own size. Usually it is done by the layout manager of the panels parent container and depends on the size of the parent, the parents children-widgets and some other things. It's posible to set the size of a container from its own layout manager, but none of the Swing layouts do that (rightly!).
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|