JavaFX : Get the selected value when CheckBoxTreeItem clicked - checkbox

I want to get all the selected values in TreeView containing CheckBoxTreeItems.
How to accomplish this?

You can listen to the events on the root to keep a Set<TreeItem> up to date or you can do a DFS for selected items starting at the root.
Note that the DFS approach certainly requires the smaller amount of code, but the other approach allows you to keep the set up to date during modifications...
#Override
public void start(Stage primaryStage) {
CheckBoxTreeItem<String> root = new CheckBoxTreeItem<>("root");
CheckBoxTreeItem<String> c1 = new CheckBoxTreeItem<>("c1");
c1.getChildren().addAll(
new CheckBoxTreeItem<>("c1.1"),
new CheckBoxTreeItem<>("c1.2"),
new CheckBoxTreeItem<>("c1.3")
);
CheckBoxTreeItem<String> c2 = new CheckBoxTreeItem<>("c2");
c2.getChildren().addAll(
new CheckBoxTreeItem<>("c2.1"),
new CheckBoxTreeItem<>("c2.2"),
new CheckBoxTreeItem<>("c2.3")
);
CheckBoxTreeItem<String> c3 = new CheckBoxTreeItem<>("c3");
c3.getChildren().addAll(
new CheckBoxTreeItem<>("c3.1"),
new CheckBoxTreeItem<>("c3.2"),
new CheckBoxTreeItem<>("c3.3")
);
root.getChildren().addAll(c1, c2, c3);
TreeView<String> treeView = new TreeView<>(root);
treeView.setCellFactory(CheckBoxTreeCell.forTreeView());
Set<TreeItem<String>> selected = new HashSet<>();
// listen for selection change
root.addEventHandler(CheckBoxTreeItem.checkBoxSelectionChangedEvent(), (CheckBoxTreeItem.TreeModificationEvent<String> evt) -> {
CheckBoxTreeItem<String> item = evt.getTreeItem();
if (evt.wasIndeterminateChanged()) {
if (item.isIndeterminate()) {
selected.remove(item);
} else if (item.isSelected()) {
selected.add(item);
}
} else if (evt.wasSelectionChanged()) {
if (item.isSelected()) {
selected.add(item);
} else {
selected.remove(item);
}
}
});
// listen for subtree add/remove
root.addEventHandler(TreeItem.childrenModificationEvent(), (TreeItem.TreeModificationEvent<String> evt) -> {
if (evt.wasAdded()) {
for (TreeItem<String> added : evt.getAddedChildren()) {
addSubtree(selected, (CheckBoxTreeItem<String>) added);
}
}
if (evt.wasRemoved()) {
for (TreeItem<String> removed : evt.getRemovedChildren()) {
removeSubtree(selected, (CheckBoxTreeItem<String>) removed);
}
}
});
Button button = new Button("print selected");
button.setOnAction(evt -> {
System.out.println("----------------");
selected.stream().map(TreeItem::getValue).forEach(System.out::println);
});
Button button2 = new Button("print dfs");
button2.setOnAction(evt -> {
System.out.println("----------------");
print(root);
});
Button remove = new Button("remove");
remove.setOnAction(evt -> {
root.getChildren().remove(c3);
});
Scene scene = new Scene(new VBox(treeView, button, button2, remove));
primaryStage.setScene(scene);
primaryStage.show();
}
private static <T> void removeSubtree(Collection<TreeItem<T>> collection, CheckBoxTreeItem<T> item) {
if (item.isSelected()) {
collection.remove(item);
} else if (!item.isIndeterminate() && !item.isIndependent()) {
return;
}
for (TreeItem<T> child : item.getChildren()) {
removeSubtree(collection, (CheckBoxTreeItem<T>) child);
}
}
private static <T> void addSubtree(Collection<TreeItem<T>> collection, CheckBoxTreeItem<T> item) {
if (item.isSelected()) {
collection.add(item);
} else if (!item.isIndeterminate() && !item.isIndependent()) {
return;
}
for (TreeItem<T> child : item.getChildren()) {
addSubtree(collection, (CheckBoxTreeItem<T>) child);
}
}
private static <T> void print(CheckBoxTreeItem<T> item) {
if (item.isSelected()) {
System.out.println(item.getValue());
} else if (!item.isIndeterminate() && !item.isIndependent()) {
return;
}
for (TreeItem<T> child : item.getChildren()) {
print((CheckBoxTreeItem<T>) child);
}
}

If you're using a TreeView, you can get the item selected like this:
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
#Override
public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue,
TreeItem<String> newValue) {
System.out.println("The selected item is : "newValue.getValue());
}
});

