JTable不会听Doubleclicks [英] JTable won't listen to Doubleclicks

查看:160
本文介绍了JTable不会听Doubleclicks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用默认组件为可编辑的 JTable 实现撤消(和重做)功能。 JTable 有一个额外的类来指定名为 SpecifiedJTable 的属性。

I´m trying to implement an undo (and redo) function for an editable JTable with the default components. The JTable has an extra class to specify its properties called SpecifiedJTable.

为了做到这一点,我想抓住一个单元格被双击的时刻(即选择/标记要编辑的单元格的时刻)将单元格中的信息及其坐标推送到堆栈上。

To do so I wanted to grab the moment when a cell is doubleclicked (i.e. the moment when a cell is chosen/marked to be edited) to push the information in the cell and its coordinates onto the stack.

这应该通过 MouseListener 来完成......至少这是我的想法。
我试过这个(站在我的 SpecifiedJTable 类的构造函数中)

This should be done by a MouseListener ...at least that was my idea. I tried this (standing in the constructor of my SpecifiedJTable class)

class JTableSpecified extends JTable {
    private static final long serialVersionUID = 1L;
    private int c; // the currently selected column
    private int r; // the currently selected row

    public JTableSpecified(String[][] obj, String[] columnNames) {
        super(obj, columnNames); // constructs the real table
        // makes that you can only select one row at a time
        this.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        // makes that columns are not squeezed
        this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        // forbids to rearrange the columns
        getTableHeader().setReorderingAllowed(false);
        // adds action listener
        this.getModel().addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                r = getSelectedRow();
                c = getSelectedColumn();
                // get the String at row r and column c
                String s = (String) getValueAt(r, c);
                if (jobDisplayed) jobSwitch(c, s);
                else resSwitch(c, s);
            }
        });    
        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    System.out.println("test");
                }
            }
        });
    }
} 

但不知何故clickCounter不想达到任何东西这高于1.

but somehow the clickCounter doesn´t want to reach anything that´s higher than 1.

我很高兴有任何答案和帮助。谢谢。

I am glad about any answer and help. Thanks.

推荐答案

您遇到的问题与使用 mouseClicked()而不是使用 mousePressed()。在这种情况下,似乎很难增加点击计数器,但仍然是可能的。它花了我很多点击和鼠标移动来增加点击计数器超过1.您可以在您的代码中自己尝试。为了让计数器超过1,你需要通过按下&同时将鼠标从一个单元格移动到另一个单元格时快速释放(或者我可能只是幸运地在单元格之间点击?)。

The problem you are experiencing is related to use of mouseClicked() rather than using mousePressed(). In this case it appears to be very hard to increase the click counter, yet still it is possible. It took me lots of clicking and also mouse movement to increase the click counter over 1. You could try it by yourself, in your code. To get the counter over 1 you need to go crazy on the mouse by pressing & releasing fast while moving the mouse from cell to cell at the same time (or maybe I was just luckily clicking between the cells?).

正如您在此完全看到的那样工作样本,由您的代码制作,两次鼠标按下,使用 mousePressed()方法被检测到正常。

As you can see in this fully working sample, made from your code, two mouse presses, using the mousePressed() method are being detected just fine.

public class JTableSpecified extends JTable {
    private static final long serialVersionUID = 1L;

    public JTableSpecified(String[][] obj, String[] columnNames) {
        super(obj, columnNames); // constructs the real table
        // makes that you can only select one row at a time
        this.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        // makes that columns are not squeezed
        this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        // forbids to rearrange the columns
        getTableHeader().setReorderingAllowed(false);
        // adds action listener
        this.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
            }
       });        
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    System.out.println("test");
                }
                System.out.println("e.getClickCount() = " + e.getClickCount());
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JPanel panel = new JPanel();
                panel.add(new JTableSpecified(new String[][]{{"oi", "oi2"}, {"oi3", "oi4"}}, new String[]{"Col1", "Col2"}));
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane(panel);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

结论:也许你实际上想要使用 mousePressed()方法?

Conclusion: Maybe you in fact want to use the mousePressed() method?

这篇关于JTable不会听Doubleclicks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