在JTable 中如果隐藏一列?
tring[] title = {"输入码","科室名称","医保科室代码"};
DefaultTableModel deptTableModel = new DefaultTableModel(0,3);
JTable deptTable = new JTable(deptTableModel);

TableColumnModel columnModel = deptTable.getColumnModel(); 
TableColumn col = columnModel.getColumn(2);
col.setMaxWidth(0);
col.setMinWidth(0);
col.setWidth(0);
col.setPreferredWidth(0);
我想隐藏列名为"医保科室代码"的这一列
请教。在这做工程时需要用到很多参数。必须解决这样的问题。谢谢大家帮助

解决方案 »

  1.   

    参照一下别人的代码import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JTable;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableColumnModel;public class HideTableColumnListener extends MouseAdapter
    implements ActionListener
    {
    private JTable table = null;
    private JPopupMenu hideColumnPopup = null;
    private HashMap menuItemColumnMap = null;
    private HashMap columnWidthMap = null;
    /**
    * Default Class Constructor.
    * @param table the JTable to which the listener is been added.
    */
    public HideTableColumnListener(JTable table) 
    {
    this.table = table;table.getTableHeader().addMouseListener( this );

    /**
    * Method used to dispose of system resources allocated by the class. It
    * removes all Listeners from the JTableHeader and the JCheckBoxMenuItems.
    * <br><br> <b><i>THIS METHOD MUST BE CALLED WHEN THE ASSOCIATED JTABLE 
    * OBJECT IS NO LONGER IN USE (table is been disposed).</i></b>
    */
    public void dispose()
    {
    if( null != table )
    {
    table.getTableHeader().removeMouseListener( this );
    table = null;
    }if( null != menuItemColumnMap )
    {
    Iterator list = menuItemColumnMap.keySet().iterator();while( list.hasNext() )
    {
    ((JCheckBoxMenuItem)list.next()).removeActionListener(this);
    }menuItemColumnMap.clear();
    columnWidthMap.clear();
    }
    }
    /**
    * Method called by the garbage collector when it determines that there are 
    * no more references to the object. It calls the dispose() method to 
    * dispose of system resources.
    * @exception Throwable - the Exception raised by this method
    */
    protected void finalize() throws Throwable
    {
    dispose();
    }/**
    * Implementation of ActionListener interface used by the menu items to 
    * hide columns.
    * @param evt an ActionEvent
    */
    public void actionPerformed(ActionEvent evt) 
    {
    Object source = evt.getSource();
    TableColumn column = null;if( null != (column = (TableColumn)menuItemColumnMap.get(source)) )
    {
    JCheckBoxMenuItem item = (JCheckBoxMenuItem)source;if( item.isSelected() )
    {
    Integer i = null;if( null != (i = (Integer)columnWidthMap.get(column)) )
    {
    column.setPreferredWidth( i.intValue() );
    }
    }
    else
    {
    column.setPreferredWidth( 0 );
    }}
    }
    /**
    * Implementation of MouseListener interface used to show the popup menu.
    * @param e a MouseEvent
    */
    public void mouseClicked(MouseEvent e)
    {
    if(((e.getModifiers() & e.BUTTON3_MASK) != 0) || e.isPopupTrigger())
    {
    if( (null == hideColumnPopup) )
    {
    initPopupMenu();
    }hideColumnPopup.show( e.getComponent(), e.getX(), e.getY() );
    }
    }/**
    * Initialize the JPopupMenu.
    */
    private void initPopupMenu()
    {
    hideColumnPopup = new JPopupMenu("Show Columns");TableColumnModel model = table.getColumnModel();
    TableColumn column = null;
    JCheckBoxMenuItem item = null;menuItemColumnMap = new HashMap();
    columnWidthMap = new HashMap();int len = table.getColumnCount();
    for(int i=0; i<len; i++)
    {
    column = model.getColumn( i );
    column.setMinWidth( 0 );String str = column.getHeaderValue().toString();columnWidthMap.put( column, new Integer(column.getPreferredWidth()) );;item = new JCheckBoxMenuItem(str);
    item.setSelected( true );
    item.addActionListener(this);
    hideColumnPopup.add( item );menuItemColumnMap.put( item, column );
    }
    }} // End class HideTableColumnListener
      

  2.   

    你自己定义一个Model, 不同情况下显示不同的列.