Results 1 to 6 of 6
Like Tree3Likes
  • 3 Post By gimbal2

Thread: JButton color won't change no matter what

  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default JButton color won't change no matter what

    I made a simple GUI consisting of 25 buttons to make the game Lights Out. To explain how it's played, you have a 5 x 5 grid of buttons that light up a button and all adjacent buttons when pressed. If a button is already lit up, it's unlit, and the goal is to light up all the buttons. To simplify my version of it, the buttons just swap colors between red and blue, starting with red. The problem is, two of my buttons won't change color at all. They change the other buttons when clicked, but its own color won't change, even with other button clicks. I really don't know how else I could explain the problem. The buttons are button 8 and 15.
    Sorry for the long code. I'm still new to GUI development and I'm also pretty lazy.
    Java Code:
    import java.awt.event.*; 
    import java.awt.*; 
    import java.util.*; 
    import javax.swing.*; 
    @SuppressWarnings("unused") 
    public class LightsOut implements ActionListener {
    	
        static int bColor[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1 -1, -1, -1, -1, -1, -1, -1, -1 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
        static int moves = 0;
        JPanel title, buttons; 
        static JButton button[] = new JButton[25]; 
        static int x = 0, y = 0;
        
        public JPanel createContentPane(){
        	 JPanel totalGUI = new JPanel(); 
             totalGUI.setLayout(null);
               
             buttons = new JPanel(); 
             buttons.setLayout(null); 
             buttons.setLocation(10, 10); 
             buttons.setSize(500, 500);
             totalGUI.add(buttons);
             
             for(int i = 0; i < button.length; i++){
            	 button[i] = new JButton("" + i);
            	 button[i].setSize(50, 50);
            	 button[i].addActionListener(this);
            	 button[i].setBackground(Color.RED);
            	 button[i].setBorderPainted(false);
            	 button[i].setBorder(null);
            	 button[i].setFocusable(false);
             }
             
           	 button[0].setLocation(0, 0);
        	 button[1].setLocation(100, 0);
        	 button[2].setLocation(200, 0);
        	 button[3].setLocation(300, 0);
        	 button[4].setLocation(400, 0);
        	 button[5].setLocation(0, 100);
        	 button[6].setLocation(100, 100);
        	 button[7].setLocation(200, 100);
        	 button[8].setLocation(300, 100);
        	 button[9].setLocation(400, 100);
        	 button[10].setLocation(0, 200);
        	 button[11].setLocation(100, 200);
        	 button[12].setLocation(200, 200);
        	 button[13].setLocation(300, 200);
        	 button[14].setLocation(400, 200);
        	 button[15].setLocation(0, 300);
        	 button[16].setLocation(100, 300);
        	 button[17].setLocation(200, 300);
        	 button[18].setLocation(300, 300);
        	 button[19].setLocation(400, 300);
        	 button[20].setLocation(0, 400);
        	 button[21].setLocation(100, 400);
        	 button[22].setLocation(200, 400);
        	 button[23].setLocation(300, 400);
        	 button[24].setLocation(400, 400);
        	 
        	 for(int i = 0; i < button.length; i++){
        		 buttons.add(button[i]);
        	 }
             totalGUI.setOpaque(true);
             return totalGUI;
        }
        public void actionPerformed(ActionEvent e){
        		if(e.getSource() == button[0]){
        			cornerSwitch(0);
        		}else if(e.getSource() == button[1]){
        			wallSwitch(1);
        		}else if(e.getSource() == button[2]){
        			wallSwitch(2);
        		}else if(e.getSource() == button[3]){
        			wallSwitch(3);
        		}else if(e.getSource() == button[4]){
        			cornerSwitch(4);
        		}else if(e.getSource() == button[5]){
        			wallSwitch(5);
        		}else if(e.getSource() == button[6]){
        			centerSwitch(6);
        		}else if(e.getSource() == button[7]){
        			centerSwitch(7);
        		}else if(e.getSource() == button[8]){
        			centerSwitch(8);
        		}else if(e.getSource() == button[9]){
        			wallSwitch(9);
        		}else if(e.getSource() == button[10]){
        			wallSwitch(10);
        		}else if(e.getSource() == button[11]){
        			centerSwitch(11);
        		}else if(e.getSource() == button[12]){
        			centerSwitch(12);
        		}else if(e.getSource() == button[13]){
        			centerSwitch(13);
        		}else if(e.getSource() == button[14]){
        			wallSwitch(14);
        		}else if(e.getSource() == button[15]){
        			wallSwitch(15);
        		}else if(e.getSource() == button[16]){
        			centerSwitch(16);
        		}else if(e.getSource() == button[17]){
        			centerSwitch(17);
        		}else if(e.getSource() == button[18]){
        			centerSwitch(18);
        		}else if(e.getSource() == button[19]){
        			wallSwitch(19);
        		}else if(e.getSource() == button[20]){
        			cornerSwitch(20);
        		}else if(e.getSource() == button[21]){
        			wallSwitch(21);
        		}else if(e.getSource() == button[22]){
        			wallSwitch(22);
        		}else if(e.getSource() == button[23]){
        			wallSwitch(23);
        		}else if(e.getSource() == button[24]){
        			cornerSwitch(24);
        		}
        }
        public static void centerSwitch(int n){
        	bColor[n] = bColor[n] * -1;
        	bColor[n - 1] = bColor[n - 1] * -1;
        	bColor[n + 1] = bColor[n + 1] * -1;
        	bColor[n - 5] = bColor[n - 5] * -1;
        	bColor[n + 5] = bColor[n + 5] * -1;
        	
        	for(int i = 0; i < bColor.length; i++){
        		if(bColor[i] == 1){
        			button[i].setBackground(Color.BLUE);
        		}else if(bColor[i] == -1){
        			button[i].setBackground(Color.RED);
        		}
        	}
        }
        public static void wallSwitch(int n){
        	if(n == 1 || n == 2 || n == 3){
            	bColor[n] = bColor[n] * -1;
            	bColor[n - 1] = bColor[n - 1] * -1;
            	bColor[n + 5] = bColor[n + 5] * -1;
            	bColor[n + 1] = bColor[n + 1] * -1;
            	
        	}else if(n == 5 || n== 10 || n== 15){
            	bColor[n] = bColor[n] * -1;
            	bColor[n + 1] = bColor[n + 1] * -1;
            	bColor[n + 5] = bColor[n + 5] * -1;
            	bColor[n - 5] = bColor[n - 5] * -1;
            	
        	}else if(n== 9 || n== 14 || n== 19){
            	bColor[n] = bColor[n] * -1;
            	bColor[n - 1] = bColor[n - 1] * -1;
            	bColor[n - 5] = bColor[n - 5] * -1;
            	bColor[n + 5] = bColor[n + 5] * -1;
            	
        	}else if(n == 21 || n == 22 || n == 23){
            	bColor[n] = bColor[n] * -1;
            	bColor[n - 1] = bColor[n - 1] * -1;
            	bColor[n + 1] = bColor[n + 1] * -1;
            	bColor[n - 5] = bColor[n - 5] * -1;
            	
        	}
        	for(int i = 0; i < bColor.length; i++){
            		if(bColor[i] == 1){
            			button[i].setBackground(Color.BLUE);
            		}else if(bColor[i] == -1){
            			button[i].setBackground(Color.RED);
            		}
            	}
        }
        public static void cornerSwitch(int n){
        	bColor[n] = bColor[n] * -1;
        	if(n == 0){
        		bColor[n + 1] = bColor[n + 1] * -1;
        		bColor[n + 5] = bColor[n + 5] * -1;
        	}else if(n == 4){
        		bColor[n - 1] = bColor[n - 1] * -1;
        		bColor[n + 5] = bColor[n + 5] * -1;
        	}else if(n == 20){
        		bColor[n + 1] = bColor[n + 1] * -1;
        		bColor[n - 5] = bColor[n - 5] * -1;
        	}else if(n == 24){
        		bColor[n - 1] = bColor[n - 1] * -1;
        		bColor[n - 5] = bColor[n - 5] * -1;
        	}
        	
        	for(int i = 0; i < bColor.length; i++){
        		if(bColor[i] == 1){
        			button[i].setBackground(Color.BLUE);
        		}else if(bColor[i] == -1){
        			button[i].setBackground(Color.RED);
        		}
        	}
        }
        public static void createAndShowGUI(){
        	JFrame.setDefaultLookAndFeelDecorated(false);
        	JFrame frame = new JFrame("Lights Out");
        	
        	LightsOut game = new LightsOut();
        	frame.setContentPane(game.createContentPane());
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setSize(500, 500);
        	frame.setResizable(false);
        	frame.setVisible(true);
        }
    	public static void main(String[] args) {
    		createAndShowGUI();
    	}
    
    }
    Essentially, every button can have an integer value of 1 or -1, starting with -1. A click on that button will multiply it by -1, so it constantly can switch between those two numbers/states. The method for each button determines the new value for that button as well as any adjacent buttons. A loop in each method then checks through every button's integer value and changes the color based on that value. I have absolutely no idea why those buttons go wrong, there's nothing that singles them out. Thanks in advance for any help.
    I am not a clever man

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: JButton color won't change no matter what

    One thing that I find odd is that your bColor array is in fact 27 in length and not 25 (Yes I counted!). If I believe cornerSwitch() method, the assumption is that they are of equal length.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: JButton color won't change no matter what

    Yeah, it's weird. I changed bColor, so it's initialized in the main method in a loop, and changed the methods so they use the length of button, and it suddenly works just fine now. But thanks

    Java Code:
    import java.awt.event.*; 
    import java.awt.*; 
    import java.util.*; 
    import javax.swing.*; 
    @SuppressWarnings("unused") 
    public class LightsOut implements ActionListener {
    	
        static int bColor[] = new int[27];
        static int moves = 0;
        JPanel title, buttons; 
        static JButton button[] = new JButton[25]; 
        static int x = 0, y = 0;
        
        public JPanel createContentPane(){
        	 JPanel totalGUI = new JPanel(); 
             totalGUI.setLayout(null);
               
             buttons = new JPanel(); 
             buttons.setLayout(null); 
             buttons.setLocation(10, 10); 
             buttons.setSize(500, 500);
             totalGUI.add(buttons);
             
             for(int i = 0; i < button.length; i++){
            	 button[i] = new JButton();
            	 button[i].setSize(50, 50);
            	 button[i].addActionListener(this);
            	 button[i].setBackground(Color.RED);
            	 button[i].setBorderPainted(false);
            	 button[i].setBorder(null);
            	 button[i].setFocusable(false);
             }
             
           	 button[0].setLocation(0, 0);
        	 button[1].setLocation(100, 0);
        	 button[2].setLocation(200, 0);
        	 button[3].setLocation(300, 0);
        	 button[4].setLocation(400, 0);
        	 button[5].setLocation(0, 100);
        	 button[6].setLocation(100, 100);
        	 button[7].setLocation(200, 100);
        	 button[8].setLocation(300, 100);
        	 button[9].setLocation(400, 100);
        	 button[10].setLocation(0, 200);
        	 button[11].setLocation(100, 200);
        	 button[12].setLocation(200, 200);
        	 button[13].setLocation(300, 200);
        	 button[14].setLocation(400, 200);
        	 button[15].setLocation(0, 300);
        	 button[16].setLocation(100, 300);
        	 button[17].setLocation(200, 300);
        	 button[18].setLocation(300, 300);
        	 button[19].setLocation(400, 300);
        	 button[20].setLocation(0, 400);
        	 button[21].setLocation(100, 400);
        	 button[22].setLocation(200, 400);
        	 button[23].setLocation(300, 400);
        	 button[24].setLocation(400, 400);
        	 
        	 for(int i = 0; i < button.length; i++){
        		 buttons.add(button[i]);
        	 }
             totalGUI.setOpaque(true);
             return totalGUI;
        }
        public void actionPerformed(ActionEvent e){
        		if(e.getSource() == button[0]){
        			cornerSwitch(0);
        		}else if(e.getSource() == button[1]){
        			wallSwitch(1);
        		}else if(e.getSource() == button[2]){
        			wallSwitch(2);
        		}else if(e.getSource() == button[3]){
        			wallSwitch(3);
        		}else if(e.getSource() == button[4]){
        			cornerSwitch(4);
        		}else if(e.getSource() == button[5]){
        			wallSwitch(5);
        		}else if(e.getSource() == button[6]){
        			centerSwitch(6);
        		}else if(e.getSource() == button[7]){
        			centerSwitch(7);
        		}else if(e.getSource() == button[8]){
        			centerSwitch(8);
        		}else if(e.getSource() == button[9]){
        			wallSwitch(9);
        		}else if(e.getSource() == button[10]){
        			wallSwitch(10);
        		}else if(e.getSource() == button[11]){
        			centerSwitch(11);
        		}else if(e.getSource() == button[12]){
        			centerSwitch(12);
        		}else if(e.getSource() == button[13]){
        			centerSwitch(13);
        		}else if(e.getSource() == button[14]){
        			wallSwitch(14);
        		}else if(e.getSource() == button[15]){
        			wallSwitch(15);
        		}else if(e.getSource() == button[16]){
        			centerSwitch(16);
        		}else if(e.getSource() == button[17]){
        			centerSwitch(17);
        		}else if(e.getSource() == button[18]){
        			centerSwitch(18);
        		}else if(e.getSource() == button[19]){
        			wallSwitch(19);
        		}else if(e.getSource() == button[20]){
        			cornerSwitch(20);
        		}else if(e.getSource() == button[21]){
        			wallSwitch(21);
        		}else if(e.getSource() == button[22]){
        			wallSwitch(22);
        		}else if(e.getSource() == button[23]){
        			wallSwitch(23);
        		}else if(e.getSource() == button[24]){
        			cornerSwitch(24);
        		}
        }
        public static void centerSwitch(int n){
        	bColor[n] = bColor[n] * -1;
        	bColor[n - 1] = bColor[n - 1] * -1;
        	bColor[n + 1] = bColor[n + 1] * -1;
        	bColor[n - 5] = bColor[n - 5] * -1;
        	bColor[n + 5] = bColor[n + 5] * -1;
        	
        	for(int i = 0; i < button.length; i++){
        		if(bColor[i] == 1){
        			button[i].setBackground(Color.BLUE);
        		}else if(bColor[i] == -1){
        			button[i].setBackground(Color.RED);
        		}
        	}
        }
        public static void wallSwitch(int n){
        	if(n == 1 || n == 2 || n == 3){
            	bColor[n] = bColor[n] * -1;
            	bColor[n - 1] = bColor[n - 1] * -1;
            	bColor[n + 5] = bColor[n + 5] * -1;
            	bColor[n + 1] = bColor[n + 1] * -1;
            	
        	}else if(n == 5 || n== 10 || n== 15){
            	bColor[n] = bColor[n] * -1;
            	bColor[n + 1] = bColor[n + 1] * -1;
            	bColor[n + 5] = bColor[n + 5] * -1;
            	bColor[n - 5] = bColor[n - 5] * -1;
            	
        	}else if(n== 9 || n== 14 || n== 19){
            	bColor[n] = bColor[n] * -1;
            	bColor[n - 1] = bColor[n - 1] * -1;
            	bColor[n - 5] = bColor[n - 5] * -1;
            	bColor[n + 5] = bColor[n + 5] * -1;
            	
        	}else if(n == 21 || n == 22 || n == 23){
            	bColor[n] = bColor[n] * -1;
            	bColor[n - 1] = bColor[n - 1] * -1;
            	bColor[n + 1] = bColor[n + 1] * -1;
            	bColor[n - 5] = bColor[n - 5] * -1;
            	
        	}
        	for(int i = 0; i < button.length; i++){
            		if(bColor[i] == 1){
            			button[i].setBackground(Color.BLUE);
            		}else if(bColor[i] == -1){
            			button[i].setBackground(Color.RED);
            		}
            	}
        }
        public static void cornerSwitch(int n){
        	bColor[n] = bColor[n] * -1;
        	if(n == 0){
        		bColor[n + 1] = bColor[n + 1] * -1;
        		bColor[n + 5] = bColor[n + 5] * -1;
        	}else if(n == 4){
        		bColor[n - 1] = bColor[n - 1] * -1;
        		bColor[n + 5] = bColor[n + 5] * -1;
        	}else if(n == 20){
        		bColor[n + 1] = bColor[n + 1] * -1;
        		bColor[n - 5] = bColor[n - 5] * -1;
        	}else if(n == 24){
        		bColor[n - 1] = bColor[n - 1] * -1;
        		bColor[n - 5] = bColor[n - 5] * -1;
        	}
        	
        	for(int i = 0; i < button.length; i++){
        		if(bColor[i] == 1){
        			button[i].setBackground(Color.BLUE);
        		}else if(bColor[i] == -1){
        			button[i].setBackground(Color.RED);
        		}
        	}
        }
        public static void createAndShowGUI(){
        	JFrame.setDefaultLookAndFeelDecorated(false);
        	JFrame frame = new JFrame("Lights Out");
        	
        	LightsOut game = new LightsOut();
        	frame.setContentPane(game.createContentPane());
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setSize(500, 500);
        	frame.setResizable(false);
        	frame.setVisible(true);
        }
    	public static void main(String[] args) {
    	    for(int i = 0; i < bColor.length; i++){
    	    	bColor[i] = -1;
    	    }
    		createAndShowGUI();
    	}
    
    }
    Last edited by herpeslurpy; 11-12-2013 at 05:23 PM.
    I am not a clever man

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: JButton color won't change no matter what

    Its not fixed until you stop calling it weird and you understand what was wrong.
    jim829, SurfMan and herpeslurpy like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: JButton color won't change no matter what

    Good point. I managed to figure out that all it needed was to check the length of button. bColor now has a length of 25, also. I just don't understand how it would single out those two buttons, but trust me, I'm trying to.
    I am not a clever man

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: JButton color won't change no matter what

    Quote Originally Posted by gimbal2 View Post
    Its not fixed until you stop calling it weird and you understand what was wrong.
    That made it into my signature :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. change color
    By faraa in forum New To Java
    Replies: 3
    Last Post: 05-19-2013, 06:22 PM
  2. JButton color
    By Bimz in forum AWT / Swing
    Replies: 6
    Last Post: 09-25-2011, 03:49 AM
  3. Replies: 4
    Last Post: 03-21-2009, 06:48 PM
  4. Disabled JButton font color
    By p900128 in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2008, 09:16 AM
  5. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •