Search code examples
javaawtgridbaglayout

Getting a GridBagLayout's cells' pixel dimensions


Is it possible to find the current dimensions in pixels of a requested cell (x,y) in a GridBagLayout JPanel?

EDIT: My usage would be to have a JLabel that fills the area provided by the layout. I want to give it the maximum size made available to it.


Solution

  • My usage would be to have a JLabel that fills the area provided by the layout. I want to give it the maximum size made available to it.

    That is the job of the layout manager. You are not supposed to specify the size of a component.

    Read the section from the Swing tutorial on How to Use a GridBagLayout. You will want to specify the "fill" constraint when you add the component to the layout.

    Maybe other layout managers can solve your problem as well. The tutorial has examples for all the layout mangers.