Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Specify exact height of panel or component  RSS feed

 
Manjula Rao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I specify the exact height of a panel or a component?
I have to ristrict the height of a JEditorPane to occupy a few pixels at the bottom of my frame. the text in the JEditorPane is set at runtime. when at runtime the text is set the size of the JEditor pane increases to acommodate the whole text. i have tried using the JScrollPane, but the problem persists.
Thankyou
Manjula
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not as familiar with swing as I am with the standard awt but this should work anyway. To get complete control over the sizes, etc of you components simply set the layout to null and use the component's setBounds() method. Unfortunately, this means you have to control all of the bounds for your components. Perhaps adding the component you want to control to it's own panel and then set that panel's layout to null might solve this probelm (however, components tend to inherit their parent layouts and the null layout maybe overridden). To dynamically size the component to the font, get the font size using myFont.getSize() and then set the bounds of the component accordingly. After this, call repaint(). I hope this helps.
Sean
 
Manjula Rao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Sean MacLean,
As u suggested, i could use a null layout and get ful control of the component size but this would prove disadvantageous in other respects like when the Resolution is changed the panel set in null layout will not change etc.
I am having this problem in two panels, one is using FlowLayout, having buttons on it. I would like to restrict the size of the panel to exactly the size of the controls placed on it and not show a blank margin around it.
In the second case i am using BorderLayout with the JEditorPane in the center.
I have tried using SetBound(), setSize(), but none of them work.
Thankyou
Manjula
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No they won't work unless you set the layout to null. How about this. In the component that is holding you panel with the buttons, set the layout to null so that you can specify it's exact size. However, to handle the resizing do something like this:


This way, everytime the window is resized (and, therefore repainted) the component will adjust itself to the size of it's parent container. You'll have to fuss with the exact variables to get the type of resizing you want but this shows you the idea. I use this a fair amount when a layout manager won't cut it. To be extra efficient you should create a window listener that will detect the resize event and only call resizeComponent() there.
Having said all this, the GridBagLayout might be able to handle the type of layout you want and you can avoid all of this from the outset. Perhaps that might be a better place to start. Good luck.
Sean
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, here's what I do:
Dimension labelSize = new Dimension((200,16);
label.setMaximumSize(labelSize);
label.setMinimumSize(labelSize);
label.setPreferredSize(labelSize);
This almost works really great! See how it does for you.
e
 
Manjula Rao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou so much Eric Moon
That has perfectly solved my problem.
Thanx once again.
Manjula
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!