Select all Same jtable value in one click - java

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.

Related

one column value cannot be greater than another column value in Jtable of java swing

I have 4 columns JTable using Default table model, i am populating two column's from DB, and user have to fill the purchase price and sale price in jTable , i wanna make sale Price cannot be greater than Mrp Please Help me..
In what problem? Can you show any relative code? Any way I recomend you write your custom table model extends DefaultTableModel (http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/table/DefaultTableModel.html), it's more flexible.

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.

automatically display the selected item in jcombobox

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);

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

Creating front-end (Java) user interactive table using MySQL as back-end

I'm a student, intrested in Java programming. Now im trying to create user interactive form.
Untill now, I have created a form view, using swing components, JLable, JTextfield where user enters data. JButtons, 'new' 'save' 'delete' 'edit', which listens user action through ActionListner.
A table that 'extends' AbstractTableModel. Table values are the ResultSet values. i have used MySQL connectivity.
Table is displayed. User can now add new row to it, using 'New' and 'Save' button.
Please watch this
My problem is, i need the corresponding TextFields to display the corresponding row user selects in the displayed table, so that 'edit' and 'delete' would be user interactive. users are allowed to select table's row.
Need Help.
Im sorry if any mistake in my question format and language. Thank you!
on row select event, populate the values of text fields with tables selected row values.
As discussed in How to Use Tables, JTable will supply a default editor for any cell in your TableModel that returns true from isCellEditable(). Just double click on the cell. See also User Selections for more on selection listeners.

Categories