Swing常用技巧

 //爲表格添加鼠標監聽
table.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
if (SwingUtilities.isRightMouseButton(e)) // 如果是右鍵點擊的話
{
int[] selectedRows = table.getSelectedRows();

//如果沒有選擇表格的任一行,則不處理
if (selectedRows.length < 1)
{
return;
}

int columnIndex = table.columnAtPoint(e.getPoint());
table.setColumnSelectionInterval(columnIndex, columnIndex);// 如果光標所在列
int modelCol = table.convertColumnIndexToModel(columnIndex);// 轉換爲Model列
if (COLUMN_INDEX.NAME_COL == modelCol)
{
JPopupMenu popupMenu = createPopMenu();
popupMenu.show(table, e.getX(), e.getY());
}
}

// 如果是左鍵雙擊的話
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == MouseEvent.BUTTON2)
{
//......
}
}
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章