MightyMike
MightyMike

Reputation: 34

Make a grid layout displaying on JPanel

My task is to create checked board on JPAnel. For that purpose I'm trying to fill in parent JPanel with JPanels that has borders, but for some reason code doesnt give desired result and no error shown to do investigation why. Here is the code:

private static class GlassView extends JFrame {

        private static int width = 600;
        private static int height = 750;

        public GlassView() {
            this.setSize(width, height);
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        }

        public static void workingFrame() {
            int cols = 0;
            int rows = 0;
            String frameName = "Bot World";

            WorkFrame workF = new WorkFrame(0, 0, frameName);
            wfFrame = workF.newFrame();
            wfFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            wfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            wfFrame.setVisible(true);

            JSplitPane splitPane = new JSplitPane();
            splitPane.setSize(width, height);
            splitPane.setDividerSize(0);
            splitPane.setDividerLocation(150);
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);

            JPanel panelLeft = createLftPanel();
            JPanel panelRight = createRightPanel();

            splitPane.setLeftComponent(panelLeft);
            splitPane.setRightComponent(panelRight);
            wfFrame.add(splitPane);
        }
    }

Here is the code for the panelRight which needs to be cheked:

public static JPanel createRightPanel() {
        JPanel panel = new JPanel();
        int rows = 100;
        int cols = 100;
        panel.setLayout(new GridLayout(rows, cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                JPanel pane = new JPanel();
                pane.add(new JTextField("both"));
                pane.setBorder(BorderFactory.createLineBorder(Color.black));
                panel.add(new JButton(""));
            }
        }
        return panel;
    }

Any help will be appreciated. Thank you

Upvotes: 0

Views: 641

Answers (1)

Evan Jones
Evan Jones

Reputation: 886

Ok, my (second) guess is that the frame isn't laid out again after the call to

wfFrame.setVisible(true);

For me the example below:

public class Framed {
    public static void workingFrame() {
        JFrame frame = new JFrame();
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.getContentPane().add(createRightPanel(10, 10));

        frame.revalidate(); // <-- HERE
    }

    public static JPanel createRightPanel(int rows, int cols) {
        JPanel panel = new JPanel(new GridLayout(rows, cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                JPanel pane = new JPanel();
                pane.setBackground((i+j)%2==0?Color.black:Color.white);
                panel.add(pane);
            }
        }
        return panel;
    }

    public static void main(String... none) throws Exception {
        workingFrame();
    }
}

Shows a checker grid, but is you remove the call to

        frame.revalidate(); // <-- HERE

Then the gird is not displayed (until you do something with the frame that causes it to lay out again). Better than calling revalidate() though may be to call setVisible only after all the components have been added.

Upvotes: 1

Related Questions