#How do I make a button turn green in this specific scenario?

24 messages · Page 1 of 1 (latest)

old zealot
#
JPanel[][] sarr = new JPanel[mr][mc];
    int[][] num = new int[11][25];

    SeatPage() {
        ImageIcon a = new ImageIcon("Seat.png");
        GridLayout gl = new GridLayout(mr, mc, 10, 10);

        JPanel jp=new JPanel();
        jp.setBackground(Color.BLACK);
        jp.setLayout(gl);
        String l="";
        int lc=0;int nc=0;
        JFrame frame = new JFrame();
        for (int i = 0; i < mr; i++) {
            l = (char) ('A' + (lc)) + "";
            for (int j = 0; j < mc; j++) {
                sarr[i][j] = new JPanel();
                sarr[i][j].setBackground(Color.BLACK);

                if(i==0 || j==0 || i==mr-1 || j==mc-1 || j==6 || j==22 || i==2 || i==11) {
                    jp.add(sarr[i][j]);
                    continue;
                }
                nc++;

                JButton b = new JButton(l+nc, a);
                b.setBackground(Color.white);
                b.setHorizontalTextPosition(JButton.CENTER);
                b.setVerticalTextPosition(JButton.CENTER);
                b.setFocusable(false);
                b.setBorder(BorderFactory.createLineBorder(Color.black, 5));
                b.addActionListener(this);
                sarr[i][j].add(b);
                jp.add(sarr[i][j]);
            }
            if(!(i==0 || i==mr-1 || i==2 || i==11))
                lc++;
            nc=0;
        }
        System.out.println(l+lc);

        frame.setIconImage(new ImageIcon("logo.png").getImage());
        frame.setExtendedState(SeatPage.MAXIMIZED_BOTH);
        frame.add(jp);
        frame.setForeground(Color.BLACK);
        frame.setVisible(true);
    }

So... the button is in a JPanel, which is in a GridLayout of another JPanel... which is in the JFrame. I am not sure if I summarized this correctly
I am not particularly familiar with awt or swing... I am just fixing this code from a friend, and it has been shortened a lot...

red hazelBOT
#

This post has been reserved for your question.

Hey @old zealot! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

stable hare
#

I think you need to set your button's background colour

#
b.setBackground(Color.GREEN);
old zealot
#

in the actionPerformed() overidden method right?

#

When I am clicking a button the e.getSource() should be that button

old zealot
stable hare
#

isn't b still the only JButton you add?

old zealot
#

Well, here's another block:

#
@Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < mr; i++) {
            for (int j = 0; j < mc; j++) {
                if (e.getSource() == sarr[i][j]) {
                    System.out.println("fv");
                    if(((JButton)sarr[i][j].getComponent(0)).getBackground().equals(Color.white))
                        num[i][j]=1;
                     else if (((JButton)sarr[i][j].getComponent(0)).getBackground().equals(Color.green))
                        num[i][j]=0;

                    for(int x=0;x<11;x++) {
                        for(int y=0;y<25;y++) {
                            JButton t =  (JButton) sarr[x][y].getComponent(0);
                            if(num[x][y]==1)
                                t.setBackground(Color.green);
                            else
                                t.setBackground(Color.white);
                        }
                    }
                    break;
                }
            }
        }
    }
#

The sarr array is of JPanel type; The JButton is part of the JPanel

#

Ah, should have posted my question properly

#

e.getSource() == sarr[i][j].getComponent(0)

#

THe problem is this line specifically

#

It says there is no child

#

When there should be one, the JButton

stable hare
#

e.getSource() is null or what?

old zealot
#

Here's the error(give me a min)

stable hare
#

sorry I'm not really an expert in Java GUI

old zealot
#
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 0
    at java.desktop/java.awt.Container.getComponent(Container.java:354)
    at SeatPage.actionPerformed(SeatPage.java:64)
old zealot
#

This used to be 200 lines

red hazelBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.