Results 1 to 5 of 5
  1. #1
    aznboarder is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default 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
    Java 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);
    				
        }
    }
    removeColumns
    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();
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The for–each loop does not allow changes to be made in the items it accesses/returns.
    Java Code:
        // iterate through each row and remove the specified column
        for (List<Object> row : _rowHashedData)
        {
          row.remove(columnIndices[index]);
        }
    Try the older construct.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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])

    db
    Last edited by DarrylBurke; 04-09-2009 at 10:09 AM.

  4. #4
    cheapcarsauctions Guest

    Default Hello From Poland

    Hello to all ! Great site. I am new here greetings to all from Poland.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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

  1. strange refreshing behavior
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 12-27-2008, 05:51 PM
  2. Issue with Jcheckbox on JTableheader
    By ram_76uk in forum AWT / Swing
    Replies: 2
    Last Post: 07-30-2008, 09:24 AM
  3. Why my JTree always collasped after refreshing?
    By lmsook10 in forum AWT / Swing
    Replies: 2
    Last Post: 06-24-2008, 06:55 AM
  4. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 08:09 AM
  5. how to stop refreshing page
    By cecily in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 02:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •