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
Related
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.
I have created a MySQL database in Netbeans with two tables (varchar and integer), both connected on the same database, and three GUI elements jcombobox, jtextfield, jbutton and jlist. How is it possible, when connecting varchar table to jcombobox, to make jtextfield to remember numerical that is relative to varchar table?
(for example, varchar that is displayed in combobox has columns like
computer,laptop, console and integer has columns 500 (computer),
450(laptop) and 250(console))
and so user can put numerical values in jtextfiled
(500 , 450 and 250 are not displayed but remembered when user selects
one item from combobox)
and after user clicks on button, the value that user has put in jtextfield are being multiplied to numbers in integer table
(whatever user chooses in jcombobox)
and then final result is shown in jlist (or is jtable is better for such data representation?) where is shown choosen item, lets say it was laptop with calculated numbers, for example 4050.
Is it possible, and how, to make those tables to be assigned to different GUI elements and then get together again?
Thank you, and sorry for confusing English.
(500 , 450 and 250 are not displayed but remembered when user selects one item from combobox)
Then you need to store an Object in the combo box that contains two properties:
the description
the number
Then you render the combo box to display the description, but then for later processing you can access the number for calculations.
For an example of this approach check out Combo Box With Hidden Data.
and so user can put numerical values in jtextfiled
Maybe you should use a JSpinner so you get automatic editing to make sure only numeric values are entered.
You can use onFocus event on the jCombobox which then pass the value as a SQL query and fetch the corresponding details and then show that in jTable.If you have larger number of values then I will say use Jtable as for better representation.
Moreover, please add some more details to your question.
I am trying to select an item from the jList in one form (Home), and extract the data from the ArrayList and output the data to separate jTextFields in a different form (Details). Below is the method I'm trying to use to do this (not a lot there I know!).
public void passObjectData()
{
int i = proObjList.getSelectedIndex();
}
I know once the method is complete, I can just call it on the form load method in the next form, but I'm stuck on how to get the method correct.
I don't know what other code, if any, will be needed for your help.
I have hardcoded data into an ArrayList and have output a name to a jList. Now I want to get all of the data of one person that is stored
in the ArrayList (name, address, tel num etc) and put this information
into jTextFields.
As I understand your question this ArrayList is the undelying data structure used to fill the ListModel and you want to get the selected index from the JList to retrieve the correct object stored in that array list. In this case you can:
Have a domain class called Person to hold the person's data (name, address, etc)
Add Person objects to the ListModel.
Provide an appropriate ListCellRenderer to display the person's name.
Use JList#getSelectedValue() to get the selected Person.
Pass this selected Person object to the text field's form and update those accordingly.
Optional: attach a ListSelectionListener to the JList in order to listen for user's selection changes and do the previous step automatically.
See the first 3 points of this approach exemplified here (note: the example is using JComboBox but the same applies to JList as well).
Suggested readings
Creating a Model
Selecting Items in a List
Writing a Custom Renderer
Side note
Not sure if by forms you mean JFrames but just in case: please note that we should avoid using multiple JFrames. See this topic: The Use of Multiple JFrames, Good/Bad Practice?
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);
Could somebody give me a suggestion on how to simply and effectively make GUI representation of directories which are contained inside the database. Now, getting information using SQL queries is one thing. I can do that.
In fact using separate small examples I can put a file inside the database along with his information and I can get the file out of the database. The thing is I was just doing this without GUI, just to test does it work.
Now I need a GUI of this and I really don't know where to start. DO I use JTable, JList or something third? Also, I think I need an multidimensional array because I have, for example, id of a file, name_of_file and size.
So I need different types to put them in: int, String and int.
Also, I need to obviously hide the id of a file from the user yet keep it at the same time in order to be able to reference it.
How do I hide it in a GUI component?
So, let's say that I have a database table for files with these columns:
id, name, size, binary_of_file.
My real table has a bit more of columns like, id of a parent directory, id of a owner, etc. but for now this is not important.
So, I tell database to give me all info about the file (except it's binary because I just want to list the files):
...
ResultSet rs = statementObject.executeQuery("SELECT id, name, size FROM Files;");
while(rs.next()){
//Where do I store the values in? Which GUI component and how?
...
I guess I need an JPanel that will contain this component that will show my files from the database. What component? Please help!
It sounds like you want to use a JTable, it has a TableModel interface that you can implement to adapt to your resultset.
Also follow the link at the top of the docs. to Creating a Table Model.
Perhaps a combined JList/JTable component would fit this need.
That is a screen shot of the GUI of FileBro.
My idea is that the JTree on the left would represent the 'directories' and table (names) of the DB. The JTable on the right would contain the data of the selected table. Change the Locate Open Edit Print buttons for Create Update Delete and the panel below that to show details of records, and it would be the start of a DB CRUD component.