import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class LockColumn extends JFrame
{
class SharedModel extends AbstractTableModel{
public int getRowCount(){return 10;}
public int getColumnCount(){return 10;}
public Object getValueAt(int r,int c){
return "("+r+","+c+")";
}
}
TableModel sharedModel=new SharedModel();
JTable table=new JTable(sharedModel),
headerTable=new JTable(sharedModel);
TableColumnModel tcm=table.getColumnModel();
TableColumn firstColumn=tcm.getColumn(0);
public LockColumn(){
setSize(600,480);
Container cp=getContentPane();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
headerTable.getTableHeader().setReorderingAllowed(false);
headerTable.setPreferredScrollableViewportSize(
new Dimension(firstColumn.getPreferredWidth()+headerTable.getColumnModel().getColumnMargin(),0));
cp.add(new ControlPane(),BorderLayout.NORTH);
cp.add(new JScrollPane(table),BorderLayout.CENTER);
}
public static void main(String argv[]){
new LockColumn().show();
}
class ControlPane extends JPanel{
JCheckBox checkBox=new JCheckBox("First Column Locked");
public ControlPane(){
add(checkBox);
checkBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JScrollPane scrollPane=(JScrollPane)SwingUtilities.getAncestorOfClass(JScrollPane.class,table);
if(checkBox.isSelected()){
tcm.removeColumn(firstColumn);
scrollPane.setRowHeaderView(headerTable);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,headerTable.getTableHeader());
}else{
tcm.addColumn(firstColumn);
int numCols=tcm.getColumnCount();
tcm.moveColumn(numCols-1,0);
scrollPane.setRowHeaderView(null);
}
table.revalidate();
}
});
}
}
}
//来自于java核心技术 duoshan&2003.8.25
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class LockColumn extends JFrame
{
class SharedModel extends AbstractTableModel{
public int getRowCount(){return 10;}
public int getColumnCount(){return 10;}
public Object getValueAt(int r,int c){
return "("+r+","+c+")";
}
}
TableModel sharedModel=new SharedModel();
JTable table=new JTable(sharedModel),
headerTable=new JTable(sharedModel);
TableColumnModel tcm=table.getColumnModel();
TableColumn firstColumn=tcm.getColumn(0);
public LockColumn(){
setSize(600,480);
Container cp=getContentPane();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
headerTable.getTableHeader().setReorderingAllowed(false);
headerTable.setPreferredScrollableViewportSize(
new Dimension(firstColumn.getPreferredWidth()+headerTable.getColumnModel().getColumnMargin(),0));
cp.add(new ControlPane(),BorderLayout.NORTH);
cp.add(new JScrollPane(table),BorderLayout.CENTER);
}
public static void main(String argv[]){
new LockColumn().show();
}
class ControlPane extends JPanel{
JCheckBox checkBox=new JCheckBox("First Column Locked");
public ControlPane(){
add(checkBox);
checkBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JScrollPane scrollPane=(JScrollPane)SwingUtilities.getAncestorOfClass(JScrollPane.class,table);
if(checkBox.isSelected()){
tcm.removeColumn(firstColumn);
scrollPane.setRowHeaderView(headerTable);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,headerTable.getTableHeader());
}else{
tcm.addColumn(firstColumn);
int numCols=tcm.getColumnCount();
tcm.moveColumn(numCols-1,0);
scrollPane.setRowHeaderView(null);
}
table.revalidate();
}
});
}
}
}
//来自于java核心技术 duoshan&2003.8.25
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货