0

I'm writing MineSweeper and using JButtons on a GridLayout. The numbers of rows and columns are entered by the user, so setting fixed size may cause several problems.

How can I remove the space between the buttons without setting the fixed size of the panel?

Adjustable cell MineSweeper

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
gtboy
  • 128
  • 1
  • 3
  • 12
  • It is better to use GridbagLayout, this will give you all the options you need for customizing your UI, like setting weight etc. – NiranjanBhat Nov 19 '12 at 06:10
  • 1) Generally it is achieved using a `GridLayout` with no spacing and `pack()` on the frame. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 19 '12 at 06:11

2 Answers2

3

This seems to be more of a issue with JButton then with GridLayout

Here, I replaced JButton with JPanel (and some border magic)

enter image description here

The other thing I tried was passing a negative h/vgap to the GridLayout

enter image description here

I'd however not recommend this, as it may have unforeseen effects on other platforms.

I personally, would be tempted to use a custom JLabel (with it's own mouse listener attached) to take the place of the buttons. You could even fire and action event when it's clicked...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • *"This seems to be more of a issue with JButton"* I don't think so. [This answer](http://stackoverflow.com/a/10862262/418556) shows how to use 4 icons as buttons (5 as labels). in a UI with no spaces between them. – Andrew Thompson Nov 19 '12 at 07:59
  • @AndrewThompson The buttons as there are, have there border offset, so that there is space between the outer edge and the default border, so the white space between the buttons is actually internal spacing. Take the border out and you can probably solve the white space issue - at least in my testing – MadProgrammer Nov 19 '12 at 08:35
1

You need to set the hgap and vgap to 0 when creating the gridlayout instance.

Like,

   GridLayout myLayout = new GridLayout(0,2,0,0);
Steve Robinson
  • 3,759
  • 3
  • 36
  • 57