2

I have a grid layout on which I have added 100 Jbuttons.Now I require to have the buttons without any margin or gap among them.What I tried is here :

private void initializeBoard() {
    container = getContentPane();
    container.setLayout(new GridLayout(1,2));
    boardPanel = new JPanel(new GridLayout(10,10));
    dataPanel = new JPanel();

    int index = 0;
    for(int i=0;i<BOARD_SIZE;i++){
        for(int j=0;j<BOARD_SIZE;j++){
            squares[i][j] = new JButton(String.valueOf(numbers[index]));
            squares[i][j].addActionListener(this);

            squares[i][j].setMargin(new Insets(0,0,0,0));
            squares[i][j].setBorder(BorderFactory.createEmptyBorder());
            squares[i][j].setBorderPainted(false);
            squares[i][j].setContentAreaFilled(false);
            squares[i][j].setFocusPainted(true);
            squares[i][j].setIcon(new ImageIcon("images/board/"+numbers[index++]+".jpg"));

            boardPanel.add(squares[i][j]);
        }
    }
    boardPanel.setBorder(BorderFactory.createEmptyBorder());
    container.add(boardPanel);
    container.add(dataPanel);
}

But the result is like this.I still get vertical gaps among the buttons. Current Result

I need to eliminate this gaps so that I get no gaps among the buttons and get the buttons to represent the whole board.

Thanks in advance.

EDIT Got it.The images I was using was not wide enough to fill those areas.Thanks.

Shaheed
  • 45
  • 5

2 Answers2

0

Got it.The images I was using was not wide enough to fill those areas.Thanks.

Shaheed
  • 45
  • 5
-1

You can also use setVgap(0), setHgap(0) to set the Vertical and Horizontal gap to zero.

Cheers!

Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23