Removing Column from TableModel in Java - java

In Java I'm using the DefaultTableModel to dynamically add a column to a JTable.
//create DefaultTableModel with columns and no rows
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(tableModel);
The columnNames variable is a string array with the column names. So after the program is up and running the user has the option to add additional columns. I do so as follows
tableModel.addColumn("New column name");
Which dynamically adds the column to the table as desired. The user can also remove columns added. For this I use the following code:
TableColumn tcol = table.getColumnModel().getColumn(0);
table.getColumnModel().removeColumn(tcol);
which should remove the column at a specified index, I've also tried:
table.removeColumn(sheet.getColumn(assessmentName));
Both of them work (visually), but here's the problem. After deleting an added column, if another column is added and the table refreshes, the previously deleted column is there again. So while it is removing the column visually, neither of the last two code snippets actually removes it from the model. I'm assuming here that since the column was added to the model that is where it needs to be removed from? Is there a specific method that I need to call or some logic that I need to implement to remove the column?

For your table, try calling table.setAutoCreateColumnsFromModel(false);
This post has a good example as to how to delete column and the underlying data.

I'm assuming here that since the column was added to the model that is where it needs to be removed from?
Yes.
Is there a specific method that I need to call or some logic that I need to implement to remove the column?
No, but you can make up your own method:
moveColumn(...); // to move the column to the end
setColumnCount(...); // to remove the last column
As a side note if you want to give the users the ability to hide/show columns check out the Table Column Manager.

Acting at the TableColumn level, as you show, has only a visual impact but no impact on the TableModel whatsoever.
If you want to really remove a column from DefaultTableModel then you'll need to subclass it and then, in your subclass:
public class MyTableModel extends DefaultTableModel {
public void removeColumn(int column) {
columnIdentifiers.remove(column);
for (Object row: dataVector) {
((Vector) row).remove(column);
}
fireTableStructureChanged();
}
}
I haven't checked it, but it should work in your case.
Of course, removeColumn() should be called only from the EDT.
Note that I wouldn't encourage anyone to produce this kind of code; in particular, using, or deriving from, DefaultTableModel is not the best solution to define a TableModel.

The DefaultDataModel doesn't have a really removeColumn() function, so I wrote a function myself, which can actually solve the problem.
private void removeColumn(int index, JTable myTable){
int nRow= myTable.getRowCount();
int nCol= myTable.getColumnCount()-1;
Object[][] cells= new Object[nRow][nCol];
String[] names= new String[nCol];
for(int j=0; j<nCol; j++){
if(j<index){
names[j]= myTable.getColumnName(j);
for(int i=0; i<nRow; i++){
cells[i][j]= myTable.getValueAt(i, j);
}
}else{
names[j]= myTable.getColumnName(j+1);
for(int i=0; i<nRow; i++){
cells[i][j]= myTable.getValueAt(i, j+1);
}
}
}
DefaultTableModel newModel= new DefaultTableModel(cells, names);
myTable.setModel(newModel);
}

Related

Java swing jTable not being updated

I built a jTable using NetBeans GUI, and I want to update it inside the constructor of the class. I'm planning to add a search option on the frame so the whole update idea is quite critical for me.
My code:
public availableTrumps(TrumpistClient TC){
initComponents();
availableTrumpsTrumpistClient=TC;
String result=null;
String query="SELECT * FROM APP.TRUMPS";
result=this.availableTrumpsTrumpistClient.WritingReading("sql_select", query);
if (result.contains("empty")){
JOptionPane.showMessageDialog(this, "There are now trumps to show.");
}
else if (result.contains("error")){
JOptionPane.showMessageDialog(this, "Error in the connection. Please try again.");
}
else{
int i;
String []data = result.split("\r\n");
String [][] data2 = new String [data.length][];
for (i = 0; i < data.length; i++)
{
data2[i] = data[i].split("&");
}
String[] columnNames = {"From", "To", "Departure Time", "Remaining Places", "Proposer", "ClosingTime", "Cost Per Seat" };
this.jTable1 = new JTable(data2,columnNames);
this.jTable1.setPreferredScrollableViewportSize(new Dimension(500,100));
this.jTable1.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jTable1);
add(jps);
jTable1.revalidate();
}
}
The input two-dimentional array data2 is fine and validated.
I added the last 5 rows of the code to see if they help with something. I don't know if they are mandatory and in any case I do not want to change the graphical properties of the jTable I built with the GUI (just the data in it).
When I run the program, I see that the jTable remains empty.
Why?
I suggest you use a table model, whenever the data changes you change the model. Build the JTable instance only once, not whenever you need to change the data.
As others have said, you don't want to create multiple JTable instances. Create one like this:
DefaultTableModel model = new DefaultTableModel(new Object[0][0],
new String[]{"From", "To", "etc."});
JTable table = new JTable(model);
Then, when you need to add rows, use
model.addRow(dataForThisRow); // Object
If you want to change a cell:
model.setValueAt(newValue, row, col); // Object, int, int
Or, to remove row i:
model.removeRow(i); // int
For more information, see the DefaultTableModel documentation.
If, for some reason, it is imperative that you recreate the table each time, I believe the problem is that you are calling revalidate without calling repaint.

