• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel is not sized correctly  RSS feed

 
Oleg Shubin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application a JPanel is not sized correctly: only its height is respected while its width is always set to 10 pixels. A screenshot is shown below:

I suspect some layout problems in my code since the component is actually my own class (EPanel) that extends JPanel; however, it should not have any issues since it is used in other places without any problems (e.g. the panel titled "OLEGSHA" is also an EPanel). What I do think is causing the problem is a JPanel inside the EPanel (line 15 in the first code). The vertical size is correct but the horizontal one isn't: it should take approximatly the entire frame.
In the code examples below I also use my own LayoutManagers but they aren't responsable either: same happened with BoxLayout and FlowLayout; as well that I tried replacing EPanel with other components (JButton in particular) and the issue disappeared.

The EISlotTable (the actual class being displayed) declaration and constructor (the rest are fields and getters):


The EPanel (the superclass of EISlotTable) declaration and constructor:


Finally, the code that handles the EISlotTable (the component's layout is VerticalFlowLayout (as mentioned before it works; similar to BoxLayout with Y_AXIS)):


Thank you in advance.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should take approximatly the entire frame.


Sounds to me like you are using a GridBagLayout as the main layout. When using a GridBagLayout if the preferred Size of the components can't be displayed, then the minimum size of the component is used.

its width is always set to 10 pixels.


Or maybe you are using custom panel for painting and you don't override the "getPreferredSize(...)" method of the panel to return the size of the panel. By default a JPanel uses a FlowLayout and the minimum size will be (10 , 10) since this accounts for the verticaly/horizontal gap before/after components, so it seems like you have added any components to the panel.

If these suggestion don't help, then post a proper SSCCE that demonstrates the problem because we can't guess what you are doing based on the random code provided.
 
Oleg Shubin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out the problem - in the EISlotTable class, I had a field named "width" and thus I named the getter "getWidth()" which actually overrides JComponent.getWidth(). As the width field in EISlotTable was 10 the component's width was also set to ten. Stupid me. Should be more careful next time, as I spent the entire day with this.
Anyway, thank you Rob, as I found this out while trying to write a SSCCE.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!