我有3个按钮,更新,删除和显示历史记录。我试图确保在可以实现任何3个按钮之前表格有一个选定的行。我这里的例子是删除按钮。我已经包含了Delete JButton的监听器和Table的Selection Listener。我不确定如何让按钮首先听表Selection Listener。
删除操作按钮侦听器
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isContactSelected();
// Display yes/no dialog box over mainpanel when okButton is clicked
int answer = JOptionPane.showConfirmDialog(mainPanel, "Are you sure you wish to delete contact?", "Decision", JOptionPane.YES_NO_OPTION);
// if yes is chosen system closed with an information box
if (answer == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog (mainPanel, "Contact has been deleted", "Information", JOptionPane.INFORMATION_MESSAGE);
// if no is chosen system stays running
} else if (answer == JOptionPane.NO_OPTION) {
// dont do anything
}}
});
检查是否已选择行
public void isContactSelected(){
ListSelectionModel selected = table.getSelectionModel();
//
selected.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
int row = table.getSelectedRowCount();
// if the number of rows selected is equal to 0
if(row > 0)
{
}else{
System.out.println("Please select a contact");
}
}
});
}
答案 0 :(得分:0)
你可以让你的按钮保持活动状态&一旦按下检查是否有任何选择&如果没有选择显示类似joptionpane
的东西int row, column;
row = table.getSelectedRow();
table.getModel().getValueAt(row, column); //from here you can delete the row/column