如何禁用JavaFXComboBox的某些项?
有人能告诉我如何禁用组合框的某些项(使用FXML或Java代码)吗?这是我的组合框:如何禁用JavaFXComboBox的某些项?,combobox,javafx-2,javafx,Combobox,Javafx 2,Javafx,有人能告诉我如何禁用组合框的某些项(使用FXML或Java代码)吗?这是我的组合框: <ComboBox fx:id="cBox"> <items> <FXCollections fx:factory="observableArrayList"> <String fx:value="Easy" /> <String fx:value="Normal" /> <String fx:va
<ComboBox fx:id="cBox">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Easy" />
<String fx:value="Normal" />
<String fx:value="Hard" />
</FXCollections>
</items>
</ComboBox>
@FXML private CustomComboBox<String> customComboBox;
...
customComboBox.setDisabledItems("Item 2", "Item 3");
cBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param)
{
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty)
{
super.updateItem(item, empty);
if (item != null || !empty)
{
this.setText(item);
this.setDisable(true); //or false
}
}
};
}
});
谢谢 我没有找到任何可以删除非活动项的方法。您可以尝试这项工作,下面的代码是动态显示项目的子列表(使用这个想法来解决您的问题)
private final observebleList allOptions=
FXCollections.observearraylist(“简单”、“正常”、“困难”);
//方法返回我们需要的子列表
私有ObservableList getSubList(int开始,int结束){
最终可观察列表toBeDisplayedList=FXCollections
.observableArrayList();
toBeDisplayedList.addAll(allOptions.subList(start,end));
返回到电子显示列表;
}
//现在是主要逻辑
如果(显示全部){
comboBox.setItems(allOptions);
}
if(仅显示简单和正常){
setItems(getSublist(0,2));
} ...
我正试图实现这一点,我想出了一个自定义组合框,用于禁用我不希望用户选择的项目。下面的代码显示了自定义ComboBox类以及如何使用它
public class CustomComboBox<T> extends ComboBox<T> {
private ArrayList<T> disabledItems = new ArrayList<T>();
public CustomComboBox() {
super();
setup();
}
public CustomComboBox(ObservableList<T> list) {
super(list);
setup();
}
private void setup() {
SingleSelectionModel<T> model = new SingleSelectionModel<T>() {
@Override
public void select(T item) {
if (disabledItems.contains(item)) {
return;
}
super.select(item);
}
@Override
public void select(int index) {
T item = getItems().get(index);
if (disabledItems.contains(item)) {
return;
}
super.select(index);
}
@Override
protected int getItemCount() {
return getItems().size();
}
@Override
protected T getModelItem(int index) {
return getItems().get(index);
}
};
Callback<ListView<T>, ListCell<T>> callback = new Callback<ListView<T>, ListCell<T>>() {
@Override
public ListCell<T> call(ListView<T> param) {
final ListCell<T> cell = new ListCell<T>() {
@Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.toString());
if (disabledItems.contains(item)) {
setTextFill(Color.LIGHTGRAY);
setDisable(true);
}
} else {
setText(null);
}
}
};
return cell;
}
};
setSelectionModel(model);
setCellFactory(callback);
}
public void setDisabledItems(T... items) {
for (int i = 0; i < items.length; i++) {
disabledItems.add(items[i]);
}
}
}
公共类CustomComboBox扩展ComboBox{
私有ArrayList disabledItems=新ArrayList();
公共CustomComboBox(){
超级();
设置();
}
公共CustomComboBox(可观察列表){
超级(名单);
设置();
}
私有无效设置(){
SingleSelectionModel=新的SingleSelectionModel(){
@凌驾
公共作废选择(T项){
if(禁用数据项包含(项)){
返回;
}
super.选择(项目);
}
@凌驾
公共无效选择(整数索引){
T item=getItems().get(索引);
if(禁用数据项包含(项)){
返回;
}
super.select(索引);
}
@凌驾
受保护的int getItemCount(){
返回getItems().size();
}
@凌驾
受保护的T getModelItem(int索引){
返回getItems().get(索引);
}
};
Callback Callback=new Callback(){
@凌驾
公共ListCell调用(ListView参数){
最终ListCell单元格=新ListCell(){
@凌驾
public void updateItem(T项,布尔值为空){
super.updateItem(项,空);
如果(项!=null){
setText(item.toString());
if(禁用数据项包含(项)){
setTextFill(颜色:浅灰色);
设置禁用(true);
}
}否则{
setText(空);
}
}
};
返回单元;
}
};
设置选择模型(模型);
setCellFactory(回调);
}
公共void setDisabledItems(T…项){
对于(int i=0;i
然后将要禁用的项目添加到组合框:
<ComboBox fx:id="cBox">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Easy" />
<String fx:value="Normal" />
<String fx:value="Hard" />
</FXCollections>
</items>
</ComboBox>
@FXML private CustomComboBox<String> customComboBox;
...
customComboBox.setDisabledItems("Item 2", "Item 3");
cBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param)
{
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty)
{
super.updateItem(item, empty);
if (item != null || !empty)
{
this.setText(item);
this.setDisable(true); //or false
}
}
};
}
});
@FXML-private-CustomComboBox-CustomComboBox;
...
customComboBox.setDisabledItems(“第2项”、“第3项”);
并更改fxml文件中的类:
<views.custom.CustomComboBox ... />
我也有同样的问题,我认为解决这个问题的最好办法是使用 组合框的方法:
<ComboBox fx:id="cBox">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Easy" />
<String fx:value="Normal" />
<String fx:value="Hard" />
</FXCollections>
</items>
</ComboBox>
@FXML private CustomComboBox<String> customComboBox;
...
customComboBox.setDisabledItems("Item 2", "Item 3");
cBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param)
{
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty)
{
super.updateItem(item, empty);
if (item != null || !empty)
{
this.setText(item);
this.setDisable(true); //or false
}
}
};
}
});
cBox.setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView参数)
{
返回新的ListCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空)
{
super.updateItem(项,空);
如果(项!=null | |!空)
{
本文件为setText(项目);
this.setDisable(true);//或false
}
}
};
}
});
如果您想要custon按钮,则需要使用以下方法:
cBox.setButtonCell(新的ListCell(){
@凌驾
受保护的void updateItem(Stringitem,布尔值为空)
{
super.updateItem(项,空);
如果(项!=null | |!空)
{
本文件为setText(项目);
this.setDisable(true);//或false
}
}
});
有关禁用组合框项目的方法,请参阅。值得注意的另一种(可能更简单?)方法是,与其从java设置颜色,不如从CSS设置颜色。如果删除行setTextFill
,而是添加一个CSS项。列表单元格:disabled{-fx text fill:#D3D3D3/*或-fx opacity:0.40*/}
,您将获得相同的效果,但在将CSS应用到应用程序时不需要挂起自己。这是因为setDisable(true)
会导致单元格标记CSS伪类“disabled”。