1
私はDestinationオブジェクトのリストとContainerを拡張するクラス(DestinationsRenderer)を持ち、リストをレンダリングするためにListCellRendererを実装しています。リストの各セルには削除ボタンがあり、ボタンクラスにはButtonが続きます。リストセル内にあるときに押されたボタンを処理する方法
私がしたいことは、削除ボタンが押されているときに、その境界を強調表示することです。私は2つの大きな問題があります。
- とがリリース押された後にボタンは反応するが、それだけで押されたとき、私は反応することを必要とする(そしてそれがリリースされている別のアクション)
- 私は1つのボタンを選択すると、リスト内のすべてのボタンは、一度
- 私はログステートメントを使用して検証上記2で選択しますが、実際の境界はさえ、私は以前thisを参照し
まで表示されません。答えたそれは最初の問題には対処していません。
リストアクションリスナーが間違った場所に追加されている可能性がありますか?ここで
はレンダラからコードスニペットです:
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
}