I am making an application for inserting examination marks into a database. I have managed to use a JTable to display data from the database but now i want to use the same JTable to insert data to the database. how i do that?I will appreciate any assistance given
You could:
1) Allow the user to select a row they want to edit.
2) Allow the user to click a button that opens a new window with textfields that are populated with the data from the selected row.
3) Add a "save" button to this newly opened window which, when clicked, queries the database and "updates" where xyz = row that has been selected.
How are you changing the TableModel from you JTable? what structure are you using, if you are using an ArrayList to fill your Jtable you could use the same structure in order to edit your elements, just create a class that implement a TableModelListener, and override the tableChanged method in order to make the update
Edit: ok so you have to get the model from your JTable,
tbl_report.getModel().getValueAt(row, column); and this will give you the value from a specific cell, since you want all the JTable Values you could make this using a for (indeed two for's) something like this:
ArrayList<Object> value= new ArrayList<Object> ();
for (int column = 0; column < resumenTable.getColumnCount(); column++) {
for (int row = 0; row < resumenTable.getRowCount(); row++) {
value.add(resumenTable.getModel().getValueAt(row, column));
}
}
You will have to cast the ArrayList elements in order to construct your query
Related
Several items in the table should be selected and erased when the button is pressed on ArrayList. But only one item is being deleted.
for (int i = 0; i < Table.getRowCount(); i++) {
if (Table.isRowSelected(i)) {
TableData.remove(i);
}
}
Table.setModel(new DemoTableModel(TableData));
You should NOT be removing data from the ArrayList.
The ArrayList can be used to load data into the DefaultTableModel but after you add the TableModel to the table all changes to the data should be done via the DefaultTableModel.
So in your case you would use:
model.removeRow(...)
method of the DefaultTableModel.
See: How to delete multiple rows from JTable , database at a time for a working example that deletes all selected rows from the DefaultTableModel.
If you are using a custom TableModel, then the custom model should implement a removeRow(...) method. See Row Table Model for a step-by-step example that creates a custom TableModel using an ArrayList to hold the data. It show how to implement the "remove row" method.
I'm working on a java desktop application and I have a Swing JTable for displaying data. I need to have a column for displaying row index for each row in the table. I don't want to add index in the grid data. Here is the way that I'm using to fill my table:
private DefaultTableModel defaultTableModel = new DefaultTableModel();
defaultTableModel.setDataVector(dataArray, headerArray);
jTable.setModel(defaultTableModel);
JTable displays what the TableModel tells it to display. Instead, extend AbstractTableModel. In your implementation of getValueAt(), return the row number for column zero and return the appropriate element of dataArray otherwise. Related examples may be found here and here.
You can add a row header to the scroll pane to display numbers for each row in the table.
Any component can be added to the row header. So one solution is to add a second (custom) JTable that just displays row numbers with a custom renderer so that the number look like the column headers.
Check out Row Table Number for an example of this approach.
I have a very odd issue with JTable.
I put data in JTable from DB. When user double clicks on any cell, it copies the cell contents of the first column of the row where user double clicked. So far, it works perfect.
The problem arises when the user sorts the JTable by clicking on the header. when the table has been sorted and now when the user double clicks on any row, it doesn't what is currently stored on that row's first column. It copies what was originally stored on the first column of that row when the JTable was not sorted.
Any ideas?
Problem:
The problem here is that you are getting the initial rows indexes in the JTable TableModel, and not the relevants row indexes shown in the table view.
Solution:
You can map the shown indexes of a sorted jTable with their relevants ones in the dataModel using convertRowIndexToModel(index) method which takes in input the row index in the view and returns the index of the corresponding row in the model.
Let's say that you have the following jTable:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));
And then loop throught model indexes and use this method with each index to get its corresponding one in the TableModel:
table.getRowSorter().convertRowIndexToModel(0); // index 0 here
As suggested in How to Use Tables: Sorting and Filtering, "When using a sorter, always remember to translate cell coordinates." Most likely, you have neglected this in your event handler.
Try Sorting your JTable TableModel data too. Jtable -> TableModel is the one which hold the actual data. JTable is just a view.
I am creating a program that filters the contents of a JTable using two columns. I have used a RowSorter and everything works fine - at least according to what I can see (the view).
Each row on the table can be double clicked to open a dialog. This dialog allows you to edit information on the table. However, the information on the dialog is still of that of the original view (before filtering was used).
Example:
Before filtering the first row on the table is row a.
After filtering the first row on the table is row b.
However, when I double click to open the dialog on the first row (after filtering, which should now be row b, the dialog for row a opens.)
[Hope I've explained this well enough]
I think my problem is that the model isn't being updated after the filters have taken place. I've tried:
for(int i = 0; i < table.getRowCount(); i++){
table.convertRowIndexToView(i);
}
and
for(int i = 0; i < table.getRowCount(); i++){
table.convertRowIndexToModel(i);
}
I am also getting a similar problem when I sort the table using the column headers.
How do I fix this problem?
I think my problem is that the model isn't being updated after the filters have taken place
The model is never updated.
Only the view is updated to show the data from the model in a sorted/filtered order.
Each row on the table can be double clicked to open a dialog.
So I assume you copy the data for one row to the dialog so it can be edited and then added back to the original model.
Therefore you need to use the convertRowIndexToModel(...) to get the model row number so you can access the model data to be displayed on the edit dialog. You would only do this for the row to be edited, not the entire table.
please see tutorial about JTable, there is excelent example about Comparator and good explanations about how to use the TableModel too
After you change data you can refresh table as below;
for(int i = 0; i < table.getRowCount(); i++)
{ table.convertRowIndexToModel(i); }
table = new jTable(myModel);
or
myModel.fireTableDataChanged()
Hope it helps
Burak
I have a jcombo box which has some items like "schoolbooks","collegebooks","historybooks".and i have dynamic ArrayList object of corresponding books...When i click the combo box item "schoolbooks" or "historybooks" ,it should display the contents in JTable from the arraylist.while every action performing,the JTable has to display the contents of the corresponding item of 'schoolbooks' or 'historybooks' .it should not append new rows when every action is performing ...i have used default table model in this.but when i add 3 or 4 rows using default table model,its appending the row with previous here..if i use removeRow(i) in for loop ,its removing 1 row or 2 rows only...Its not removing previous all rows suppose if i have 7 rows ..i am not able to solve this..please if anyone know this,please help...
You might want to review How to Use Tables as a guide to preparing your sscce. As you are using DefaultTableModel, you'll need to show how you construct the Object[] passed to addRow() and how you calculate the index passed to removeRow().
if i use removeRow(i) in for loop ,its removing 1 row or 2 rows only...Its not removing previous all rows suppose if i have 7 rows
When you remove multiple rows you need to remove the row from the end of the table down to 0:
for (int i = table.getRowCount() - 1; i > 0; i--)
{
// add logic here
}