I need to place a circle at the center of a GridBagLayout grid. How can I find the current cell width and height?
int center = (int) ( ((double) BoardSize / Num/2) - ((double) size/2.0) );
g.fillOval(center, center, size, size);
At the moment I am using the size of the containing Frame (BroadSize), but I want to add other elements to GridBag that would make it confusing.
You can't get the cell size.
You need to use an appropriate component that will center itself in the space available. You may need to override the setAlignmentX()
and setAlignmentY()
values of you custom component. Of course this approach also assume you have overridden the getPreferredSize()
method of your component properly.
The easiest way is to just use a JLabel with an Icon. See Playing With Shapes for an easy way to do this.