Related

RecyclerView: how do I bind CheckBox state from ViewHolder to onBindViewHolder?

I have a RecyclerView list of CardViews. Each CardView has a CheckBox that the user can select/de-select. The initial selection launches a Contextual Action Bar. An ArrayList of Integers is used to hold the checkbox state (selected or un-selected). Scrolling and the checkbox views appear to be working correctly. However, when I click a checkbox to de-select it, it remains checked and another checkbox on a different CardView is de-selected? What am I missing here?
Please note that I do not want to set up a ClickListener in onBindViewHolder.
MainActivity.java
public class MainActivity extends AppCompatActivity implements
RecyclerItemClickListener {
private ArrayList<ListItem> allList;
boolean isMultiSelect = false; // for the Contextual Action Bar status
private ActionMode mActionMode;
ArrayList<ListItem> multiselect_list = new ArrayList<>();
private ArrayList<Integer> checkedListItems = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
...
// method from the Adapter, the ItemHolder's onClick()
public void onCheckBoxClick(View view, int position) {
if (!isMultiSelect) {
multiselect_list = new ArrayList<>();
isMultiSelect = true;
if (mActionMode == null) {
mActionMode = startSupportActionMode(actionModeCallback);
}
}
multi_select(position);
}
public void multi_select(int position) {
if (mActionMode != null) {
// If a CardView with a CheckBox already selected is clicked on, then the
// Checkbox is unselected, the position is removed from the multiselect_list
// and the size of the list is decremented by +1.
if (multiselect_list.contains(allList.get(position))) {
multiselect_list.remove(allList.get(position));
}
else {
// If an empty CheckBox on a CardView is clicked on, then the position is added to the
// multiselect_list and the size of the list is incremented by +1.
multiselect_list.add(allList.get(position));
}
if (multiselect_list.size() == 1) {
mActionMode.setTitle("1 selected");
}
else if (multiselect_list.size() >= 2) {
mActionMode.setTitle(multiselect_list.size() + " selected");
}
else if (multiselect_list.size() == 0) {
mActionMode.finish();
}
refreshAdapter();
}
}
public void refreshAdapter() {
adapter.selectedItemsList = multiselect_list;
adapter.mListItems = allList;
adapter.notifyDataSetChanged();
}
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
Menu context_menu;
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_action_mode, menu);
context_menu = menu;
return true;
}
...
}
Adapter.java
public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public ArrayList<ListItem> mListItems;
private Context mContext;
private RecyclerItemClickListener recyclerItemClickListener;
public ArrayList<ListItem> selectedItemsList = new ArrayList<>();
public MyRecylerAdapter(Context context, ArrayList<ListItem> listItems, ArrayList<ListItem> selectedList) {
this.mContext = context;
this.mListItems = listItems;
this.selectedItemsList = selectedList;
}
private class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private CheckBox chkSelected;
private ItemHolder(final View itemView) {
super(itemView);
chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
chkSelected.setOnClickListener(this);
}
public void onClick(View v) {
int adapterPos = getAdapterPosition();
// send data to MainActivity() for starting CAB.
if (recyclerItemClickListener !=null) {
recyclerItemClickListener.onCheckBoxClick(v, adapterPos);
}
if (((CheckBox)v).isChecked()) {
checkedListItems.add(adapterPos);
}
else {
checkedListItems.remove(adapterPos);
}
}
void bind(int position) {
if (checkedListItems.contains(position)) {
chkSelected.setChecked(true);
}
else {
chkSelected.setChecked(false);
}
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);
final ItemHolder itemHolder = new ItemHolder(view);
...
return itemHolder;
}
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final ListItem listItem = mListItems.get(position);
final ItemHolder itemHolder = (ItemHolder) holder;
itemHolder.bind(position);
...
}
In Adpater declare an ArrayList of integers
ArrayList<Integer> checkedItems = new ArrayList();
And in bind function
void bind(int position) {
if (checkedItems.contains(position)) {
chkSelected.setChecked(true);
}
else {
chkSelected.setChecked(false);
}
}
In OnBindviewholder add below code
CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int adapterPos = getAdapterPosition();
if(isChecked) {
checkedItems.add(Integer.valueOf(adapterPos));
}else {
checkedItems.remove(Integer.valueOf(adapterPos));
}
}
}

How can i manage changed value text by highlighting the edited text only?

