據我從源代碼可以看出,沒有價值來跟蹤控制器被選中與否。但是,您可以手動跟蹤使用ControlEvent資料監聽器和手動更改背景顏色作爲快速和哈克解決方法:
import controlP5.*;
ControlP5 controlp5;
ListBox list;
int selectedIndex = -1;//no selection initially
int colourBG = 0xffff0000;
int colourSelected = 0xffff9900;
void setup(){
size(400,400);
controlp5 = new ControlP5(this);
list = controlp5.addListBox("LIST")
.setPosition(130, 40)
.setSize(100, 150)
.setItemHeight(20)
.setBarHeight(20)
.setColorBackground(colourBG)
.setColorActive(colourSelected);
for (int i=0;i<80;i++) {
ListBoxItem lbi = list.addItem("item "+i, i);
lbi.setColorBackground(colourBG);
}
}
void draw(){
background(255);
}
void controlEvent(ControlEvent e) {
if(e.name().equals("LIST")){
int currentIndex = (int)e.group().value();
println("currentIndex: "+currentIndex);
if(selectedIndex >= 0){//if something was previously selected
ListBoxItem previousItem = list.getItem(selectedIndex);//get the item
previousItem.setColorBackground(colourBG);//and restore the original bg colours
}
selectedIndex = currentIndex;//update the selected index
list.getItem(selectedIndex).setColorBackground(colourSelected);//and set the bg colour to be the active/'selected one'...until a new selection is made and resets this, like above
}
}
所以在上面的例子中,的selectedIndex存儲先前/最近選擇的列表索引。這然後在controlEvent處理程序中使用。如果之前進行了選擇,請恢復正常的背景顏色。然後繼續將選定的索引設置爲最近選擇的項目,並將背景色設置爲活動項目,這樣看起來就像是選中了。
這是一個手動/哈克的方法。較長的版本將涉及擴展ListBox java類並添加此功能,或者編輯controlP5源代碼,重新編譯庫並使用定製版本。
你正在使用什麼版本的處理和controlp5? –
我正在使用處理版本1.5.1和controlp5 1.5.2。我正在使用舊版本,因爲我的應用程序正在使用GLGraphics 1.0.0,並且無法使它在更新版本的處理中工作。 – omegaFlame