0

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

TT.
  • 15,774
  • 6
  • 47
  • 88
MightyMike
  • 34
  • 5
  • *My task is to create checked board on JPAnel* - what is a checked board? Do you mean a chess board with alternating coloured squares? See: https://stackoverflow.com/questions/6811247/drawing-in-jlayeredpane-over-exising-jpanels/6811800#6811800 for a basic example. – camickr Nov 15 '19 at 01:00
  • @camickr checked means board splited into squares with no color – MightyMike Nov 15 '19 at 03:45
  • Still don't know what that means. What is a "board". There is no Swing component with that name. There is nothing special about creating a "grid". All you need is a JPanel with the GridLayout. You specify the number of columns you want in the grid and use 0 for the rows. Then you create a loop for the number of elements you want to add to the panel. The components will automatically wrap to the next row when the column size is reached. – camickr Nov 15 '19 at 04:38
  • So first create an example with just the frame and your panel with the GridLayout and child panels. Once this works then you worry about using the split pane. If you have problems then post a proper [mre] demonstrating the problem. An "MRE" means we can copy/paste/compile and test the code to see the problem you are describing. – camickr Nov 15 '19 at 04:39

1 Answers1

1

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.

Evan Jones
  • 876
  • 4
  • 9
  • EXACTLY!!! frame.revalidate(); - made it all work, you see I didn't know that rule and thus was strugling.....this is what I get (just if you are interested) http://prntscr.com/pxhxt8 I think now I need to get rid of those white margin between columns and that would be it. – MightyMike Nov 15 '19 at 17:26