Clearing data from one JTable is also deleting the another JTable

I am doing an application in Java using Swing. I have two tables and I have to copy contents from one table to another (Replication.) The problem is if I clear the destination Table rows then my source table rows are also getting deleted.
If I press CopyAll then I will copy all the contents from Table-A to Table-B. If I press clear then I have to clear Table-B. But the problem is Table-A is also getting cleared.
For copying
public void copyAll() {
TableModel tableAModel = tableA.getModel();
tableB.setModel(tableAModel);
repaint();
}
For clearing rows (I am doing for table-B)
public void clearTableB() {
DefaultTableModel clearTableData = (DefaultTableModel) tableB.getModel();
clearTableData.setNumRows(0);
}
I think I am getting problem while copying in copyAll() method. I am getting tableA's Model and then clearing it at clearTable() method.
If the above copyAll() method is wrong please tell me how can I implement copyAll(), removeTableB().
You have copied the TableModel between the two tables. This means the two tables share the same data. If you delete the contents of the TableModel, both tables will loose their data.
You should create two separate TableModel instances, and keep them in sync (for example by using a listener as the TableModel fires events each time the model is updated)
In your copy version, you set the model of the first table to the second table. So the two tables share the same model. You should make a copy of the model :
public void copyAll() {
final TableModel tableAModel = tableA.getModel();
final DefaultTableModel copy = new DefaultTableModel(tableAModel.getRowCount(), 0);
for (int column = 0; column < tableAModel.getColumnCount(); column++) {
copy.addColumn(tableAModel.getColumnName(column));
for (int row = 0; row < tableAModel.getRowCount(); row++)
copy.setValueAt(tableAModel.getValueAt(row, column), row, column);
}
tableB.setModel(copy);
}
Both tables are using the same model. You have to give Table B it's own Model, copy the values manually. Your current copyAll method copies the reference to the Table Model, it doesn't copy the contents.
That is because you shared the TableModel for the two tables. In the copy method, you should create a clone of the Model and use the clone for the second table.
If you are using DefaultTableModel You can get Vector of data from the model using getDataVector() and clone() it.
public void copyAll() {
TableModel tableAModel = tableA.getModel(), tableModelB;
Vector tableModelBDataVector = ((DefaultTableModel)tableAModel).getDataVector();
int tableModelAColumnCount = tableAModel.getColumnCount();
Vector<String> tableModelAColumnVector = new Vector<String>(tableModelAColumnCount);
for (int i = 0; i < tableModelAColumnCount; i++)
tableModelAColumnVector.add(tableAModel.getColumnName(i));
tableModelB = new DefaultTableModel((Vector)tableModelBDataVector.clone(), (Vector)tableModelAColumnVector.clone());
tableB.setModel(tableModelB);
}

How to make a column, row or cell editable in a GlazedLists/JTable in java?

Can any one drop a line of code to show how to make a GlazdJTable's cell editable?
JTable table = new Jtable();
TableFormat tableFormat = GlazedLists.tableFormat(properties, headers);
model = new EventTableModel<Artikel>(filterList, tableFormat);
selectionModel = new EventSelectionModel<Artikel>(filterList);
table.setSelectionModel(selectionModel);
table.setModel(model);
// how to set table cell editable?
Note: I know that TableFormat must implement the WritableTableFormat interface. but i don't know should i create a custom table format or it is possble to set the Jtable cell editable just like a JTable.
Override TableModel's method
public boolean isCellEditable(int rowIndex, int columnIndex) to return true for editable and false for the rest cells.
The recommended way is to use a WritableTableFormat. The EventTableModel checks to see whether the table format is a WritableTableFormat and if so delegates the isEditable() question to that (as described in the EventTableModel docs). Otherwise EventTableModel assumes the table is not editable.
At the moment you're using the GlazedLists.tableFormat() convenience method rather than instantiating your own TableFormat. That's fine, there is a method precisely for this case where you specify whether each column is editable by passing in an array of booleans. See the GlazedLists.tableFormat(String[] propertyNames, String[] columnLabels, boolean[] editable) documentation.

