Empty combo box when editable is true - javafx

I want to auto select the first element in a combo box:
final ComboBox selectStatus = new ComboBox();
selectStatus.getItems().addAll(
"Active",
"Blocked",
"Suspended"
);
selectStatus.getSelectionModel().select(0);
selectStatus.setEditable(true);
But when I add editable=true the combo box is empty. Can I solve this problem somehow?

Do like this:
//first set it editable
selectStatus.setEditable(true);
//then, set the value of the first item
selectStatus.getSelectionModel().select(0);
When you set it editable the values displayed is cleared, so you have to set the value after set it editable.
See the javadocs.

Related

How do I get a new value from an editable combo box?

I am trying to add a value to an editable combo box called nameComboBox that I created in the Scene Builder.
I populate the combo box with this code:
private ObservableList<String> getNames()
{
return (FXCollections.observableArrayList("Freddy","Kerstin"));
}
..
nameComboBox.getSelectionModel().select(getNames());
I have a Save button defined form the Scene Builder. The code looks like this:
#FXML
private void handleSaveBtn()
{
System.out.println("The new name is " + nameComboBox.getValue());
}
When the scene is displayed, the combo boxes editable field is displayed empty with the two names hidden in the list underneath the empty field, which is what I want to happen.
If then type "Rusty" in the empty field and click a save button all that happens is that the println statement returns
"The new name is null".
If I wanted to do something with the new value, like validate it or store it in a database, how do I get the value that I entered in the editable field?
Try using this instead of .getValue() :
nameComboBox.getEditor().getText()
This returns the value of the textProperty of the TextField (.getEditor()) of the editable ComboBox.
try this
nameComboBox.setItems(getNames());
nameComboBox.setValue("Freddy");

The default '---Select--' is not coming in the razor drop-down while loading that drop-down on button click

The value is getting populated in the drop-down but the default value --select-- is not coming on top of drop-down populated list.
#Html.DropDownListFor(model => model.HoursVal, new SelectList(Enumerable.Range(0, 24)), "--select--")
I think your issue is that HoursVal is an integer so it's default value is 0 and since your select list is new SelectList(Enumerable.Range(0, 24)) which has 0 value as well it will always be selected in case that HoursVal property is not set for a specific value. If for example HoursVal was int? or a string and you won't set is to any value (leave it null) your dropdown will select the --select-- option.

How to set selected item in QComboBox with QtreeView

i have the following code for QComboBox with WtreeView set as combo view
this->db->select("SELECT top 10 company, address, phone, id FROM data");
QTreeView *ptv = new QTreeView(this);
ptv->setModel(this->db->model);
ptv->setColumnHidden(3, true);
ui->comboBox->setModel(this->db->model);
ui->comboBox->setView(ptv);
connect(ui->comboBox, SIGNAL(activated(int)), this, SLOT(getComboIndex(int)));
How can i set selected item or index for column 2 for example. I can set the first column with
ui->comboBox->setCurrentIndex(index);
but this is not working for other column only for the first.
Try setting the model column to the one you want to change:
ui->comboBox->setModelColumn(2);
ui->comboBox->setCurrentIndex(index);

Change databound Drop Down List programmatically

I have a drop down list that is populated in the page load event from a database table.
The drop down has a DataTextField set to a project name and the DataValueField set to the project id (interger).
Later I change the dropdowlist selected item with this code in the selectedindexchanged event of a gridview
GridViewRow row = GridView1.SelectedRow;
ddlProjectList.SelectedItem.Text = row.Cells[2].Text;
Does Changing the drop down list with this code cause the DataValueField property to change to the correct Project ID number also? If not is there a better way to do this?
=============================================EDIT
actually this code seems to be adding an additional item to the list so that the project i set with this code is listed twice so I don't think my code is correct
This does not change anything else than the text of the selected item in your DropDownList. Do you want to change the text and value of the project, or do you want to select the project from the DropDownList that relates to the selected row in the grid?
To change the project name and id, you'll have to change it in the data source behind the ProjectList. But if you just want to select the related project, you can use this:
var row = GridView1.SelectedRow;
if (ProjectList.Items.FindByText(row.Cells[2].Text) != null)
{
ProjectList.ClearSelection();
ProjectList.Items.FindByText(row.Cells[2].Text).Selected = true;
}
Setting SelectedItem.Text does not actually change the selected item by its text, it changes the text of the currently selected item. You need to use ddl.Items.FindItemByText (or it may be worded FindByText, I forget at the moment) to find the item, then set the ListItem.Selected property to true.
HTH.
You can do
ddlProjectList.FindByText(row.Cells[2].Text).Selected = true;
This will actually set it.

How do I set the selected value in an ASP.NET Drop Down List?

I'm using an ASP.NET dropdown. I want to set the selected element using an ID (Id) which is stored in the DataValue field.
ddlTest.DataValueField = "Id";
ddlTest.DataTextField = "Name";
ddlTest.DataSource = searchResultList;
ddlTest.DataBind();
But all I can see is the SelectedIndex property which isn't the correct thing. How do I set by value?
After it's bound you can set the SelectedValue property to the correct id.
ddlTest.selectedValue = [whatever value you want to set];
If you need the text of the selected item then you can use
dropdwonlistID.SelectedItem.Value // selecting value of the selected item
dropdwonlistID.SelectedItem.Text // selecting displayed text of the selected item

Resources