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.
Related
Ok I have a JTable where I'm displaying a JList on every cell. To do this I had to implement TableCellRenderer and extend DefaultCellEditor. Here is where I return the actual JList to be rendered in GUI.
What I want to do is when user de-selects an item from a JList, I want to also de-select all items for all JLists for that table row starting at the clicked column.
My problem is that I can't figure out a way to de-select all items that come after the current clicked table column. All I can access is this DefaultListModel. I guess I need to access the actual JList in order to remove all selected items.
Below is method I'm using. Any ideas how to do this? Thanks.
public void deselectFromLocation(int row_, int column_){
DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
int cols = dtm.getColumnCount();
for(int i=column_; i<cols;i++){
PCSListModel lm = (PCSListModel) dtm.getValueAt(row_, i);
//How can I access the actual JList object in order to remove all selected items?
//The PCSListMode is DefaultListModel and has no access to JList object. Thanks.
}
}
Presumably, your renderer and editor obtain the existing selection state from your TableModel, perhaps updating an instance of ListSelectionModel that is used as part of preparing the component for use. You can update the other model value(s) in your implementation of stopCellEditing(). Your TableModel will have to fire a suitable TableModelEvent for the other cells; do not do so for the value being edited. A related example is seen here.
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.
Hello I have been working with javax swing and I came across a weird problem and got me questioning.
I can for example:
JTable table = new JTable();
// Indeed, 2 different objects:
// The TableModel (which, i think is supposed to contain rows and columns?
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
// And the column model, supposed to define the columns of a table?
TableColumnModel tcm = table.getColumnModel();
// I can add columns to my table in two different manners
dtm.addColumn("A Column");
// or
TableColumn column = new TableColumn();
column.setHeaderValue("Another column");
column.setWidth(120);
column.setMinWidth(120);
column.setMaxWidth(120);
tcm.addColumn(column);
// And notice that both commands will add a column in the table
// So our table model should now have 2 columns.
// But does it?
System.out.println(dtm.getColumnCount()); // outputs 1;
System.out.println(tcm.getColumnCount()); // outputs 2;
System.out.println(table.getColumnCount()); // outputs 2;
// The visual shows 2 columns, but the model has only 1.
From that I can tell JTable uses tableColumnModel and tableColumnModel gets all the columns added into tableModel, but, when I add a column to the TableModel it gets added to the table, but the tableModel remains outdated.
Now, the problem is: it's really interesting to add a column via columnModel because I can define the sizes, layout, editable options there, but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel.
Any thoughts on this?
The TableModel is used to contain data. The data is accessible in row/columns.
The TableColumnModel is used by JTable to control the View of the data. That is it controls the columns that are displayed in the JTable. You can also reorder the columns to display the data in a different order.
...but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel
That is correct. The purpose of the TableColumnModel is to simply customize the view, not manipulate data.
Maybe you have an application that contains many columns of data, but access to specific columns is limited by "security level". In this case the data is always stored in the TableModel, but you need to change the view to control which columns of data are visible. So you can remove/add columns from the TableColumnModel.
When you add a column to the TableModel, the JTable gets notified and it recreates all the TableColumns for you. This can be a good or bad thing because when the TableColumnModel is recreated you lose any custom renderers and editor that you may have added to a TableColumn. You can prevent this from happening buy using:
table.setAutoCreateColumnsFromModel( false );
Now the TableColumnModel will not be updated and it is your responsibility to manually create and add the TableColumn to the TableColumnModel.
But in general you:
add/change data through the TableModel.
change the view through the TableColumnModel.
I was re designing my codes with formerly using
JTable jtable = new JTable();
int selectedIndex = jtable.getSelectedColumn();
//implementations
Now, I need to use DefaultTableModel. Is there a method or implementation that is related to this method of JTable? Thanks!
A TableModel has nothing to do with the selected column of the JTable.
It doesn't matter what TableModel you use with the JTable, you still use
int selectedIndex = table.getSelectedColumn();
method to get the selected column.
Of course, there is no selected column when you first create the JTable. The user must select the column or you must set the selection on a particular cell of the table.
So you only use the getSelectedColumn() method in response to some kind of user event.
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