mhrzn
mhrzn

Reputation: 389

Loop the JButton in three color when clicked?

How can i change the JButton color to loop to green,yellow,white when it is clicked? if i click it must go to yellow ,click again then white, click again then changes to green and so on.

public void mouseClicked(MouseEvent e) 
{
     countClick++;       
    switch (countClick) 
    {
        case 0:
            m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.yellow);
            break;
        case 1:
            m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.yellow);
            break;

        case 2:
            m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.red);
            countClick =0;
    }

}

What if the button is already yellow then i have to click it twice. Note: The Button are stored in the array list. and i used loop to add it to the grid layout. the colored are read from the text file. This is how i added the JBUtton on GridlLayout. This is in another class.

 btn = new ArrayList<>();

        while((text = br.readLine())!=null)
        {
            tmp=text.split(",");

            for(int i=0; i<tmp.length;i++)
            {
                System.out.print(tmp[i]);

                switch (tmp[i]) 
                {
                    case "0":
                        btn.add( i,new JButton() );
                        btn.get(i).setBackground(Color.GRAY);
                        btn.get(i).setEnabled(false);
                        newPanel.add(btn.get(i));
                        break;
                    case "1":
                        btn.add( i,new JButton("A") );
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                    case "2":
                        btn.add( i,new JButton("G") );
                        btn.get(i).setBackground(Color.GREEN);
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                    case "3":
                        btn.add( i,new JButton("Y") );
                        btn.get(i).setBackground(Color.YELLOW);
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                    case "4":
                        btn.add( i,new JButton("R") );
                        btn.get(i).setBackground(Color.RED);
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                }
            }
        }

Upvotes: 0

Views: 202

Answers (3)

mhrzn
mhrzn

Reputation: 389

@Override
public void mouseClicked(MouseEvent e) 
{
    if(Color.green==( m.btn.get(m.btn.indexOf(e.getSource())).getBackground()))
    {
        m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.red);
    }
    else if(Color.red==( m.btn.get(m.btn.indexOf(e.getSource())).getBackground()))
    {
        m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.yellow);
    }
    else if(Color.yellow==( m.btn.get(m.btn.indexOf(e.getSource())).getBackground()))
    {
        m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.green);
    }

}

i guess this will do for now, sorry for the trouble guys.

Upvotes: 0

Ren&#233; Link
Ren&#233; Link

Reputation: 51333

I would do it this way, because it let you easily change the colors and re-use the "color changer" with other buttons.

public  class ButtonBackgroundColorChanger implements ActionListener {
    private int actualColorIndex = 0;
    private Color[] colors;

    public ButtonBackgroundColorChanger(Color... colors) {
        if (colors.length < 1) {
            throw new IllegalArgumentException(
                    "At least one color must be provided");
        }
        this.colors = colors;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        AbstractButton abstractButton = (AbstractButton) e.getSource();

        Color nextColor = nextColor();
        abstractButton.setBackground(nextColor);
    }

    private Color nextColor() {
        Color actualColor = colors[actualColorIndex++];
        actualColorIndex = actualColorIndex % colors.length;
        return actualColor;
    }

}

Then use it, e.g.

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout(FlowLayout.CENTER));

        ButtonBackgroundColorChanger buttonBackgroundColorChanger1 = new ButtonBackgroundColorChanger(
                Color.YELLOW, Color.WHITE, Color.GREEN);
        AbstractButton button1 = new JButton("click to change color");
        contentPane.add(button1);
        button1.addActionListener(buttonBackgroundColorChanger1);

        ButtonBackgroundColorChanger buttonBackgroundColorChanger2 = new ButtonBackgroundColorChanger(
                Color.BLUE, Color.CYAN, Color.MAGENTA);
        AbstractButton button2 = new JButton("click to change color");
        contentPane.add(button2);
        button2.addActionListener(buttonBackgroundColorChanger2);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Upvotes: 0

Sven Br&#252;ck
Sven Br&#252;ck

Reputation: 11

You could try this

private int countClicks =0;

   public void actionPerformed(ActionEvent e) {
         countClicks++;
         switch (countClicks){
              case 0:
                  boutton.setBackground(Color.YELLOW);
                  break;
              case 1:
                  boutton.setBackground(Color.WHITE);
                  break;
              case 2:
                  boutton.setBackground(Color.GREEN);
                  countClicks=0;
                  break;
     }

Upvotes: 1

Related Questions