Reputation: 51
I have a list of Destination objects and a class (DestinationsRenderer) which extends Container and implements ListCellRenderer to render the list. Each cell in the list has a delete button, and the button class extends Button.
What I want to do is highlight the border of the delete button when it is being pressed. I have two major issues:
I referenced this previously answered question, but that also doesn't address the first issue.
Is it possible that the list action listener is being added in the wrong place?
Here are code snippets from the renderer:
public DestinationsRenderer(final StateMachine stateMachine){
// other code initializing the buttons and labels
mDeleteButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
if (mList.getSelectedIndex() < mStateMachine.getNumDestinations()) {
mIsDeleteSelected = true;
mStateMachine.deleteDestination(mList.getSelectedIndex());
}
}
}
}
public Component getListCellRendererComponent(final List list,
final Object value,
final int index,
final boolean isSelected){
// list of Destinations
if(mList != null){
mList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
if(mIsDeleteSelected){
mIsDeleteSelected = false; // if i take out this line of code,
// all of the buttons get highlighted
// when i click,
// but if i leave it in, none of them do
Style style = mDeleteButton.getPressedStyle();
style.setBorder(Border.createLineBorder(1, 1416152));
mDeleteButton.setPressedStyle(style);
mDeleteButton.setUnselectedStyle(style);
mDeleteButton.setSelectedStyle(style);
mDeleteButton.setDisabledStyle(style);
} else {
Style style = mDeleteButton.getPressedStyle();
style.setBorder(Border.createEmpty());
mDeleteButton.setPressedStyle(style);
mDeleteButton.setUnselectedStyle(style);
mDeleteButton.setSelectedStyle(style);
mDeleteButton.setDisabledStyle(style);
}
}
});
}
mList = list;
// more code handling other list cell rendering stuff
}
Upvotes: 1
Views: 83
Reputation: 3760
The getListCellRendererComponent is being called constantly it would be a mistake to add listeners to the List inside that method. I would suggest you to change to a Container BoxLayout Y instead of a List, it would be easier to handle buttons and events on a Container
Upvotes: 1