How to handle table which sorting and adding data parallel?

I am facing problem of duplicate rows in the JXTable. If I sort the JXTable data while the new rows are being inserted in JXTable, the final result in JXTable shows duplicate rows that make invalid result in table. Even it also shows correct count of rows that has been inserted but some rows are completely missing whereas some rows are found duplicate in JXTable.
If I sort the JXTable after all data has been inserted successfully then it is showing correct data i.e no duplicate rows and no rows missing.
code example :
I have a method to which I am passing defaultTableModel and here is am adding items in the table
public void addingItems(DefaultTableModel defaultTableModel)
{
for(int i=0;i< numberofItems;i++){
Vector vobject = new Vector();
vobject.add("...");
vobject.add("xxx");
vobject.add("yyy");
...
..
vobject.add("");
defaultTableModel.addRow(vobject);
}
one the other hand I have adding code of sorting at tableHeader actionlistener
tableheader.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
Vector data = defaultTableModel.getDataVector();
Collections.sort(data, new ColumnSorter(colIndex, ascending));
}
});
I have put code in the synchronized block but not getting success.
Please provide a better solution to handle this issue.
I have put code in the synchronized block but not getting success.
Because you have to handle synchronization not only in your sort handler block, but also on each modification operation.
For example:
...
vobject.add("");
synchronized(monitor) {
defaultTableModel.addRow(vobject);
}
}
and
...
Vector data = defaultTableModel.getDataVector();
synchronized(monitor) {
Collections.sort(data, new ColumnSorter(colIndex, ascending));
}
}
In this case your UI could look somewhat unresponsive if you have a lot of data in your table because sort is N^2*logN. If you have somewhat about 200 entries - it wouldn't make a problem for you.

deleting datas in the table in GUI

I have question that how can I delete all datas from my jTable in GUI when a user entered a key?
thanks
You can set a new empty data model:
TableModel newModel = new DefaultTableModel();
jtable.setModel(newModel);
You need to understand that a JTable is a view of the data, while the actual data resides in the TableModel. If you need to clear out the table, then you need to clear out the TableModel.
If your TableModel is an AbstractTableModel, you must provide implementations of 3 methods:
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
Frequently the actual data objects are stored in an additional data structure (e.g. a list), and then the AbstractTableModel queries that list.
List<DomainObject> objects = new ArrayList<DomainObject>();
public int getRowCount() { return objects.size(); }
// How many columns you make depends on what features of the objects you're exposing.
public int getColumnCount() { return NUMBER_OF_COLUMNS; }
public Object getValueAt(int row, int column) {
DomainObject object = objects.get(row);
... // pull out the property based on the column they pass in
}
// By exposing this method, you can allow your Controller code to reach into this model
// and delete all the rows.
public void clear() {
objects.clear()
}
What HH is suggesting you do is change the model of your JTable to reference an empty model, which will in effect clear out the table. However, the columns etc. will not be persisted correctly (the new DefaultTableModel has no idea what those column names would be).
After you've researched how the view and model fit together more, take a look at GlazedLists. It allows a very powerful way to create TableModels which provide dynamic views of your data, e.g. by filtering out rows that do not match certain criteria.
To sum up - you're not going to find a method on the JTable to clear out its contents, because that's the job of the TableModel. You need some way of ensuring that the TableModel's backing data structures are cleared out.
If you are using the DefaultTableModel then you can just use:
model.setRowCount(0);
This is better than creating a new DefaultTableModel. Creating a new TableModel causes the TableColumnModel to be recreated, which means all the TableColumns will be resize to default values and recreated in the order in which the columns exist in the model. The user may have changed these properties and shouldn't be forced to do it again.
If you are just deleting certain rows that contain a particulsar value, then you can use the DefaultTableModel.removeRow(...) method. Make sure you start by deleting row from the end of the model and count down to 0.
call removeAll of j_table method at addActionListener
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
j_table.removeAll();
data_model_table.setRowCount(0);
}
});

Categories