ソース コードからわかる限り、コントローラーが選択されているかどうかを追跡する価値はありません。ただし、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 ソース コードを編集してライブラリを再コンパイルし、カスタム バージョンを使用する必要があります。