3

How to add JLabel out of the GridLayout? I have an 8x8 grid layout.

Container content = getContentPane();
content.setLayout(new GridLayout(8, 8,2,2));
for (int f = 0; f < btnArr.length; f++){
    for (int s = 0; s < btnArr.length; s++){
        btnArr[f][s] = new JButton();
        btnArr[f][s].addActionListener(this);
        content.add(btnArr[f][s]);
        btnArr[f][s].setBackground(randomColor());
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gorakh_sth
  • 171
  • 2
  • 3
  • 10
  • Yes I want the 8x8 grid of buttons as well as another label added outside the buttons.. – Gorakh_sth Jan 26 '13 at 13:44
  • 1
    The trick to this then, would be to put (a panel with) the buttons into a larger layout/panel. This is known as a [nested layout](http://stackoverflow.com/questions/5621338/how-to-add-jtable-in-jpanel/5630271#5630271). Have a look over the code for that answer and have a go at it yourself. – Andrew Thompson Jan 26 '13 at 13:48
  • I was try it but I can't solve it .... I have to solve this problem as soon please help me. – Gorakh_sth Jan 26 '13 at 14:11
  • @AndrewThompson can't we just manually set `JLabel` it with the help of `setLocation()`? – joey rohan Jan 26 '13 at 14:26
  • Would you provide me the simple example of nested layout ? – Gorakh_sth Jan 26 '13 at 14:54
  • 1
    For [example](http://stackoverflow.com/a/5137250/230513). – trashgod Jan 26 '13 at 15:02
  • @joeyrohan `setLocation()` will typically be ignored by a layout manager. So ..no. But wait, let me reconsi.. **no.** – Andrew Thompson Jan 30 '13 at 14:33
  • @AndrewThompson yeah right, till the set Layout is != `.setLayout(null)` – joey rohan Jan 30 '13 at 15:01
  • @joeyrohan re. `null` layout. Abandon all hope, ye' you enter there! Sizing and positioning components across different OS', PLAFs, screen resolutions, (...) for a resizable GUI is such complex logic, that you should encapsulate it in a custom layout manager. See [setLayout(null) is never necessary. Ever!](https://forums.oracle.com/forums/thread.jspa?threadID=1351374) for more details on my take on the matter. – Andrew Thompson Jan 30 '13 at 16:17
  • @AndrewThompson Woha! Thanks alot! It really opened my eyes! :) – joey rohan Jan 30 '13 at 17:27

1 Answers1

9

SimpleNestedLayout

import java.awt.*;
import javax.swing.*;

class SimpleNestedLayout {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(5,5));

                int sz = 4;
                Container content = new JPanel(new GridLayout(sz, 0, 2, 2));
                for (int f=0; f<sz*sz; f++) {
                    content.add(new JButton());
                }
                gui.add(content, BorderLayout.CENTER);

                Container info = new JPanel(
                        new FlowLayout(FlowLayout.CENTER, 50, 5));
                info.add(new JLabel("Flow"));
                info.add(new JLabel("Layout"));
                gui.add(info, BorderLayout.PAGE_START);

                gui.add(new JLabel("Label"), BorderLayout.LINE_END);

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Notes

  • For an 8x8 grid, change sz to 8.
  • If the 'label' mentioned is like the label seen in the GUI, it might go in the outer BorderLayout where Flow Layout (itself a panel) or Label appear as well as either of two other vacant positions in the outermost gui panel.
  • Both info (FlowLayout) & content (GridLayout) panels can also accept more components as needed.
  • Simple examples of other nested layouts.
    1. PlayerGui (31 LOC)
    2. WestPanel (30 LOC) not a great example as it extends JPanel instead of simply keeping an instance, but short.
    3. AmortizationLayout (53 LOC) especially nice as an example as it outlines the parent & child layouts using a titled border.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433