分类:
2008-09-09 12:42:22
具体思路:
我们可以考虑使用两个JTable(fixTable,mainTable)
定义一个JScrollPane,把mainTable放在他的viewPort中,
在新new一个JViewport,把fixTable放在他的上面
利用JScrollPane的setRowHeaderView()方法,把这个JViewport作为他的RowHeader
最后把fixTable的 tableHeader放在JScrollPane的左上方(利用setCorner方法)
实现选中的同步,因为是2个table,所以我们要人工实现他的同步,使用户开着好象一个table.
有的时候我们可能还要在表格上监听鼠标事件,那么这个我们如何同步呢,我的想法是我们增加鼠标事件时只给mainTable添加,而fixTable的鼠标事件默认调用mainTable的处理方法,从而实现事件的传递。
好了下面我们来看一下代码:
首先是我封装好的FixTable类
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
publicclass FixTable {
private JTable mainTable;
private JTable fixTable;
private FixTableModel fixModel;
private MainTableModel mainModel;
privateintfixColumnCount; // 冻结的列数
private String[] columnHeader; // 表头
private JScrollPane mainScroll;// 装载在父Container的组件
// private Vector tableData = new Vector(); // 表格中数据
private Object[][] tableData;
public FixTable(int fixColumnCount, String[] columnHeader,
Object[][] tableData) {
this.fixColumnCount = fixColumnCount;
this.columnHeader = columnHeader;
this.tableData = tableData;//
this.initComponents();
}
/**
*初始化界面
*/
privatevoid initComponents() {
mainTable = new JTable();
fixTable = new JTable();
mainModel = new MainTableModel();
fixModel = new FixTableModel();
mainTable.setModel(mainModel);
mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
mainTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixTable.setModel(fixModel);
fixTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mainTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
publicvoid valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
checkSelection(false);
}
});
fixTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
publicvoid valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
checkSelection(true);
}
});
// 添加鼠标事件同步两个表的鼠标双击事件
mainTable.addMouseListener(new MouseAdapter() {
publicvoid mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
if (e.getSource().equals(mainTable)) {
MouseListener[] mls = (MouseListener[]) fixTable
.getListeners(MouseListener.class);
for (int i = 0; i < mls.length; i++)
mls[i].mouseClicked(e);
}
}
}
});
fixTable.addMouseListener(new MouseAdapter() {
publicvoid mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
if (e.getSource().equals(fixTable)) {
System.out.println("首先点击了冻结列");
} else {
System.out.println("首先点击了内容列");
}
// if (e.getSource().equals(fixTable)) {
// MouseListener[] mls = (MouseListener[]) mainTable
// .getListeners(MouseListener.class);
// for (int i = 0; i < mls.length; i++)
// mls[i].mouseClicked(e);
// }
}
}
});
mainScroll = new JScrollPane();
mainScroll.getViewport().add(mainTable);
JViewport fixPort = new JViewport();
fixPort.setPreferredSize(fixTable.getPreferredSize());
fixPort.add(fixTable);
mainScroll.setRowHeaderView(fixPort);
mainScroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixTable
.getTableHeader());
}
[1]