Results 1 to 5 of 5
Thread: JTableHeader not refreshing
- 04-08-2009, 03:13 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
JTableHeader not refreshing
Hello,
I'm implementing a table that allows a person to right click on cells and either delete the entire rows or columns that have been selected. I have no problems modifying the tablemodel to delete the rows or columns, but I can't seem to get the JTableHeader to remove the deleted column names... I made sure to delete the column names in my table model as well. I also tried calling the 'columnRemoved' method on the tableheader to no avail. Here's a sample of my action listener and my removeColumns call in my table model. Does anyone know if I'm missing something? I also tried a revalidate(), repaint(), but no luck.
ActionListener
removeColumnsJava Code:public void actionPerformed(ActionEvent ae) { Object eventSource = ae.getSource(); if (eventSource==_deleteColumnMenuItem) { JScrollPane scrollPane = (JScrollPane) _tabbedDataPane.getSelectedComponent(); JTable table = (JTable) scrollPane.getViewport().getView(); UniqueTableModel tableModel = (UniqueTableModel) table.getModel(); TableColumnModel columnModel = table.getColumnModel(); int[] selectedColumnIndices = columnModel.getSelectedColumns(); tableModel.removeColumns(selectedColumnIndices); TableColumnModelEvent columnModelEvent = new TableColumnModelEvent(columnModel, selectedColumnIndices[0], selectedColumnIndices[selectedColumnIndices.length-1]); table.getTableHeader().columnRemoved(columnModelEvent); } }
Java Code:public void removeColumns(int[] columnIndices) { Arrays.sort(columnIndices); for (int index=columnIndices.length-1; index >=0; index--) { // remove column name _columnNames.remove(columnIndices[index]); // iterate through each row and remove the specified column for (List<Object> row : _rowHashedData) { row.remove(columnIndices[index]); } } fireTableDataChanged(); }
- 04-09-2009, 06:14 AM #2
The for–each loop does not allow changes to be made in the items it accesses/returns.
Try the older construct.Java Code:// iterate through each row and remove the specified column for (List<Object> row : _rowHashedData) { row.remove(columnIndices[index]); }
- 04-09-2009, 08:36 AM #3
hardwired, I don't have a solution but I don't think that's the problem. The code isn't attempting to remove row, it's calling a method of row to remove (columnIndices[index])
dbLast edited by DarrylBurke; 04-09-2009 at 09:09 AM.
- 04-09-2009, 08:47 AM #4cheapcarsauctions Guest
Hello From Poland
Hello to all ! Great site. I am new here greetings to all from Poland.
- 04-11-2009, 04:31 AM #5
I didn't have enough code to test so I made up something simple that does what I think you are trying to do.
Java Code:import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; public class RemoveColumn { JTable table; private void removeColumn(int colIndex) { CustomModel model = (CustomModel)table.getModel(); // Get model index of selected column. Using the // modelIndex avoids trouble if columns have been // dragged/reordered by the user. TableColumnModel colModel = table.getColumnModel(); TableColumn col = colModel.getColumn(colIndex); int modelIndex = col.getModelIndex(); // Make new dataArrays. int newColCount = table.getColumnCount()-1; Vector dataVector = model.getDataVector(); Object[][] newData = new Object[dataVector.size()][newColCount]; for(int i = 0; i < dataVector.size(); i++) { Vector oldRow = (Vector)dataVector.get(i); newData[i] = new Object[newColCount]; for(int j = 0, k = 0; j < oldRow.size(); j++) { if(j == modelIndex) { continue; } newData[i][k++] = oldRow.get(j); } } // Make new columnIdentifiers array. Vector colIds = model.getColIds(); Object[] newIds = new Object[newColCount]; for(int i = 0, j = 0; i < colIds.size(); i++) { if(i == modelIndex) { continue; } newIds[j++] = colIds.get(i); } model.setDataVector(newData, newIds); model.fireTableStructureChanged(); } private JScrollPane getTableComponent() { int rows = 5; int cols = 8; Object[][] data = new Object[rows][cols]; Object[] colIds = new Object[cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { data[i][j] = String.valueOf((i+1)) + (j+1); if(i == 0) { colIds[j] = "Column " + (j+1); } } } table = new JTable(new CustomModel(data, colIds)); Dimension d = table.getPreferredSize(); table.setPreferredScrollableViewportSize(d); table.setColumnSelectionAllowed(true); return new JScrollPane(table); } private class CustomModel extends DefaultTableModel { public CustomModel(Object[][] data, Object[] colIds) { super(data, colIds); } /** columnIdentifiers is protected in DefaultTableModel. */ public Vector getColIds() { return columnIdentifiers; } } private JPanel getControl() { JButton button = new JButton("remove selected column"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Get index of first selected column. // Simplified for sake of brevity. int col = table.getSelectedColumn(); if(col != -1) { removeColumn(col); } } }); JPanel panel = new JPanel(); panel.add(button); return panel; } public static void main(String[] args) { RemoveColumn test = new RemoveColumn(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getTableComponent()); f.add(test.getControl(), "Last"); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } }
Similar Threads
-
strange refreshing behavior
By diggitydoggz in forum New To JavaReplies: 4Last Post: 12-27-2008, 04:51 PM -
Issue with Jcheckbox on JTableheader
By ram_76uk in forum AWT / SwingReplies: 2Last Post: 07-30-2008, 08:24 AM -
Why my JTree always collasped after refreshing?
By lmsook10 in forum AWT / SwingReplies: 2Last Post: 06-24-2008, 05:55 AM -
Bug in refreshing jsp
By anki1234 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-31-2007, 07:09 AM -
how to stop refreshing page
By cecily in forum New To JavaReplies: 1Last Post: 07-24-2007, 01:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks