Saving after Deleting a Row from a Jtable - java

I have a JTable, which is a DefaultTableModel. The data in the table is accessed from the ArrayList CarList. The problem I am having, is that after I delete the row, it is only deleted temporarily. To delete the row in the project, the user has to select a row and then press the button delete. When I use the coding that I am using, the row is deleted from the jTable, but the data is not removed from my Arraylist, so when I open up the JTable again, the row that I deleted is still there. Can anyone please help me? I have some coding here :
ArrayList CarList = CarRentalSystem.CarList;
UsingFiles CarFile = CarRentalSystem.CarFile; //ArrayLists accessed from the whole project
/**
* Creates new form ViewCars
*/
public ViewCars() { //creating the table and accessing the data from the arraylists
initComponents();
this.CarList=CarList;
DefaultTableModel model = (DefaultTableModel) carTable.getModel();
for (int i=0; i<CarList.size(); i++){
Car car = (Car) CarList.get(i);
model.addRow(new Object[]{car.getCarNum(), car.getManufacturer(), car.getModel(), car.getBooked(), car.getAircondition()});
btnEdit.setVisible(true);
btnDelete.setVisible(false);
btnSave.setVisible(false);
}
//delete button coding
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) this.carTable.getModel();
int[] rows = carTable.getSelectedRows();
for(int i=0;i<rows.length;i++){
model.removeRow(rows[i]-i);
JOptionPane.showMessageDialog(null, "Row Deleted"); //the row is deleted but the data isn't
}
}
}

Each time you create the JTable model you are taking all of item in your car list. You delete them from the table model, but you never remove the items from your array list. You need to modify your deletion code to remove from the carList.
You should also be naming your variables using lower camel case conventions.
I would create your own table model that contains the car list so that the table model itself can directly remove things from the car list. Unless you have use cases for the car list not being in sync with what you display in your JTable.

Here you are failing to remove the items from ArrayList, therefore every time you create the JTable model all the items from CarList are added to JTable.
You need to add
CarList.remove(Object_To_Remove);
to your delete code.
For more help on how to delete objects from ArrayList see this. Remove Item from ArrayList this question on StackOverflow may help you. Or better way you can directly go for Docs.

Related

Correct method to swap two rows in the SWT table

I am using the SWT library in my software, not in Eclipse. I need to swap two rows at runtime in a table SWT that I populated via an array. I used the following method:
I take the array
swap two elements(with Collection.swap)
I empty the table
I reinsert the elements from the array
I'd like to know if there is a better method, do I have to reinsert all the elements in the table? because when there are so many rows the visual effect is evident and it seems a waste since I would like to swap only two rows at a time.
Here is the code:
if(table.getSelectionIndices()[0]<table.getItemCount()&&table.getSelectionIndices()[0]>0) {
Collections.swap(pluginsTmp, table.getSelectionIndices()[0], table.getSelectionIndices()[0]-1);
int nextSelectionIndex=table.getSelectionIndices()[0]-1;
updateTable();
table.setSelection(nextSelectionIndex);
}
In this case it serves to "go up" the selected item, exchanging it with those above
public void updateTable() {
table.removeAll();
for(int a=0;a<pluginsTmp.size();a++) {
Plugin p = pluginsTmp.get(a);
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0,p.getClass().getName().toString());
item.setText(1,p.getVersionMajor()+"."+p.getVersionMinor()+"."+p.getVersionBuild());
item.setText(2,p.getAuthor());
item.setText(3,(p.isEnabled())?"Enabled":"Disabled");
item.setData("className",p.getClass().getName().toString());
item.setData("status",(p.isEnabled())?"1":"0");
}
}
Thank you all
Just call setText and setData on the two table items that have changed.
You can get an existing TableItem from the table using
TableItem item = table.getItem(index);
where index is the row in the table.

How to change Static jtable limit to dynamic (static object array to dynamic)