My question might seems to be duplicate although it is not the same issue i had experienced before.
I have successfully added a richtexbox column to the datagridview as it has being my problem for a while. now you can add richtextbox to the datagridview.
Now am trying to highlight the edited text only but am not setting it work as after i edited the text it highlight the whole text.
for an example from what i wanna get
"Test" = Testing
from the example i above, i only want to highlight the only added one from the existing one.
the code below highlighted the whole text from the datagridview richtextbox cell which is not what i want.
code that am using for color change
private void Gridview_Output_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
Gridview_Output.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
}
}
Richtextbox class
public partial class RichTextBoxControl : DataGridViewColumn
{
public RichTextBoxControl(): base()
{
base.CellTemplate = new RichTextboxCell1();
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (!((value == null)) && !(value.GetType().IsAssignableFrom(typeof(RichTextboxCell1))))
{
throw new InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}
}
}
public class RichTextboxCell1 : DataGridViewTextBoxCell
{
public RichTextboxCell1()
{
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
RichTextBoxEditingControl1 ctl = (RichTextBoxEditingControl1)DataGridView.EditingControl;
if (this.RowIndex >= 0)
{
if ((!object.ReferenceEquals(this.Value, DBNull.Value)))
{
if (this.Value != null)
{
if (this.Value != string.Empty)
{
try
{
ctl.Text =this.Value.ToString();
}
catch (Exception ex)
{
}
}
}
}
}
}
public override System.Type EditType
{
get
{
return typeof(RichTextBoxEditingControl1);
}
}
public override System.Type ValueType
{
get
{
return typeof(String);
}
}
public override object DefaultNewRowValue
{
get
{
return String.Empty;
}
}
}
class RichTextBoxEditingControl1 : RichTextBox, IDataGridViewEditingControl
{
private DataGridView dataGridViewControl;
private bool valueIsChanged = false;
private int rowIndexNum;
public RichTextBoxEditingControl1()
{
}
public object EditingControlFormattedValue
{
get
{
return this.Text;
}
set
{
if (value is string)
{
this.Text = value.ToString();
}
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.Text;
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public int EditingControlRowIndex
{
get
{
return rowIndexNum;
}
set
{
rowIndexNum = value;
}
}
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
if (Keys.KeyCode == Keys.Left || Keys.KeyCode == Keys.Up || Keys.KeyCode == Keys.Down || Keys.KeyCode == Keys.Right || Keys.KeyCode == Keys.Home || Keys.KeyCode == Keys.End || Keys.KeyCode == Keys.PageDown || Keys.KeyCode == Keys.PageUp)
{
return true;
}
else
{
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
public DataGridView EditingControlDataGridView
{
get
{
return dataGridViewControl;
}
set
{
dataGridViewControl = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valueIsChanged;
}
set
{
valueIsChanged = value;
}
}
public Cursor EditingControlCursor
{
get
{
return base.Cursor;
}
}
protected override void OnTextChanged(System.EventArgs eventargs)
{
valueIsChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(eventargs);
}
#region IDataGridViewEditingControl Members
Cursor IDataGridViewEditingControl.EditingPanelCursor
{
get { return base.Cursor; }
}
#endregion
}

Customized TreeView: setOnAction for Custom Label in TreeCell

I have made a TreeView, represented by a custom cellFactory where each cell is represented by an HBox looking like this.
How can I access the checkbox so that if you check it, a private boolean field in the corresponding EventTreeItem changes it's value?
Code:
public class EventTreeItem extends TreeItem<String>{
SimpleStringProperty item;
boolean important = true;
public EventTreeItem(boolean important, int id){
this.noNode = noNode;
super.setValue(id);
}
public EventTreeItem(){
}
public void setImportant(Boolean important){
this.important = important;
}
}
CellFactory:
public final class CustomTreeCellFactory extends TreeCell<String>{
private TextField textField;
private HBox hBox;
private HBox hBoxLeaf;
public CustomTreeCellFactory(){
try {
hBox = (HBox) FXMLLoader.load(getClass().getResource("/Views/TreCell.fxml"));
} catch (IOException e) {
System.out.println("This didn't work");
e.printStackTrace();
}
try {
hBoxLeaf = (HBox) FXMLLoader.load(getClass().getResource("/Views/TreCellLowestLevel.fxml"));
} catch (IOException e) {
System.out.println("This didn't work");
e.printStackTrace();
}
hBox.setAlignment(Pos.CENTER_LEFT);
hBoxLeaf.setAlignment(Pos.CENTER_LEFT);
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
EventTreeItem eventTreeItem = (EventTreeItem) getTreeItem();
if (getTreeView().getTreeItemLevel(getTreeItem())==1) {
setGraphic(this.hBox);
((CheckBox) ((HBox)getGraphic()).getChildren().get(3)).setSelected(((EventTreeItem) getTreeItem()).important);
((Label) hBox.getChildren().get(0)).setText(eventTreeItem.noNode.getEntryNumber() + " " + eventTreeItem.noNode.getClass().getName().split("\\.")[3]);
((Label) hBox.getChildren().get(1)).setText(eventTreeItem.noNode.getDate().toString());
}else if (getTreeView().getTreeItemLevel(getTreeItem())==2){
setGraphic(this.hBoxLeaf);
}
} else {
setGraphic(null);
}
}
}
NodeTreeView
public class NodeTreeView implements ChartView{
private FilteredListModel filteredListModel;
TreeItem<String> root;
AnchorPane parent;
TreeView treeView;
public NodeTreeView(FilteredListModel filteredListModel, TabPane tabPane) throws IOException {
this.filteredListModel = filteredListModel;
parent = (AnchorPane) FXMLLoader.load(getClass().getResource("/Views/TryTreeViewInAnchorPane.fxml"));
parent.setVisible(true);
generateTree();
}
private void generateTree() {
this.root = new EventTreeItem();
root.setExpanded(true);
filteredListModel.makeEventNodeArrays().forEach(node->{
EventTreeItem item = new EventTreeItem((EventNoNode) node);
EventTreeItem item2 = new EventTreeItem();
root.getChildren().add(item);
item.getChildren().add(item2);
});
treeView = (TreeView) parent.getChildren().get(0);
treeView.setRoot(root);
treeView.setShowRoot(false);
treeView.setEditable(true);
treeView.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
#Override
public TreeCell<String> call(TreeView<String> param) {
return new CustomTreeCellFactory();
}
});
}
}
You can add a listener to checkbox's selectedProperty in your CustomTreeCellFactory constructor (which is not a factory, btw; you should call it CustomTreeCell instead):
public CustomTreeCellFactory() {
// ...
CheckBox checkbox = ...;
checkbox.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
((EventTreeItem) getTreeItem()).important = isSelected;
});
}
Btw, it is probably a better idea to make the "important" flag be part of the item, i.e. instead of TreeView<String>, you would have TreeView<MyItem> where MyItem is
class MyItem {
String item;
boolean important;
MyItem(String item, boolean important) {
this.item = item;
this.important = important;
}
}

