我的表格有4列,我只希望列1被允許點擊。此外,此代碼還允許使用左鍵鼠標按鈕,我只想要右鍵。檢測鼠標右鍵何時被點擊
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int iCol = jTable1.getSelectedColumn();
if(iCol == 1 && evt.getModifiers() == InputEvent.BUTTON3_MASK)
{
if(evt.getClickCount() == 2)
{
int iRow = jTable1.getSelectedRow();
File iFile = new File(jTable1.getValueAt(iRow, iCol).toString());
String iPath = iFile.getAbsolutePath();
File iDir = new File(iPath.substring(0, iPath.lastIndexOf(File.separator)));
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().open(iDir);
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
我很抱歉,如果這個問題被證明爲2
是不是左鍵正常'InputEvent.BUTTON1_MASK'功能? – MadProgrammer
爲什麼?你已經檢查了'iCol == 1'。無論如何。您可以通過[setSelectionModel()](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setSelectionModel(javax.swing.ListSelectionModel))控制特定行中的選擇。 ,但我不知道如何控制列。 – ignis
@ignis我認爲OP更感興趣的只是對列1進行雙擊而不是限制選擇。但是你應該可以使用'ColumnModel'的選擇模型來控制列的選擇;) – MadProgrammer