我創建了一個類,它可以動態地鎖定和解鎖列。 在我的程序中,我創建了兩個具有相同tablemodel的表格。 一個是在scrollview的Jviewport中,另一個是在RowHeaderView中。 問題是,當您解鎖所有鎖定的列 ,並且您想要再次開始鎖定時,不起作用。沒有錯誤,但它就像事件沒有回答。JTable的動態固定列問題
步驟產生問題:
- 嘗試代碼,
- 放在固定的表中的所有列,
- 然後用右鍵雙擊解鎖,
- 然後重新啓動鎖定,並解鎖
做這個程序,你可以看到鼠標事件不再回答
張貼SSCCE時public class Prova extends JFrame{
private JTable mainTable,fixedTable;
private JScrollPane scrollPane;
private JTableHeader mainTableHeader;
private TableColumnModel originalColumnModel,mainColumnModel,fixedColumnModel;
private TableColumn[] columns;
private int ncols,counter;
public Prova(){
counter = 0;
TableModel mainTableModel = new DefaultTableModel(5, 10);
scrollPane = new JScrollPane();
mainTable = new JTable(mainTableModel);
mainColumnModel = mainTable.getColumnModel();
fixedTable = new JTable();
fixedTable.setAutoCreateColumnsFromModel(false);
fixedTable.setModel(mainTable.getModel());
ncols = mainTableModel.getColumnCount();
columns = new TableColumn[ncols];
for (int i=0;i<ncols;i++){
columns[i] = mainColumnModel.getColumn(i);
}
mainColumnModel = mainTable.getColumnModel();
fixedColumnModel = fixedTable.getColumnModel();
mainTableHeader = mainTable.getTableHeader();
mainTableHeader.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent me){
if (SwingUtilities.isRightMouseButton(me)){
if (ncols - counter>1){
counter ++;
int col = mainTable.columnAtPoint(me.getPoint());
TableColumn column = mainColumnModel.getColumn(col);
mainColumnModel.removeColumn(column);
fixedTable.getColumnModel().addColumn(column);
scrollPane.setRowHeaderView(fixedTable);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
}
}
}
});
fixedTable.getTableHeader().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me){
if (SwingUtilities.isRightMouseButton(me) && me.getClickCount()== 2){
while (mainColumnModel.getColumnCount() > 0){
mainColumnModel.removeColumn(mainColumnModel.getColumn(0));
}
while (fixedColumnModel.getColumnCount() > 0){
fixedColumnModel.removeColumn(fixedColumnModel.getColumn(0));
}
for(int i=0;i<ncols;i++){
mainColumnModel.addColumn(columns[i]);
}
scrollPane.setRowHeaderView(null);
}
}
});
scrollPane.setViewportView(mainTable);
add(scrollPane, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Prova().setVisible(true);
}
});
}
}
你的問題是什麼?此外,儘量減少你的例子:http://stackoverflow.com/help/mcve - 它有助於可讀性,並有助於給出相關的答案 –
這將是很好的檢查由亞當提供的LINK –
'現在它的所有短' - 沒有代碼更糟,因爲我們無法執行代碼,因此我們無法看到問題所在。任何人都無法根據該代碼解決問題。提問時,您需要發佈正確的「SSCCE」。我很幸運,我看到了舊的代碼。閱讀我的答案,提供有關創建更好的SSCCE以及問題答案的提示(無論如何我都瞭解問題)。 – camickr