It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes How to hide collumns in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to hide collumns in JTable" Watch "How to hide collumns in JTable" New topic
Author

How to hide collumns in JTable

Michal Bury
Greenhorn

Joined: Sep 13, 2006
Posts: 6
I want to hide last four collumns in my JTable. Is there any way to set some collumns in table to be not Visible?
thanks
prashant gour
Ranch Hand

Joined: Feb 07, 2006
Posts: 45
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.table.JTableHeader;

public class FixedTable extends JFrame implements PropertyChangeListener
{
JScrollPane scrTabel1;
JTable tblFixedData = new JTable(200, 6);

public FixedTable()
{
Container container = getContentPane();
container.add(getFixedTable());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}

private JPanel getFixedTable()
{

JTable tableTrade = new JTable(345,8);

scrTabel1 = new JScrollPane(tableTrade);

JTableHeader header = tblFixedData.getTableHeader();

JPanel pnl1 = new JPanel(new BorderLayout());
pnl1.add(header,"North");
pnl1.add(tblFixedData,"Center");

JPanel pnl = new JPanel(new GridLayout(1,2));
pnl.add(pnl1);
pnl.add(scrTabel1);

scrTabel1.addPropertyChangeListener(this);

pnl.setPreferredSize(new Dimension(300,300));

tblFixedData.setSelectionModel( tableTrade.getSelectionModel() );
tblFixedData.getTableHeader().setReorderingAllowed( false );
tblFixedData.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
tableTrade.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

return pnl;
}

public void propertyChange(PropertyChangeEvent pv){

JViewport viewport = scrTabel1.getViewport();
System.out.println(viewport);
tblFixedData.setPreferredScrollableViewportSize(viewport.getPreferredSize());
tblFixedData.revalidate();
tblFixedData.repaint();
}

public static void main(String[] args)
{
new FixedTable();
}
}

try above code, hope this will give you some clue.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to hide collumns in JTable