-1

I have googled for a while now but I don't find what I'm looking for. I'm trying to build a simple GUI where my contentPane contains a JPanel with a GridLayout.

I want a grid of say 3x3 and fill the bottom right cell with a green-background-jpanel. The rest of the cells are supposed to be white for now. How do I achieve this?

I know that I could for-loop and fill all other cells with white-background-jpanels. But that's not feasible for the rest of the project. What's the proper / non-hackerish way to do this? I just want a grid where I can say "Look, it's n x m and for now I only want to fill only e.g. row 3 column 4"

ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
user3182532
  • 1,007
  • 5
  • 20
  • 35
  • 4
    Do you have `JPanels` in all the cells? You can have an `Array` of dimension `n x m` of `JPanel` which are to be added to the `GridLayout` and then you can directly access the `grid[n][m]`th `JPanel` and set its color. – Sanket Makani May 30 '17 at 08:19
  • It sounds pretty straightforward in my ears. What have you tried? Show us your code! In what way does it fail? – Ole V.V. May 30 '17 at 08:41
  • 1
    It seems to me the question is how your code is to determine the number of rows and columns so it can use the right indices for the bottom right cell. How to do that, we cannot tell without the context. Maybe you can [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? – Ole V.V. May 30 '17 at 08:44
  • 1
    1) See [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). It uses logic to color the components (in this case buttons) alternating dark and light. In your case, you could ..change the logic inside the loop to get the required colors. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) of your best attempt. 3) Voting to close. – Andrew Thompson May 30 '17 at 08:46

1 Answers1

1

Okay I got it :)) Thank you Sanket Makani, I followed your idea! Now I can modify whatever field I want to inside my grid. Here's the full code example:

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


public class Test {

    public static void main(String[] args) {

        // frame
        JFrame frame = new JFrame("Pedigree Builder");
        frame.setPreferredSize(new Dimension(400, 300));

        // menu bar
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        // "file" menu
        JMenu fileMenu = new JMenu("File");
        menubar.add(fileMenu);

        // exit button for "file" menu
        JMenuItem exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });
        fileMenu.add(exitMenuItem);

        // content pane = grid
        JPanel contentPane = new JPanel(new GridLayout(0, 3));
        frame.setContentPane(contentPane);

        // init array of fields for the grid
        JPanel[] fieldArray = new JPanel[9];
        for (int i = 0; i < fieldArray.length; i++){
            fieldArray[i] = new JPanel();
            contentPane.add(fieldArray[i]);
        }

        // modify content of particular cell (in this case: bottom right)
        fieldArray[8].setBackground(Color.green);

        frame.pack();
        frame.setVisible(true);

    }

}
user3182532
  • 1,007
  • 5
  • 20
  • 35