Class TableColumnManager

  • All Implemented Interfaces:
    java.awt.event.ActionListener, java.awt.event.MouseListener, java.beans.PropertyChangeListener, java.util.EventListener, javax.swing.event.TableColumnModelListener

    public class TableColumnManager
    extends java.lang.Object
    implements java.awt.event.MouseListener, java.awt.event.ActionListener, javax.swing.event.TableColumnModelListener, java.beans.PropertyChangeListener
    It will give the user the ability to hide columns and then re-show them in their last viewed position. This functionality is supported by a popup menu added to the table header of the table. The TableColumnModel is still used to control the view for the table. The manager will invoke the appropriate methods of the TableColumnModel to hide/show columns as required.
    Code taken from tips4java. Written by Rob Camick, which states free usage: You are free to use and/or modify any or all code posted on the Java Tips Weblog without restriction. A credit in the code comments would be nice, but not in any way mandatory.
    • Constructor Summary

      Constructors 
      Constructor Description
      TableColumnManager​(javax.swing.JTable table)
      Convenience constructor for creating a TableColumnManager for a table.
      TableColumnManager​(javax.swing.JTable table, boolean menuPopup)
      Create a TableColumnManager for a table.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void actionPerformed​(java.awt.event.ActionEvent event)  
      void columnAdded​(javax.swing.event.TableColumnModelEvent e)  
      void columnMarginChanged​(javax.swing.event.ChangeEvent e)  
      void columnMoved​(javax.swing.event.TableColumnModelEvent e)  
      void columnRemoved​(javax.swing.event.TableColumnModelEvent e)  
      void columnSelectionChanged​(javax.swing.event.ListSelectionEvent e)  
      void hideColumn​(int modelColumn)
      Hide a column from view in the table.
      void hideColumn​(java.lang.Object columnName)
      Hide a column from view in the table.
      void hideColumn​(javax.swing.table.TableColumn column)
      Hide a column from view in the table.
      boolean isMenuPopup()
      Get the popup support.
      void mouseClicked​(java.awt.event.MouseEvent e)  
      void mouseEntered​(java.awt.event.MouseEvent e)  
      void mouseExited​(java.awt.event.MouseEvent e)  
      void mousePressed​(java.awt.event.MouseEvent e)  
      void mouseReleased​(java.awt.event.MouseEvent e)  
      void propertyChange​(java.beans.PropertyChangeEvent e)  
      void reset()
      Reset the TableColumnManager to only manage the TableColumns that are currently visible in the table.
      void setMenuPopup​(boolean menuPopup)
      Add/remove support for a popup menu to the table header.
      void showColumn​(int modelColumn)
      Show a hidden column in the table.
      void showColumn​(java.lang.Object columnName)
      Show a hidden column in the table.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TableColumnManager

        public TableColumnManager​(javax.swing.JTable table)
        Convenience constructor for creating a TableColumnManager for a table. Support for a popup menu on the table header will be enabled.
        Parameters:
        table - the table whose TableColumns will managed.
      • TableColumnManager

        public TableColumnManager​(javax.swing.JTable table,
                                  boolean menuPopup)
        Create a TableColumnManager for a table.
        Parameters:
        table - the table whose TableColumns will managed.
        menuPopup - enable or disable a popup menu to allow the users to manager the visibility of TableColumns.
    • Method Detail

      • reset

        public void reset()
        Reset the TableColumnManager to only manage the TableColumns that are currently visible in the table.

        Generally this method should only be invoked by the TableColumnManager when the TableModel of the table is changed.

      • isMenuPopup

        public boolean isMenuPopup()
        Get the popup support.
        Returns:
        the popup support
      • setMenuPopup

        public void setMenuPopup​(boolean menuPopup)
        Add/remove support for a popup menu to the table header. The popup menu will give the user control over which columns are visible.
        Parameters:
        menuPopup - when true support for displaying a popup menu is added otherwise the popup menu is removed.
      • hideColumn

        public void hideColumn​(int modelColumn)
        Hide a column from view in the table.
        Parameters:
        modelColumn - the column index from the TableModel of the column to be removed
      • hideColumn

        public void hideColumn​(java.lang.Object columnName)
        Hide a column from view in the table.
        Parameters:
        columnName - the column name of the column to be removed
      • hideColumn

        public void hideColumn​(javax.swing.table.TableColumn column)
        Hide a column from view in the table.
        Parameters:
        column - the TableColumn to be removed from the TableColumnModel of the table
      • showColumn

        public void showColumn​(int modelColumn)
        Show a hidden column in the table.
        Parameters:
        modelColumn - the column index from the TableModel of the column to be added
      • showColumn

        public void showColumn​(java.lang.Object columnName)
        Show a hidden column in the table.
        Parameters:
        columnName - the column name from the TableModel of the column to be added
      • mousePressed

        public void mousePressed​(java.awt.event.MouseEvent e)
        Specified by:
        mousePressed in interface java.awt.event.MouseListener
      • mouseReleased

        public void mouseReleased​(java.awt.event.MouseEvent e)
        Specified by:
        mouseReleased in interface java.awt.event.MouseListener
      • mouseClicked

        public void mouseClicked​(java.awt.event.MouseEvent e)
        Specified by:
        mouseClicked in interface java.awt.event.MouseListener
      • mouseEntered

        public void mouseEntered​(java.awt.event.MouseEvent e)
        Specified by:
        mouseEntered in interface java.awt.event.MouseListener
      • mouseExited

        public void mouseExited​(java.awt.event.MouseEvent e)
        Specified by:
        mouseExited in interface java.awt.event.MouseListener
      • actionPerformed

        public void actionPerformed​(java.awt.event.ActionEvent event)
        Specified by:
        actionPerformed in interface java.awt.event.ActionListener
      • columnAdded

        public void columnAdded​(javax.swing.event.TableColumnModelEvent e)
        Specified by:
        columnAdded in interface javax.swing.event.TableColumnModelListener
      • columnMoved

        public void columnMoved​(javax.swing.event.TableColumnModelEvent e)
        Specified by:
        columnMoved in interface javax.swing.event.TableColumnModelListener
      • columnMarginChanged

        public void columnMarginChanged​(javax.swing.event.ChangeEvent e)
        Specified by:
        columnMarginChanged in interface javax.swing.event.TableColumnModelListener
      • columnRemoved

        public void columnRemoved​(javax.swing.event.TableColumnModelEvent e)
        Specified by:
        columnRemoved in interface javax.swing.event.TableColumnModelListener
      • columnSelectionChanged

        public void columnSelectionChanged​(javax.swing.event.ListSelectionEvent e)
        Specified by:
        columnSelectionChanged in interface javax.swing.event.TableColumnModelListener
      • propertyChange

        public void propertyChange​(java.beans.PropertyChangeEvent e)
        Specified by:
        propertyChange in interface java.beans.PropertyChangeListener