the size of table limit is set as static limit and i want to change that to dynamic
here jtable & object declared.
public JTable issuetable = null;
static Object[][] data;
here is my jtable
public JTable getIssues() {
issuetable = new JTable();
String[] colName = {"Member", "Book", "Issue Date", "Return Date ",
"Remarks" };
List<Issue>issues=ServiceFactory.getIssueServiceImpl().findAllIssue();
the size of issuedata is limited to 100000 .. i want to change the limit to dynamic..
data=new Object[issues.size()][100000];
for(Issue issue:issues){
data[i][0]=issue.getMemberId().getName();
data[i][1]=issue.getBookId().getName();
data[i][2]=issue.getIssueDate();
data[i][3]=issue.getReturnDate();
data[i][4]=issue.getRemark();
data[i][5]=issue;
i++;
}
if u know answer please share here..
In your previous question, You were using a DefaultTableModel. Keep in mind, a TableModel is a data structure in itself. There is no need at to store the data in two data structures, i.e. your data[][] and the DefaultTableModel. The underlying structure of DefaultTableModel is a dynamic Vector of Vectors.
What you can do is this. Just declare your DefaultTableModel with 0 rows, using this constructor
public DefaultTableModel(Object[] columnNames, int rowCount)
Then just add rows dynamically to the structure with
public void addRow(Object[] rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
So basically, your declaration will be like this
String[] colName = {"Member", "Book", "Issue Date", "Return Date ", "Remarks" };
DefaultTableModel model = new DefaultTableModel(colName, 0);
JTable table = new JTable(model);
Then just add rows like
String member = "Stack";
String book = "overflow";
Data issueDate = date;
....
Object[] row = { member, book, issueDate, returnDate, remarks };
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(row);
Please read the DefaultTableModel api documentation to see more constructors and methods available
Instead of an array, use a dynamically resizable data structure in your implementation of AbstractTableModel. This EnvDataModel is an example that contains a Map<String, String>.
Instead of copying all the data from your List to the DefaultTableModel you can use your List as the data structure for a custom TableModel. Then you can add/remove Issue object from this TableModel.
See the JButtonTableModel.java example from Row Table Model for an simple example of how the RowTableModel can be extended to give you this functionality.
Using this approach the data is only ever in one place and you can access Issue objects directly from the TableModel.

JTable in Netbeans

I want to add db(MySql) data to a single specific column in jTable when there is three columns. From this below coding the data automatically add to the 1st column, but I want to add it to the 2nd column in Jtable. Please help me ..i'm new to netbeans !!!
Connection con = Driver.connect();
ResultSet rst = Handler.getData(con, "select lec_name from lecturer");
DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
while (rst.next()) {
Object ob []= {rst.getString(1)};
dtm.addRow(ob);
}
Each element in the Object array is a column. This means, you simply means you just need to fill the row array with the correct values
Object ob []= {rst.getString(1), rst.getString(2), rst.getString(3)}};
dtm.addRow(ob);
This of course assumes you've added the appropriate columns to the model in the first place

JTable's and DefaultTableModel's row indexes lose their synchronization after I sort JTable

JAVA
NETBEANS
// resultsTable, myModel
JTable resultsTable;
DefaultTableModel myModel; //javax.swing.table.DefaultTableModel
myModel = (DefaultTableModel) resultsTable.getModel();
// event of clicking on item of table
String value = (String) myModel.getValueAt(resultsTable.getSelectedRow(), columnIndex)
I use JTable and DefaultTableModel to view a table of various info
and I want to get a value of a certain column of the selected index of the table.
The code I wrote above works fine except when:
I use the sort of the GUI (click on the field name I want to sort on the table)
The table is properly sorted but after that when I select a row, it gets
the value of the row that was there before the sort.
This means that after sorting (using the JTable's GUI)
the 'myModel' and 'resultsTable' objects have different row indexes.
How do I synchronize those two?
You need to use the 'convertXXX' methods on the JTable see the JavaDoc
int row = resultsTable.getSelectedRow();
if (row != -1) {
row = table.convertRowIndexToModel(row);
String value = (String) myModel.getValueAt(row, columnIndex)
A problem with using the JTable.getValueAt() is to get the column you want. When the columns are moved around in the GUI the indexes "change" to match the view. By using the AbstractTableModel.getValueAt() and the JTable.convertXXX() (as outlined by Guillaume) it's just a matter of using the column indexes for the model when retrieving data.
Except from the solution Guillaume gave (Thanks)
I did this:
// resultsTable, myModel
JTable resultsTable;
DefaultTableModel myModel; //javax.swing.table.DefaultTableModel
myModel = (DefaultTableModel) resultsTable.getModel();
// event of clicking on item of table
String value = (String) **resultsTable**.getValueAt(resultsTable.getSelectedRow(), columnIndex)
I used the resultsTable Object instead of the myModel Object to get the value.

How to make a columns in JTable Invisible for Swing Java

I have designed one GUI in which I have used one JTable from which I have to make 2 columns invisible . How should I do that ?
Remove the TableColumn from the TableColumnModel.
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) );
If you need access to the data then you use table.getModel().getValueAt(...).
For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager.
First remove the column from the view
table.removeColumn(table.getColumnModel().getColumn(4));
Then retrieve the data from the model.
table.getModel().getValueAt(table.getSelectedRow(),4);
One thing to note is that when retrieving the data, it must be retrieve from model not from the table.
I tried 2 possible solutions that both work, but got some issue with the 1st solution.
table.removeColumn(table.getColumnModel().getColumn(4));
or
table.getColumnModel().getColumn(4).setMinWidth(0);
table.getColumnModel().getColumn(4).setMaxWidth(0);
table.getColumnModel().getColumn(4).setWidth(0);
In my recent case, I preferred the 2nd solution because I added a TableRowSorter.
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
When using table.removeColumn(table.getColumnModel().getColumn(4)), it will physically remove the column from the view/table so you cannot use table.getValueAt(row, 4) - it returns ArrayIndexOutOfBounds. The only way to get the value of the removed column is by calling table.getModel().getValueAt(table.getSelectedRow(),4). Since TableRowSorter sorts only what's on the table but not the data in the DefaultTableModel object, the problem is when you get the value after sorting the records - it will retrieve the data from DefaultModelObject, which is not sorted.
So I used the 2nd solution then used table.getValueAt(table.getSelectedRow(),4);
The only issue I see with the 2nd approach was mentioned by #camickr: "When you set the width to 0, try tabbing, when you hit the hidden column focus disappears until you tab again. This will confuse users."
i had the same problem and because of i am using TableColumnModel removColumn(); does'not help me so i used this
table.getColumnModel().getColumn(0).setWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getColumnModel().getColumn(0).setMaxWidth(0);
and worked fine for me it hide a column 0 and i still able to get value from it
Set the min, max and "normal" width to 0:
jTable.getColumn("ABC").setMinWidth(0); // Must be set before maxWidth!!
jTable.getColumn("ABC").setMaxWidth(0);
jTable.getColumn("ABC").setWidth(0);
Note: Since you can't set a maxWidth < minWidth, you need to change minWidth, first (javadoc). Same is true for width.
The second approach is to extend TableColumnModel and override all the methods to create the illusion (for the JTable) that your model doesn't have those two columns.
So when you hide a column, you must return one less when the table asks for the number of columns and when it asks for the column X, you may have to add +1 to the column index (depending on whether it is to the left or right of the hidden column), etc.
Let your new table model forward all method calls (with the corrected indexes, etc) to the actual column model and use the new table model in the JTable.
If you remove the column from the JTable the column is still present in the TableModel.
For example to remove the first ID column:
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn(tcm.getColumn(0));
If you want to access the value of the removed column, you have to access it through the getValueAt function of the TableModel, not the JTable. But you have to convert to rowIndex back to rowIndex in the model.
For example if you want to access the first column of the selected row:
int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
int value = (Integer)table.getModel().getValueAt(modelRow,0);
I have tried them all: they don't help. The BEST way ever is to make a new table model without the column you want to delete. This is how you do it:
table = (DefaultTableModel) <table name>.getModel();
DefaultTableModel table1 = new DefaultTableModel();
Vector v = table.getDataVector();
Vector v1 = newvector(v,<column index you want to delete>);
Vector newvector(Vector v,int j){
Vector v1= new Vector();
try{
Vector v2;
Object[] o = v.toArray();
int i =0;
while(i<o.length){
v2 = (Vector) o[i];
v2.remove(j);
v1.add(v2);
i++;
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error in newvector \n"+e);
}
return v1;
}
Vector getColumnIdentifiers(int i) {
Vector columnIdentifiers = new Vector();
int j=0;
while(j<i){
columnIdentifiers.add(("c"+(j+1)));
j++;
}
return columnIdentifiers;
}
table1.setDataVector(v1,getColumnIdentifiers((<column count>-1)));
<table name>.setModel(table1);
You could create a subclass of the model, and override TableModel.getColumnCount as follows:
int getColumnCount() {
return super.getColumnCount()-2;
}
The last two columns would then not be displayed in the JTable.

Categories