Home > database >  I can't modify my JButton after having initialized it in the JPanel
I can't modify my JButton after having initialized it in the JPanel

Time:12-31

import javax.swing.;
import java.awt.;

public class Space extends JFrame {
   

    private JButton[][] jb;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Space frame = new Space();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Space() {
         JPanel contentPane=new JPanel();
         contentPane.setLayout(new BorderLayout(0, 0));
         JPanel p=new JPanel();
         contentPane.add(p);

         setContentPane(contentPane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        p.setLayout(new GridLayout(20,20));
        jb=new JButton[20][20];
        for(int i=0;i<jb.length;i  ) {
            for(int j=0;j<jb[i].length;j  ) {
                jb[i][j]=new JButton();
                jb[i][j].setIcon(new ImageIcon(getClass().getResource("i1.jpg")));
                p.add(jb[i][j]);
            }
        }
        JButton b=new JButton();
        b.setIcon(new ImageIcon(getClass().getResource("i2.jpg")));
        jb[10][10]=b; // when i set this cell, when i run, the icon of jb[10][10] didn't modified



    }

}

i tried that to modify my button jb[10][10] but when i run all button have the same icon, i want that the button share all values of b the icon too

i created a 20x20 matrice of button using gridlayout . And the problem is at the bottom, when i want to modify the button already initialized..

CodePudding user response:

Regarding,

I can't modify my button ...

Yes, you can, but that is not what your code is trying to do. Inside your nested for loop, you have this line:

p.add(jb[i][j]);

which adds a JButton object that has already been created and placed in the jb array. So the JButtons in the JPanel are the same ones that have been added to the 2D array.

But later, change the object that the array element refers to here:

jb[10][10]=b;

When you do this, the JButton in the array at position [10][10] is no longer the same object, and so you are not "modifying the JButton" but rather are swapping references in one object that holds references to the JButton, the 2D array, but not in the other object that holds references to the buttons, the JPanel. So now the array and the JPanel have discordant references at that position.

Instead, to do what you desire, you must explicitly change the state of the button that already exists and is already held by both the JPanel and the array:

// *** not this ***
// JButton b=new JButton();
// b.setIcon(new ImageIcon(getClass().getResource("i2.jpg")));
// jb[10][10]=b;

// *** but instead THIS ***
Icon icon = new ImageIcon(getClass().getResource("i2.jpg"));
jb[10][10].setIcon(icon); // change the state, not the reference
  • Related