automatically display the selected item in jcombobox - java

I'm done with retrieving data from MySQL to JComboBox.
It worked successfully.
Initial
I choose category "OTHERS" over "INGREDIENTS" then saved it.
But when I use the search button, the category display INGREDIENTS instead of OTHERS.
my problem is that I want to display OTHERS (or whatever category I choose)
If this code will retrieve my data in the field:
inventCodeField.setText(rs.getString("ItemCode"));
How can I retrieve the data I selected from the JComboBox without removing the other choices?

After querying the specific record, extract the field value for category then use it as a parameter for setSelectedItem() of JComboBox class.
// assume that category is a String that holds the category value after querying
cbo_category.setSelectedItem(category);

Related

Set combobox items dynamically based on search query

I have a combobox that I would like to act as a search field. When I enter some value I want to call a backend service that would based on the entered value return a list of possible values. I would like to set these values as the combobox items dynamically. So far I have this code:
ComboBox<String> comboBox = new ComboBox<>("Name");
comboBox.setAllowCustomValue(true);
comboBox.setAutofocus(true);
comboBox.addCustomValueSetListener(e -> comboBox.setItems(backendService.getData(e.getValue())));
This works, but it is not ideal, as I (the user) has to type the value, hit enter and focus the combobox again. At this point the matched values from backend are set as items in the combobox. Is there a way to fetch items from the backend dynamically as the user writes in the input with some time delay (e.g. ValueChangeMode.LAZY for TextField) and set them as combobox items while writing user input?
After reading through this link I found a solution using lazy data loading. I had to adjust my backend service to accept a PageRequest object, so the following line of code did exactly what I wanted:
comboBox.setItems(query ->
backendService.getData(PageRequest.of(query.getPage(), query.getPageSize())));

Select all Same jtable value in one click

I am creating a prototype of a payroll for my project, i would like to ask, How can i select all value with the same name and Employee's id in table with just clicking one of them? And all the selected row will pass to another table. Please help me, I just don't have no idea on how to do it?
How can i select all value with the same name and Employee's id
First of all I would suggest that it is the Employee's Id that is unique, so you would want to search the table for the selected id.
Easiest approach would be to "filter" the table based on the Employee's Id. Read the section from the Swing tutorial on Sorting and Filtering. The working example shows how to do a dynamic search as you type text into a text field.
And all the selected row will pass to another table
Now you have all the filtered data in the table to you just iterate through the JTable using the getValueAt(...) method to get the data you want and that add the data to another JTable using the addRow(...) method of the DefaultTableModel of that table.

Select table values to text field using SWT?

I have Table (TableViewer) containing TableItems with values. I have another class TextField mapped according to table column name.
Now my question is: When I select any row in table, the values in that row should get reflected in Text field. In Text field I can edit these vales and save to to table. Please let me know how I can achieve this?
What ever "Baz" has said is the right thing to do. If editing is table data is requirement, then make the table columns as editable. But if you want to specifically edit table row separately in a control (like Text field), then get IStructuredSelection from TableViewer and get the data in control to edit. Then after you edit, get the data back from control and set it back to table. Hope it helps.

For a particular selected value from a drop-down box, Display its corresponding values in drop down list?

I have two fields in a webpage. Both are composite primary keys. Out of that when I select one field (eg. out of a list of countries I select - India), the second field should display all the values corresponding to the previous field (eg. after selecting India I should get all the states in another drop-down menu).
I'm implementing this using JSP and Ajax.
Can someone please help me with this or send me the code snippets.?
Neal,
There are various codes available like this. Just do googling.
Try with link

Binding database column to java jComboBox and JList

Ok, say I have a database table with two columns - one "Name", the other "Age", and there are over 40 names and their respective ages in the table. I want these names to be listed out in a jList/jComboBox, and also I want to be able to click on a name in the jList/jComboBox and have its respective age appear in - say - a text box. Do I have to go about this by simply writing a code that selects all the names from the table and populates the jList/jComboBox and then another code that takes the selected name, puts it in an sql statement, finds the matching age and sends it to a text box, OR is there some kind of a VB-esque column-to-comboBox/List-binding that I can utilise to go about this?
For only 40 name-age combinations, I would just query the database once, and store this information in a Map. Then you can just query the map when a name is selected, and update the age textfield. This will go a lot faster then running SQL queries each time the selection has been changed.
You have to set Model for your swing elements and for updating data based on changes at one place to other implement Listeners.
Have a look at this
Binding comboboxes in swing
Create a custom object that stores both the name and age values and add this object to the combobox. Then when you select an item you have access to both values.
For example: How to use Map element as text of a JComboBox

Categories