Question

So I have a label and I want to do the next, as default it starts on red background color, when i do first click (mousePressed) i change the background color to Green.

Now, i want it to turn back to Red once i press for 2nd time once i press it again.

Something like if it is red, turn green. And if it is green, turn red.

However i don't manage to get it right... I tried something like this:

    Object o = evt.getSource();
    boolean checkGreen = false;

    if (o.equals(lblSI)) {
        lblSI.setBackground(Color.GREEN);
        checkGreen = true;
    }
    if (o.equals(lblSI) && checkGreen == true) {
        lblSI.setBackground(Color.RED);
    }

But it obviously doesnt work since I first turn it green, then red, its an instant change, cant find the right code...

Was it helpful?

Solution

You can use an else to take a different action. However, the state of the green color needs to be part of the object fields, not defined in the action method (as it would be reset to false for each action then).

It might also be more clear if you separate the check for the source and the check for color selection.

... object definition ...
boolean isGreen = false;

... action listener...
Object o = evt.getSource();

if (o.equals(lblSI)) {
    if (isGreen) {
      lblSI.setBackground(Color.RED);
    } else {
      lblSI.setBackground(Color.GREEN);
    }
    isGreen = !isGreen;
}

Adding a complete example that instead sets the foreground color as background color will not work on all platforms.

public class RedGreen implements Runnable {

    private JButton press;

    @Override
    public void run() {
        JFrame frame = new JFrame("RedGreen");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        press = new JButton("Press");
        press.addActionListener(new ActionListener() {
            boolean isGreen = false;
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isGreen) {
                    press.setForeground(Color.RED);
                } else {
                    press.setForeground(Color.GREEN);
                }
                isGreen = !isGreen;
            }
        });
        frame.getContentPane().add(press);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String...args) throws Exception {
        SwingUtilities.invokeAndWait(new RedGreen());
    }
}

OTHER TIPS

Here we go for a pretty clean way of doing it. I hope this answers your question.

public class MyLabel extends JLabel implements MouseListener {

private boolean isRed;
private boolean isGreen;
private boolean first_time = true;

public MyLabel(String name) {
    super(name);
    this.isRed = true;
    this.isGreen = false;
    this.setOpaque(true);
    this.addMouseListener(this);
    this.setBackground(Color.red);
}

public void setRed(boolean val) {
    isRed = val;
}

public void setGreen(boolean val) {
    isGreen = val;
}

public boolean getRed() {
    return isRed;
}

public boolean getGreen() {
    return isGreen;
}


@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {
    if (first_time) {
        first_time = false;
        this.setGreen(true);
        this.setRed(false);
    }
    if (getRed()) {
        this.setBackground(Color.red);
        this.setGreen(true);
        this.setRed(false);
    }
    else if (getGreen()) {
        this.setBackground(Color.green);
        this.setGreen(false);
        this.setRed(true);
    }
}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

}

And the next:

public class TestFrame extends JFrame {
public TestFrame() {
    JLabel label = new MyLabel("Name of Label");
    this.add(label);
    this.setVisible(true);
    pack();
}

}

And lastly the main method:

public class Main {
public static void main(String[] args) {
    new TestFrame();
}

}

If you have any questions about what I'm doing feel free to ask me. This is how I learnt myself how to code better by asking questions. All the best with the coding :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top