 |
-- Main.aberrant - 12 May 2008
This example demonstrates how to create a user interface that displays a table with "frozen" columns. Frozen means it is not effected by the horizontal scrolling of the rest of the table.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
/**
* NonScrollingColumnsDemo
*
* Creates a table system that has some columns that do not participate in scrolling.
* In effect these columns are "frozen" and do not move.
*
* @author Collin Fagan
*/
public class NonScrollingColumnsDemo extends JPanel {
private static final long serialVersionUID = 1L;
private JTable table = new JTable();
private JTable nonScrollingColumns = new JTable();
/**
* sample data column names
*/
private static final String sportColumn = "Sport";
private String[] columnNames = { "First Name", "Last Name", sportColumn,
"# of Years", "Vegetarian" };
/**
* sample table data
*/
private Object[][] data = {
{ "Mary", "Campione", "Snowboarding", 5, Boolean.FALSE },
{ "Alison", "Huml", "Rowing", 3, Boolean.FALSE },
{ "Kathy", "Walrath", "Knitting", 2, Boolean.FALSE },
{ "Sharon", "Zakhour", "Speed reading", 20, Boolean.FALSE },
{ "Philip", "Milne", "Pool", 10, Boolean.FALSE } };
/**
*
*/
public NonScrollingColumnsDemo() {
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setModel(model);
// if not on 1.6 comment this out
table.setFillsViewportHeight(true);
nonScrollingColumns.setAutoCreateColumnsFromModel(false);
nonScrollingColumns.setModel(model);
nonScrollingColumns.setSelectionModel(table.getSelectionModel());
nonScrollingColumns.setFillsViewportHeight(true);
JTableHeader nonScrollingHeader = nonScrollingColumns.getTableHeader();
nonScrollingHeader.setResizingAllowed(false);
nonScrollingHeader.setReorderingAllowed(false);
TableColumn firstColumn = table.getColumn("First Name");
table.removeColumn(firstColumn);
nonScrollingColumns.addColumn(firstColumn);
nonScrollingColumns.setPreferredScrollableViewportSize(nonScrollingColumns.getPreferredSize());
setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setRowHeaderView(nonScrollingColumns);
scrollPane.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, nonScrollingHeader);
add(scrollPane, BorderLayout.CENTER);
}
/**
* Start the demo on the Event Dispatch Thread.
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
// turn bold fonts off in metal
UIManager.put("swing.boldMetal", Boolean.FALSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame demoFrame = new JFrame();
demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demoFrame.setContentPane(new NonScrollingColumnsDemo());
demoFrame.setSize(300, 200);
demoFrame.setVisible(true);
}
});
}
}
-- KleopatraX - 16 Jun 2008
Slightly disappointed that this simplistic approach which has been around from the very first days in the life of Swing (formerly codeguru, tame, gol - don't know where they are currently hosted) is still recycled without any cautioning comments. The issues are cross-table selection, navigation, sorting/filtering ... and more. The sad fact is, that there is no simple solution to the very common requirement. The best (OS) approach I've seen so far is the xFrame project which compounds two tables into a new non-table component with similar api as the JTable.
Sorry for spoiling the fun
Jeanette
|