0

I want to build a 2d array of toggle buttons in Java that when I click on a button, it passes from green to red, how can I do this? The following code creates the array of buttons.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Container implements ActionListener {  

    private static JFrame container;
    private static JToggleButton[][] butoes;    

    public static void main(String args[]) {

        container = new JFrame("Game of Life");
        butoes = new JToggleButton[20][20];
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                butoes[i][j] = new JToggleButton();
            }
        }

        container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        container.setLayout(new GridLayout(20, 20));
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                container.add(butoes[i][j]);
            }
        }
        container.pack();
        container.setSize(700, 700);
        container.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent evento) {
    }
}
Line
  • 1,529
  • 3
  • 18
  • 42
Mr.Castro
  • 45
  • 1
  • 6

2 Answers2

4
UIManager.put("ToggleButton.select", yourColorHere);

From

https://community.oracle.com/thread/1485709?start=0&tstart=0

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • I know the op is only asking a simple question, but it's a little like trying to hammer in nail with a small moon and has issues from within a large context. So if we look at from the context the op has provided, then it'll probably work, but if they have other views which use toggle buttons, this won't work, generally, only hit UIManager if you want every thing to change – MadProgrammer May 02 '16 at 20:39
  • That's nice, doesn't change the fact that it doesn't work :P – MadProgrammer May 02 '16 at 21:00
  • This answer does work for me for setting the **selected** color (when replacing `yourColorHere` with a color, of course; `UIManager.put("ToggleButton.select", Color.RED);`). – Ironcache May 03 '16 at 04:11
4

ControlAltDel's answer is correct, and should likely be used as the solution for this specific question in this context. However, being general, there are a couple things here. First, when it comes to reacting to GUI changes in Swing, you're going to want to familiarize yourself with the listener paradigm it uses. This paradigm allows you to react to (almost) any GUI event you want, and modify your program appropriately.

However, in this case, that's tangential knowledge (I'm leaving it in the answer because it's still very important to know when dealing with Swing UIs though). The problem is that the selected color is only exposed through the UI of the button, so, in order to update it, you would need to change the default UI associated with this specific button (there are alternative approaches, but this is one of the easier ones).

This does seem more cumbersome than just updating the UIManager, as ControlAltDel proposed. But it's also important to note that changes to core properties in the UIManager are global (as MadProgrammer eluded to). If you update the selected toggle button color in the UIManager, you're updating it for every instance of JToggleButton, which, while in this specific scenario might not be a bad thing, is definitely something to keep in mind in practice.

Taking all of this into consideration, one potential fix would be to replace this:

butoes[i][j] = new JToggleButton();

With something like this:

JToggleButton b = new JToggleButton();
b.setBackground(Color.GREEN);
b.setUI(new MetalToggleButtonUI() {
    @Override
    protected Color getSelectColor() {
        return Color.RED;
    }
});
butoes[i][j] = b;

Recommend checking out this lesson on the Oracle site, the javadoc for JToggleButton, and this other SO question.

Community
  • 1
  • 1
Ironcache
  • 1,719
  • 21
  • 33
  • **did not work , I tried to put all the toggle buttons green and also didnt work .** – Mr.Castro May 02 '16 at 21:37
  • Apologies; I wasn't at a dev environment when I posted that. There are two problems. First, it should have read `setBackground()` instead of `setForeground()`. Second, the color of the selected button actually has nothing to do with the background color (this was forgetful on my end). There are numerous workarounds for this (again, using ControlAltDel's solution should work), but I'll updated my answer shortly to include an alternative which is to override the UI. – Ironcache May 03 '16 at 03:48