[英]Override method that toggles 'isSelected' in getListCellRendererComponent
我想制作自己的方法來控制組件何時被“ isSelected”。
我有一個包含多個JPanel
的JList
。 JPanel extends ListCellRenderer<>
的構造類JPanel extends ListCellRenderer<>
。
為了顯示我選擇了一個JList組件(JPanel),我使用了它。
@Override
public Component getListCellRendererComponent(..., boolean isSelected, ...) {
if(isSelected){
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
我想要一種使選定項目保持“選定”狀態的方法,盡管我選擇選擇另一個。 我知道可以通過按住CTRL來完成,但是.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
不能完全解決問題。 我想通過單擊選擇多個,然后通過單擊取消選擇。
為此,我已經使用ListSelectionMode,但是我找不到辦法。
完成上述操作后,我想實現一種方法,該方法僅在單擊特定區域時才在列表中選擇一個組件(而不是預設的整個組件)。 我已經制作了此方法,如果單擊了正確的區域,它將返回true,否則返回false。 但是,由於我無法弄清楚如何覆蓋使組件“ isSelected”的mouseevent,所以這很棘手。
這是我要覆蓋'isSelected'方法的方法的代碼;
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
if(ActionHandler.mouseClickedPrebuild(evt.getPoint())){
//This code runs if that special place is clicked!
//So now the component should be 'isSelected' or
//deselected if it already was 'isSelected'.
}
}
});
這段代碼在我的JList
的構造函數中
還有mouseClickedPrebuild
方法;
public static boolean mouseClickedPrebuild(Point point) {
int index = theJList.locationToIndex(point);
Rectangle bounds = theJList.getCellBounds(index,index);
Point p = bounds.getLocation();
return ( ... long list of greater than & less than ...);
//This gives the certain area which is accepted to return true
我解決了這個問題!
因此,通過運行此行可以顯示我的視圖;
// UI Class JScrollPane Custom JList
UIConstructor.listview.setViewportView(new ListView( -insert ArrayList here- ));
這是我的ListView。 我用來解決問題的自定義DefaultListSelectionModel是@FuryComptuers發布的;
我必須對代碼進行一些更改,因為selectionModel
的兩個方法將在我的mouseevent之前運行。 我以靜態方式保存了變量,因此不是在setSelectionInterval
中運行代碼,而是在mousePressed
。
然后,如果單擊特定列表元素中的窗簾區域,則可以添加返回true的boolean isSelected
。
public class ListViewd extends JList {
static boolean isSelected;
static Point point;
static boolean gS = false;
static int in0;
static int in1;
@Override
public Dimension getPreferredScrollableViewportSize() {
Dimension size = super.getPreferredScrollableViewportSize();
size.setSize(new Dimension(0,0));
return size;
}
public ListView(ArrayList<System> items) {
DefaultListModel<System> list = new DefaultListModel<System>();
for (System item : items) {
list.addElement(item);
}
this.setSelectionModel(new DefaultListSelectionModel() {
boolean gestureStarted = false;
@Override
public void setSelectionInterval(int index0, int index1) {
gS = gestureStarted;
in0 = index0;
in1 = index1;
gestureStarted = true;
}
@Override
public void setValueIsAdjusting(boolean isAdjusting) {
if (!isAdjusting) {
gestureStarted = false;
}
}
});
ListSelectionModel selectionModel = this.getSelectionModel();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
point = e.getPoint();
isSelected = ActionHandler.mouseClickedPrebuild(point);
if(!gS && isSelected){
if (isSelectedIndex(in0)) {
selectionModel.removeSelectionInterval(in0, in1);
} else {
selectionModel.addSelectionInterval(in0, in1);
}
}
}
});
setModel(list);
setCellRenderer(new ListModelPrebuild());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.