Reputation: 549
I am new to Java and would like to achieve the following.
I have a various comboboxes
. For each combobox
I have a button that should clear the content of the combobox. The clear buton is created from a generic ClearComboBoxButton
class that I have created. As part of this ClearComboBoxButton
class I want to add a MouseListener
that will clear the combobox
associated with it.
When I create my button the associated combobox
is passed as a parameter.
This is what I have at the moment. (My button is created successfully but the listener
does not seem to fire)
public class ClearComboBoxButton extends JXButton implements MouseListener{
private JXComboBox cboComboBox;
private static final long serialVersionUID = 1L;
public ClearComboBoxButton(JXComboBox cboComboBox){
super();
this.setCboComboBox(cboComboBox);
setPreferredSize(new Dimension(20, 20));
setMinimumSize(new Dimension(20,20));
setMaximumSize(new Dimension(20, 20));
setToolTipText("Clear");
setIcon(new ImageIcon("C:\\Java\\icons\\cancel.png"));
}
public void mouseClicked(MouseEvent arg0, JXComboBox cboComboBox) {
System.out.println("Cleared");
this.setCboComboBox(cboComboBox);
cboComboBox.setSelectedIndex(-1);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public JXComboBox getCboComboBox() {
return cboComboBox;
}
public void setCboComboBox(JXComboBox cboComboBox) {
this.cboComboBox = cboComboBox;
}
}
Upvotes: 0
Views: 678
Reputation: 691685
First of all, the mouseClicked()
method doesn't do anything:
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
Second, in order for your listener to be called, it should be added to the component which fires mouse events, and that you want to listen to. Since your button wants to be aware of the events fired by itself, you need
this.addMouseListener(this);
But you should never use a MouseListener
to listen to button clicks. An ActionListener
should be used for that. First because it's simpler, and more correct, semantically. Second, because many users use their keyboard to click a button.
I would personally avoid extending JXButton, and use composition instead. And I would also use an anonymous class as the listener, rather than making your class implement it.
Upvotes: 2
Reputation: 23637
Your event source (Button) needs to register an event handler in order to receive mouse events. Since your Button implements MouseListener, you can register this
as your listener:
public ClearComboBoxButton(JXComboBox cboComboBox){
super();
this.setCboComboBox(cboComboBox);
setPreferredSize(new Dimension(20, 20));
setMinimumSize(new Dimension(20,20));
setMaximumSize(new Dimension(20, 20));
setToolTipText("Clear");
setIcon(new ImageIcon("C:\\Java\\icons\\cancel.png"));
this.addMouseListener(this); // add this line
}
Upvotes: 1