I'm writing a simple paint program in Java Swing and i'm hold by a problem of creating a grid of color buttons. I did it by creating a JPanel at the top of the window. this is what it looks like right now
I'm adding the buttons this way:
for (String name : colors.keySet()) {
ColorAction action = new ColorAction(name, colors.get(name));
// above is my class that handles action of clicking button
BufferedImage image = loadButtonImage(name);
JButton button = new JButton (new ImageIcon(image));
button.addActionListener(action);
button.setContentAreaFilled(false);
add(button);
}
Instead of that I want it to look like a grid of these squares fitting close to each other and each square would be a color button. Right now, when I click somewhere near the color square I'm actually clicking a button and it looks awful. I'm using the GridLayout and I know that it extends all the buttons to cover the whole panel by default and it causes the problem.
Would there be a simple way to achieve my goal other than complicated code using GridBagLayout?