![](/img/trans.png)
[英]How can I make a JTable cell do different things on single-click than on double-click?
[英]Double-click wrong behaviour at jtable
再會!
我有一個 jTable,我正在用 JButton 監聽器填充它。 用鼠標雙擊填充 jTable 后,我調用 JOptionPane.showMessageDialog。 在那個問題被accurate之后。 再次單擊 JButton 后,jTable 再次填充。 如果我再次雙擊行\\單元格,它會通過 JOptionPane 向我顯示 2 條消息,而不是一條。 據我所知,我需要刷新模型,但沒有成功。
我的代碼:
表單.java
btnExec = new JButton("Exec");
table = new JTable();
scrollPane.setViewportView(table);
// Create Button Listener
ActionListener startProcess = new ExecButtonPressed();
btnExec.addActionListener(startProcess);
ExecButtonPressed.java
public class ExecButtonPressed implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
System.out.println("table rows:" + Form.table.getRowCount());
DefaultTableModel tableModel = new DefaultTableModel();
prepare(tableModel); // create headers
fill(tableModel); // add rows
Form.table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Form.table.setModel(tableModel);
sortresize(Form.table); //resize width of columns + sort by id desc
Form.table.setDefaultEditor(Object.class, null);
DoubleClicker.whattodo(); // class of double-click behavior logic
}catch (Exception e1) {
e1.printStackTrace();
}
}
//
private void prepare(DefaultTableModel tableModel) throws Exception {
try {
tableModel.addColumn("id");
tableModel.addColumn("name");
tableModel.addColumn("age");
}catch (Exception e1) {
e1.printStackTrace();
}
}
//
private void fill(DefaultTableModel tableModel) {
Object [] line = {"01", "Qw" , "20"};
tableModel.addRow(line);
Object [] line2 = {"02", "As" , "21"};
tableModel.addRow(line2);
Object [] line3 = {"03", "Zx" , "22"};
tableModel.addRow(line3);
}
//
private void sortresize(JTable table) {
//Resize width
for (int column = 0; column < table.getColumnCount(); column++) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
if (column == 0) {
tableColumn.setPreferredWidth( 45 );
} else if (column == 1){
tableColumn.setPreferredWidth( 50 );
} else if (column == 2){
tableColumn.setPreferredWidth( 55 );
}
}
//Sort table.
if (table.getRowCount() > 1){
TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
table.setRowSorter(sorter);
List<RowSorter.SortKey> sortKeys = new ArrayList<>();
int columnIndexToSort = 0;
sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.DESCENDING));
sorter.setSortKeys(sortKeys);
sorter.sort();
};
}
//
}
DoubleClicker.java
public class DoubleClicker {
public static void whattodo() {
Form.table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
try {
System.out.println("click:" + mouseEvent.getClickCount());
Form.table = (JTable) mouseEvent.getSource();
Point point = mouseEvent.getPoint();
int row = Form.table.rowAtPoint(point);
int columnid = Form.table.columnAtPoint(point);
Form.table.convertRowIndexToModel(row);
Form.table.convertColumnIndexToModel(columnid);
if (mouseEvent.getClickCount() == 2 && Form.table.getSelectedRow() != -1) {
if (Form.table.getColumnName(columnid).equalsIgnoreCase("name")) {
JOptionPane.showMessageDialog(null,
(String) Form.table.getValueAt(row,columnid), "Selected name", 1);
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
}
我嘗試了不同的選擇,比如
tableModel.setRowCount(0);
tableModel.getDataVector().removeAllElements();
tableModel.fireTableDataChanged();
也許我把它放在錯誤的地方? 因為在第二個按鈕執行時,每次鼠標點擊都會注冊兩次。
更新:我添加了
System.out.println("table rows:" + Form.table.getRowCount());
在 public void actionPerformed(ActionEvent e) { 之后,正如我在控制台中看到的那樣,第二個按鈕執行后 Form.table.getRowCount() == 3。 還添加了
System.out.println("click:" + mouseEvent.getClickCount());
正如我所看到的,作為 public void mousePressed(MouseEvent mouseEvent) 的第一行,在每次點擊表中的第二個按鈕執行兩次后,我得到:click:1 click:1 在第一次執行時它只是 click:1
似乎是一種又一種的看法。
整個 Form.java 以防有人願意編譯
public class Form {
private JFrame frame;
public static JTable table;
public static JButton btnExec;
public static JScrollPane scrollPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Form window = new Form();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Form() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btnExec = new JButton("Exec");
btnExec.setBounds(10, 11, 89, 23);
frame.getContentPane().add(btnExec);
scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(10, 45, 369, 174);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
// Create Button Listener
ActionListener startProcess = new ExecButtonPressed();
btnExec.addActionListener(startProcess);
}
}
在 coderanch 論壇上得到了答案,決定在那里提交。 也許將來有人會需要它。
解決方法是在創建表時添加應該添加到表中的MouseListener(而不是在ActionListener 的actionPerformed() 方法中)。
所以我已經轉移了DoubleClicker.whattodo(); // class of double-click behavior logic
DoubleClicker.whattodo(); // class of double-click behavior logic
ExecButtonPressed.java 到 Form.java DoubleClicker.whattodo(); // class of double-click behavior logic
類:
table = new JTable();
scrollPane.setViewportView(Form.table);
DoubleClicker.whattodo(); // class of double-click behavior logic
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.