Devikn Devikn - 1 year ago 203
Java Question

How to create a JButton Array with existing JButtons in Java?

I'm doing a color pencil project using 100 JButtons as pixels positioned like a 10x10 matrix. I also have 10 other jButtons representing colors and 2 other representing the tools "pencil" and "bucket".

Im only working with the pencil jButton for now, so you can paint any of the 100 JButtons by clicking the pencil JButton and then picking one of the color JButtons.

The algorithm works fine, the problem is the fact I need to apply the same coloring method (colorButton) to all the 100 JButtons so i wanted to make an Array to store all the JButtons and then call my colorButton method for each of them.

I have no clue on how to store all my 100 JButtons into the JButton array, since they are already created and named.

This is what im trying:

public void colorButton(JButton button){
if (type == "pencil"){
button.setBackground( new Color(101,101,101));
}else if(color.equals("white")){
button.setBackground( new Color(255,255,255));
}else if(color.equals("black")){
button.setBackground( new Color(0,0,0));
}else if(color.equals("blue")){
button.setBackground( new Color(0,0,255));
}else if(color.equals("red")){
button.setBackground( new Color(255,0,0));

public void buttonArray(){
JButton[] button = new JButton[100];

for (int i = 0; i < 100; i++){
button[i] = jButton1; //I need to get each of the 100 buttons here

So other than only JButton1 i need a way to store all of the 100.

Any idea?


*Edited to clarify the question and situation

Answer Source

Without knowing the context of what this is used for and assuming the colorButton() method is a mock as it is missing a couple of braces.

The following Java code uses reflection to populate an ArrayList using the existing JButtons defined in the ColorButtons class.

I am still unsure why you need to assign the Array in a loop with another List, but here it is.

public class ColorButtons {
    // JButton sample.
    private JButton button1 = new JButton("1");
    private JButton button2 = new JButton("2");
    private JButton button3 = new JButton("3");

    // This is used to store the buttons.
    ArrayList<JButton> jbuttons = new ArrayList<JButton>();

    // Boilerplate, as I have no idea what this does.
    private String type = "pencil";
    private String color = "white";

     * Populate the JButton List on instantiation.
     * @see ColorButtons#populateJButtonList()
    public ColorButtons() {
        // Populate "jbuttons" ArrayList with JButtons.

    public void colorButton(JButton button) {
        if (type == "pencil") {
            if (color == "gray") {
                button.setBackground(new Color(101, 101, 101));
            } else if (color == "white") {
                button.setBackground(new Color(255, 255, 255));
            } else if (color == "black") {
                button.setBackground(new Color(0, 0, 0));
            } else if (color == "blue") {
                button.setBackground(new Color(0, 0, 255));
            } else if (color == "red") {
                button.setBackground(new Color(255, 0, 0));

    public void buttonArray() {
        JButton[] button = new JButton[100];

        for (int i = 0; i < 100; i++) {
            // Assign each JButton in the list to array element.
            for (JButton jbutton : jbuttons) {
                button[i] = jbutton; // I need to get each of the 100 buttons
                                        // here
                System.out.println("Button" + button[i].getText());

     * This is used to add the JButtons to a list using reflection. Used in the
     * constructor.
     * @see ColorButtons#ColorButtons()
    public void populateJButtonList() {
        // Gets the class attributes, e.g. JButton, String, Integer types, everything.
        // In this case it is this class, but can define any other class in your project.
        Field[] fields = ColorButtons.class.getDeclaredFields();

        // Loop over each field to determine if it is a JButton.
        for (Field field : fields) {

            // If it is a JButton then add it to the list.
            if (field.getType().equals(JButton.class)) {
                try {
                    // De-reference the field from the object (ColorButtons) and cast it to a JButton and add it to the list.
                    jbuttons.add((JButton) field.get(this));
                } catch (IllegalArgumentException | IllegalAccessException
                        | SecurityException e) {

    public static void main(String... args) {
        ColorButtons color = new ColorButtons();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download