1

I'm generating a chessboard using 3 JPanels with folowing GridLayouts:

  1. 8x8 for gameboard
  2. 1x8 for vertical markers left from the board
  3. 8x1 for horizontal markers under the board

I'm using resizable JFrame to contain it alltogether. I actually used this code, a little modified.

This means, after populating al three panels, add them to main JFrames panel like this:

Container contentPanel = getContentPane();
/**Create some panels**/
JPAnel 8x8 = new JPanel();
8x8.setLayout(new GridLayout(8, 8));
JPAnel 1x8 = new JPanel();
8x8.setLayout(new GridLayout(1, 8));
JPAnel 8x1 = new JPanel();
8x8.setLayout(new GridLayout(8, 1));
/**Populate frames**/
...
/**Assign frames**/

contentPanel.add(8x8, BorderLayout.CENTER);
contentPanel.add(8x1, BorderLayout.SOUTH);
contentPanel.add(1x8, BorderLayout.WEST);

It's not that bad, but it still looks like this:

enter image description here

The bottom line expands unther to vertical line. The larger the window is, the more obvious this issue gets.

I'd like to glue the marker lines to the chessboard so that every number will allways be aligned with appropriate line.

I tried to assign 2x2 layout to the parent frame contentPanel however, grid layout seems to require all elements to be the same size, so it just makes bigger areas for 8x1 and 1x8.
My approach:

contentPanel.setLayout(new GridLayout(2, 2));

contentPanel.add(1x8);    //vertical markers first
contentPanel.add(8x8);  //Then the chessboard


//Create empty placeholder for bottom-right corner
JPanel empty = new JPanel();
contentPanel.add(empty);
//finally add bottom markers
contentPanel.add(2x1);

But the result is even worse: enter image description here

So how do I use those drunk grid layouts? Or should I start differently?

Edit: Using gridBaglayout:

 contentPanel.setLayout(new GridBagLayout());
 /**Follows as after contentPanel.setLayout(new GridLayout(2, 2)); in the prewious code**/

Result:enter image description here

After playing with it a little:

/*VERTICAL MARKERS*/
//set vertical fill
c.fill = GridBagConstraints.VERTICAL;
//Set current possition to [0,0] in the grid 
c.gridx = 0;
c.gridy = 0;
//Add them to panel
contentPanel.add(indexysvis, c);

/*CHESSBOARD*/
//set vertical fill to both horizontal and vertical
c.fill = GridBagConstraints.BOTH;
//Set current possition to [1,0] in the grid 
c.gridx = 1;
c.gridy = 0;
contentPanel.add(sachovnice, c);

/*HORIZONTAL MARKERS*/
//set vertical fill to both horizontal
c.fill = GridBagConstraints.HORIZONTAL;
//Set current possition to [1,0] in the grid 
c.gridx = 1;
c.gridy = 1;
contentPanel.add(indexyvodo,c);

It won't try to fit the window:

enter image description here

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778

3 Answers3

4

You can use a 9x9 grid with conditional check. That will make things simpler and better too. A possible generation of such layout with conditional checking might be as following:

GridLayout layout = new GridLayout(0,9);
        jPanel1.setLayout(layout);

        for(int row =0; row < 9; row++)
            for(int col=0; col<9;col++)
            {
                 JButton button = new JButton(); 
                if(col==0 && row==8)
                {    button.setEnabled(false);
                     button.setContentAreaFilled(false);
                }
                else if(col==0)
                    button.setText(""+(char)('A'+row));
                else if(row == 8)
                   button.setText(""+(char)('A'+col - 1)); 
                else if(col%2==0)
                    button.setBackground(Color.white);
                else     button.setBackground(Color.black);

                jPanel1.add(button);
            }

It will have an output like:

enter image description here

Sage
  • 15,290
  • 3
  • 33
  • 38
  • Niiice.. Do you use a screenshot tool? If so, which one? – Andrew Thompson Nov 29 '13 at 14:21
  • 2
    I have built a Giffer lib. After creating the jframe instance `JFrame` I just pass the `jframe` instance to the program, it takes the shot with defined time elapse and upon closing the app, it writes the Giff with the shots it took in the application life cycle. I will share it with all of you after performing some more test ;) – Sage Nov 29 '13 at 14:26
  • And obviously, i took advantage of the `Robot` class and it's `createScreenCapture(rectangle)` function ;) – Sage Nov 29 '13 at 14:30
  • Great idea that, passing the frame as a ref. As far as sharing the code goes, it might be a bit tricky for 2 reasons. 1) The forum limit for code seems to be running very close at around 650 LOC. 2) The forum is not designed for 'here is some nice code'. (Which I personally think is a pity.) I've had one example [moved to code review](http://codereview.stackexchange.com/q/4446/7784), while another [attracted criticism](http://stackoverflow.com/q/18858312/418556). -- I guess one way to 'sneak it under the radar' is to put it in code review. – Andrew Thompson Nov 29 '13 at 14:33
  • *"..took advantage of the `Robot` class"* Also consider variants like as shown [here](http://stackoverflow.com/a/5853992/418556). It will only capture the content pane, which in some cases is better. – Andrew Thompson Nov 29 '13 at 14:35
  • I know the use of the nice idea(+1) you have proposed on the linked post. Actually when i programmed the library one month before i used the idea mentioned by you first. And this approach is still part of the library. It specially helps when we want to capture an animation of specific component. So basically my Giff library has both approach. One to capture the whole `JFrame` and other one mentioned by you to capture a specific component. :) ;) – Sage Nov 29 '13 at 14:40
  • *"So basically my Giff library has both approach."* -- "We have both kinds. Country ***&*** Western" (with apologies to The Blues Brothers) Choices are wonderful. :) – Andrew Thompson Nov 29 '13 at 14:43
  • I've actually hoped I won't have to merge `.createLabels` and `.createChessboard`, but if you believe there's no other way, I'm gonna do it like you say. By the way, would you mind sharing that screenshot code somehow? That would be very usefull for me in the future. – Tomáš Zato Nov 29 '13 at 14:57
  • @Sage: You can put your Giffer on a blog. Your blog, Andrew's blog, or my blog. The you can drop the link like dandelion seeds on Stack Overflow. – Gilbert Le Blanc Nov 29 '13 at 15:10
  • On more thing: Why is it `GridLayout(` **`0`** `,9)`? – Tomáš Zato Nov 29 '13 at 15:12
  • @TomášZato, it is just to liberate the `GridLayout` to increase number of rows by itself if needed. – Sage Nov 29 '13 at 16:15
  • @GilbertLeBlanc, I will do, surely. Give me this week to give it a finishing touch. I wanted to optimize it to some level. And then i will share it with all of you for reviewing :) – Sage Nov 29 '13 at 16:17
0

Set gridBagLayout to the contentPane also. This will place your panels in desired positions. enter image description here

enter image description here

Prasad
  • 1,188
  • 3
  • 11
  • 29
0

Either go for one panel with 9x9 (the numbers would be as big as each field) or use a GridBagLayout, which would give you complete control. That said, GridBagLayout is a complete PITA. Perhaps TableLayout is more to your liking.

Hendrik
  • 5,085
  • 24
  • 56