Lez
Lez

Reputation: 11

detecting clicks not working

So, I've been doing this code using JFrame where I have to change an label foreground color to one selected from a jcolorchooser, first thing i tried unsuccessfully to use the "Mouseclicked" event from the colorchooser element and it just doesn't work, I think I know why but Im not sure so lets just leave it in "it doesn't work properly", I´ve been trying other options and i endup with the one i think could be the most effective, implementing mouselistener but I think Im not using it as i should becouse it is not working and right now Im just really tired, so can you guy give me a hand? here is the code:

public class dieztres extends javax.swing.JFrame implements MouseListener{

@Override
public void mouseClicked(MouseEvent e) {
    mylabel.setForeground(mycolorchooser.getSelectionModel().getSelectedColor());
}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}                 

}

getting everything useless out of the way(like auto generated code) thats the important parts and I want to apologize if this is basic stuff, Im new in this area.

Upvotes: 0

Views: 41

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347332

Start by taking a look at How to Use Color Choosers which demonstrates how you might solve your problem...

Directly from the tutorial...

tcc.getSelectionModel().addChangeListener(this);
. . .
public void stateChanged(ChangeEvent e) {
    Color newColor = tcc.getColor();
    banner.setForeground(newColor);
}

Make sure make use of the available tutorials and consult the JavaDocs when you have issues, they often have solutions for the more common issues

Upvotes: 1

Albert
Albert

Reputation: 337

Implementing MouseListener is not enough. You must also register your class with a Component. A JFrame is a Component and your class is a JFrame, so just put this.addMouseListener(this) somewhere convenient such as the constructor method.

Upvotes: 1

Related Questions