mvvm how to pass data from one view model to another view model

I have One View which has one Data grid with radio Button , onchecking radio Box , the selected row should go to other View Screen Textbox
here is my first ViewModel
public class CampaignSearchResultsViewModel : ViewModelBase
{
public CampaignSearchResultsViewModel(List<Lead> obj)
{
foreach(Lead lead in obj)
{
SelectedLead = lead;
}
}
public CampaignSearchResultsViewModel()
{
this.Commands.Add("CheckedCommand", new ActionCommand<Lead>(CheckIt));
Commands.Add("OutboundSelect", new ActionCommand<Object>(OutboundSelection));
_leads = new ObservableCollection<Lead>();
}
public ICommand OutboundSelect
{
get
{
return Commands["OutboundSelect"];
}
}
public void OutboundSelection(Object obj)
{
}
private void CheckIt(Lead lead)
{
SelectedLead = lead;
LeadViewModel lmv = new LeadViewModel(this);
}
#region Private
private ObservableCollection<Lead> _leads;
public bool IsChecked { get; set; }
private ICommand _checkedCommand;
private object _testProperty;
private Lead _selectedLead;
private ICollectionView icv;
#endregion
private ICommand _checkedRadioCommand;
private bool _inboundChecked;
#region Properties
public ObservableCollection<Lead> Leads
{
get { return _leads; }
set
{
_leads = value;
FirePropertyChanged("Leads");
}
}
public Lead SelectedLead
{
get { return _selectedLead; }
set { _selectedLead = value; }
}
public ICommand CheckedCommand
{
get
{
return Commands["CheckedCommand"];
}
}
public bool InboundChecked
{
get
{
return _inboundChecked;
}
private set
{
if (_inboundChecked != value)
{
_inboundChecked = value;
FirePropertyChanged("InboundChecked");
}
}
}
#endregion
}
i have to map SelectedLead to the other view model i have pass info to SearchCampaignMembers() method , how
public partial class LeadViewModel : ViewModelBase
{
public void SearchCampaignMembers()
{
_service.Load(_service.SearchCampaignMembersQuery(Entity.FirstName, Entity.LastName), lo =>
{
if (!lo.HasError)
{
ListLead = lo.Entities.ToList();
_savedLeadStatusId = Entity.LeadStatusId;
EntitySet = _service.Leads;
if (ListLead.Count == 1)
{
if (Entity != null)
{
IsVendorLead = Entity.LeadTypeId == Lookups.LeadType.VendorLead;
//Lead Update History
EntityQuery<LeadUpdateHistory> historyquery = null;
historyquery = _service.GetLeadUpdateHistoryByLeadIdQuery(Entity.LeadId);
_service.Load(historyquery, l =>
{
if (!l.HasError)
{
EntityHistory = _service.LeadUpdateHistories;
}
}, null);
//Lead Assignment
EntityQuery<LeadsAssignment> assignmentquery = null;
assignmentquery = _service.GetLeadsAssignmentByLeadIdQuery(Entity.LeadId);
_service.Load(assignmentquery, l =>
{
if (!l.HasError)
{
EntityAssignment = _service.LeadsAssignments;
}
}, null);
if (Entity.LeadTypeId == Lookups.LeadType.PhoneLead)
{
IsInboundLead = Entity.VendorId == null;
IsOutboundLead = Entity.VendorId != null;
}
else
{
IsInboundLead = false;
IsOutboundLead = false;
}
//SelectTimeToCall(Entity);
if (IsOutboundLead)
SelectedCampaign = Entity.LeadCampaigns.FirstOrDefault().Campaign;
else
SelectCampaign(Entity);
OperationsListener listener = new OperationsListener();
listener.Completed += (s, args) =>
{
CompleteInitializing();
//SwitchTab(param.InitialTab);
Action action = () =>
{
SelectDealer(Entity);
};
//GetDealerRecommendation(Entity.Address.ZipCode, action);
SelectStatus(Entity);
//if (callback != null)
// callback();
};
LoadLookupData(listener);
listener.Start();
}
}
else if (ListLead.Count >= 1)
{
CampaignSearchResultsViewModel vm = new CampaignSearchResultsViewModel();
foreach (Lead lead in ListLead)
{
vm.Leads.Add(lead);
ObservableCollection<Lead> abc;
abc = new ObservableCollection<Server.DataAccess.Lead>();
}
ViewController.OpenDialog("SearchCampaignResults", vm, r =>
{
});
}
else if (ListLead.Count == 0)
{
ViewController.OpenDialog("NoResults", (r) =>
{
});
}
}
else
{
//if (callback != null)
// callback();
}
}, null);
}
}
If you use MVVM Light Toolkit, see Messenger class see this answer for sample.

