原文:https://community.oracle.com/thread/1360505
package gpt.presentation.gui.test;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* @author RONAS1
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FreezeColumnsTest extends JFrame {
Object[][] data;
Object[] column;
TTable table;
public FreezeColumnsTest() {
super( "Fixed Column Example" );
data = new Object[][]{
{ "a","b","c","","","", "" , ""},
{ "a","b","c","","","", "", ""},
{ "a","b","c","","","", "", ""},
{ "a","b","c","d","","","", ""},
{ "a","b","c","","e","","", ""},
{ "a","b","c","","","f","", ""},
{ "a","b","c","","","","g",""},
{ "a","b","c","","","","","h"}};
column = new Object[]{"A","B","C","D","E","F", "G", "H"};
AbstractTableModel model = new AbstractTableModel() {
public int getColumnCount() { return column.length; }
public int getRowCount() { return data.length ;}
public String getColumnName(int col) {
return (String)column[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object obj, int row, int col) {
data[row][col] = obj;
}
public boolean CellEditable(int row, int col) {
return true;
}
};
table = new TTable( model );
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(table,BorderLayout.CENTER );
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(table,BorderLayout.CENTER );
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane( table);
scroll.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
getContentPane().add( scroll, BorderLayout.CENTER);
}
public static void main(String[] args) {
FreezeColumnsTest frame = new FreezeColumnsTest();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
class TTable extends JTable{
public TTable(TableModel dm) {
super(dm);
setTableHeader(new TTableHeader(getColumnModel()));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = this.getColumnModel().getColumn(0).getWidth();
Point pt = getLocation();
if(pt.x < 0){
BufferedImage image = new BufferedImage(w, getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setClip( 0,0,w,getHeight());
super.paintComponent(g2d);
int ox = Math.abs(pt.x);
g.drawImage(image,ox,0,null);
g2d.dispose();
int lx = ox + w;
g.drawLine(lx, 0, lx, getHeight());
}
else{
int ox = Math.abs(pt.x);
int lx = ox + w;
g.drawLine(lx, 0, lx, getHeight());
}
}
}
class TTableHeader extends JTableHeader{
TTableHeader(TableColumnModel md){
super(md);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = this.getColumnModel().getColumn(0).getWidth();
Point pt = getLocation();
if(pt.x < 0){
BufferedImage image = new BufferedImage(w, getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setClip( 0,0,w,getHeight());
super.paintComponent(g2d);
int ox = Math.abs(pt.x);
g.drawImage(image,ox,0,null);
g2d.dispose();
int lx = ox + w;
g.drawLine(lx, 0, lx, getHeight());
}
else{
int ox = Math.abs(pt.x);
int lx = ox + w;
g.drawLine(lx, 0, lx, getHeight());
}
}
}