How to get the selected item from a ComboBox in JavaFX? - combobox

i have ComboBox cbx and a TabPane Contains Tabs (tab: t) and a button b1. So on click on this button b1, it adds a new tab t in the TabPane and it adds a new item in the ComboBox cbx contains the same name of the tab. The problem is i don't know how to get the item from cbx and much the name of the item with the same name of the tab and then Do something so how i can do this with javafx and thanks very much :)

Take a look at the API:
http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ComboBox.html
There's a comboBox.getValue() method which gives you the selected item.

To get the item selected by a user, use the getValue() method, you do that in an event handler that respond to a button click.
public void btnSelected()
{
String message = "You chose ";
message += cbo.getValue();
System.out.println(message, "Your Choice ");
}

Related

WPF ListBoxItem Events

One of those 'Why is this so hard?" questions.
I have a ListBox (containing details of share portfolios). The listbox item uses a grid to display attributes of the portfolio. Source is a list of portfolios in the View Model.
ListBox is multiselect - when selection changes, a list of the constituents of the selected portfolios is re-populated.
What I want to do is put a button (or menu or whatever) on the listboxitem to display a list of possible actions (Trade, Unitise, Delete etc).
When an action is selected I need to execute the action against the appropriate portfolio. Ideally I want the actions to be available for both selected and unselected items.
I can handle the event, but how do I detect which item (portfolio) the user selected? I've looked at GotFocus() but it doesn't seem to fire.
In other words if a control in a Listboxitem, fires an event, how does the event 'know' which ListBoxItem raised it?
For me, the solution here, seen as you mentioned MVVM, would be to have the ListBox populated by a collection of ViewModels, e.g., something like ObservableCollection<PortfolioViewModel>.
It would then just be a case of binding the Command property of the Button to an ICommand on the ViewModel that executes whatever work you need doing.
I can handle the event, but how do I detect which item (portfolio) the user selected? I've looked at GotFocus() but it doesn't seem to fire.
You could cast the DataContext of the clicked Button to the corresponding object in te ListBox, e.g.:
private void DeleteButton_Clicked(object sender, RoutedEventArgs e)
{
Button deleteButton = sender as Button;
var portfolio = deleteButton.DataContext as Portfolio; //or whatever your type is called
//access any members of the portfolio...
}

Silverlight how do I add a "select all" option to a checkbox list?

I have a checkbox list in Silverlight. It's actually a Telerik rad combo box with checkboxes in it.
What I'm trying to do is add an initial item to that list with the label "Select All". When the user clicks on that item it will select or deselect the items in the list. In addition, when the user deselects on of the items it should deselect the "Select All".
The problem is that I have a CheckedItemsChanged event that fires when an item in the list is changed. If I try to change the list during that event it complains that I can't change a collection while in the collection changed event.
Is there another way I can do this?
I'm guessing you're attempting to do something like
void SomeComboBox_CheckedItemsChanged(object sender, SomeEventArgs e)
{
// Do stuff with checked items in list
}
Does it help if you use Dispatcher.BeginInvoke to do the stuff involving the checked items, i.e. something like the following?
void SomeComboBox_CheckedItemsChanged(object sender, SomeEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
// Do stuff with checked items in list
});
}

WPF DataGrid Button Click in New Line Gets the wrong line in Current Item

I have a WPF DataGrid with a button on one of the columns.
When I click the button I have this function called:
MyClass mySC = (MyClass)(CollectionViewSource.GetDefaultView(grdMyClass.DataContext).CurrentItem);
This code works perfect, but when I click on the button on the new line (the last on on the grid) I get the msSC of the line before it, and not null, or something that related to the last new line.
how can I check if the button was clicked in the new line ?
If your intention is that clicking on a button should do something with the data bound to that row then just get the data from the button's data context. If null then user clicked on an empty row.
private void Button_Click(object sender, RoutedEventArgs e)
{
MyClass data = (sender as FrameworkElement).DataContext as MyClass;
}
I just got the exact same problem and created my own solution.
I have a DataGrid with ItemsSource binded to a list in my ViewModel, and SelectedIndex binded to an int in the viewmodel, in order to be able to play with the list when we choose something (and for example simulate an event such as "OnSelectionChanged")
So the solution is very simple here:
//App selected on the list
private int _selectedApp;
public int SelectedApp
{
get { return _selectedApp; }
set
{
if (value == _listApps.Count) _selectedApp = -1;
else _selectedApp = value;
OnPropertyChanged("SelectedApp");
}
}
I just check that the index isn't out of range: if it is, I set it to -1, so my app considers nothing is actually selected.
Hope this can help, feel free to ask more :)

How to pass selected radio button in the next window and show that as selected in WPF?

I have a WPF Window with a datagrid dgSample. it has been bound to a list lstSample like this:
dgSample.itemssource=lstSample;
this datagrid also has a radio button column wherein i select one row by clicking on the radio button, and then, i can move to the next page after i click on the next button. On the next page, there is again the same datagrid, with the same radiobutton column. What I want is, that when i reach this page, i want the radio button that was selected in the previous page to be selected here as well.
I have tried binding the radiobutton column with an IsSelected Property by doing:
IsChecked="{Binding Path IsSelected, Mode=TwoWay}"
but this is not working.
What can I do to make it work?
P.S.: I prefer code-behind solution than the xaml one.
Please help !
Your model needs to implement INotifyPropertyChanged and call
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"))
to get it to update in another view.
NB: If you set
public event PropertyChangedEventHandler PropertyChanged = delegate { };
you won't have to check for null.

Input validation in WPF

i am developing an application in wpf using MVVM design pattern. i have a listbox when an item is slected then a dialog is open having the same record in editable mode. this dialog is binded with the selected item of the list. i have apply the validation rule for textbox using IDataErrorInfo. when the user update a record on dialogbox then at every key press, the selected record in listbox is also changed. if the user press save button then i submit changes to database. but if user click cancel button then i do not submit changes to database but the list box is updated with the current updation in GUI. when i refresh the list then old value appears again. My requirement is to update the listbox only when the user hit the save button but not on every key press on dialog box. I first fill the generic list with the linq to sql classes then bind the listbox with it. Please let me know what i have to do.
Thanks in advance
The problem is that you are editing the same object on both forms. You should pass the SelectedItem to the dialog form, but then re-query the database for the item that was passed to the constructor. This does two things: allows you to cancel the changes when the object has been edited, and provides the user with the most current data from the database.
Think of it this way... If the listbox contained data that was even a few minutes old, your user would be modifying data that may have already changed by another user running your application.
Once the user saves (or deletes) the record in the dialog form, you must then refresh the listbox. Typically I use the following method:
DialogViewModel:
// Constructor
public DialogViewModel(MyObject myObject)
{
// Query the database for the required object
MyObject = (from t in _dc.MyObjects where t.ID == myObject.ID
select t).Take(1).Single();
}
// First define the Saved Event in the Dialog form's ViewModel:
public event EventHandler Saved;
public event EventHandler RequestClose;
// Raise the Saved handler when the user saves the record
// (This will go in the SaveCommand_Executed() method)
EventHandler saved = this.Saved;
if (saved != null)
saved(this, EventArgs.Empty);
ListBox ViewModel
Views.DialogView view = new Views.DialogView();
DialogViewModel vm = new DialogViewModel(SelectedItem); // Pass in the selected item
// Once the Saved event has fired, refresh the
// list of items (ICollectionView, ObservableCollection, etc.)
// that your ListBox is bound to
vm.Saved += (s, e) => RefreshCommand_Executed();
vm.RequestClose += (s, e) => view.Close();
view.DataContext = vm;
view.ShowDialog();

Resources