How to disable some items of javaFX ComboBox?

Can someone show me how to disable some item of my combobox (With FXML or Java code)? here is my ComboBox:
<ComboBox fx:id="cBox">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Easy" />
<String fx:value="Normal" />
<String fx:value="Hard" />
</FXCollections>
</items>
</ComboBox>
Thanks!
i didn't found any methods that can inactive ComboBox items. You can try this work around , below code is to display sublist of items dynamically(use this idea to solve your problem).
private final ObservableList<String> allOptions =
FXCollections.observableArrayList("Easy","Normal","Hard");
// method which returns sublist we need
private ObservableList<String> getSubList(int start,int end) {
final ObservableList<String> toBeDisplayedList = FXCollections
.<String> observableArrayList();
toBeDisplayedList.addAll(allOptions.subList(start, end));
return toBeDisplayedList;
}
// now main logic
if(displayAll) {
comboBox.setItems(allOptions);
}
if(display only easy and normal) {
comboBox.setItems(getSublist(0,2));
} ...
I was trying to achieve this and I came up with a custom ComboBox that disables the items I don't want the user to select. Below code shows the custom ComboBox class and how to use it.
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]);
}
}
}
Then add the items to disable to the ComboBox:
#FXML private CustomComboBox<String> customComboBox;
...
customComboBox.setDisabledItems("Item 2", "Item 3");
And change the class in the fxml file:
<views.custom.CustomComboBox ... />
I had the same issue and I think that the best solution to this problem is to use the
setCellFactory(Callback,ListCell> value) method of ComboBox:
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
}
}
};
}
});
and if you want a custon ButtonCel you need to use the setButtonCell(ListCell value) Method:
cBox.setButtonCell(new ListCell<String>() {
#Override
protected void updateItem(Stringitem, boolean empty)
{
super.updateItem(item, empty);
if (item != null || !empty)
{
this.setText(item);
this.setDisable(true); //or false
}
}
});
comboBox.setItems(FXCollections.observableArrayList(EnumValues.values()));
comboTipoOperacoes.getItems().remove(4); // remove the item 4 of Enums.

Resources