【问题标题】:Swing Double click摆动双击
【发布时间】:2013-03-25 22:45:53
【问题描述】:

当尝试通过双击从列表中选择项目时,它为两个列表选择一次,但在单击选择后!!!

    listScrollPanel.setViewportView(categoryList);
    subCategoryList.setModel(new javax.swing.AbstractListModel() {
        String[] strings = {"Sub-category 1", "Sub-category 2", "Sub-category 3", "Sub-category 4", "Sub-category 5", "Sub-category 6"};
    });
subCategoryList.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        infoBox(Integer.toString(evt.getClickCount()), subCategory);
        if (evt.getClickCount() == 2) {
            subCategoryList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
                @Override
                public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                    subCategoryListValueChanged(evt);
                }
            });
        }
    }
});
private void subCategoryListValueChanged(javax.swing.event.ListSelectionEvent evt) {

    subCategory = (String) subCategoryList.getSelectedValue();
    labelSC.setText(CAT_HEADER);
    listScrollPanel.setViewportView(categoryList);
}
private static void infoBox(String Message, String location) {
    JOptionPane.showMessageDialog(null, Message, location, JOptionPane.INFORMATION_MESSAGE);
}

如何让他只听双击? 我有 categoryList ,双击后 ListSelection 将 viewportView 更改为 subCategory ,它的工作原理相同!但他们在前两次双击后对单击做出反应!

【问题讨论】:

  • !!!请为什么大喊大叫,为了更好的帮助,请尽快发布SSCCE,简短,可运行,可编译,只是带有列表的容器,
  • 是的,我看到了这个问题!并添加到我的代码中!但它没有用!所以我问!有人告诉我,我必须删除addListSelectionListener!!!如果你能看到,在我的代码中是双击你抛出的问题!但它没有奏效!我真的很感谢这里的所有作者,倾听和尊重,但你的......就像something sort of like was

标签: java swing mouseevent listselectionlistener


【解决方案1】:

每次发生双击鼠标事件时,您都会添加一个新的ListSelectionListener。每一个都将响应单击鼠标。您可以单独使用MouseListener

subCategoryList.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent evt) {
        if (evt.getClickCount() == 2) {
           String selectedValue = subCategoryList.getSelectedValue();
           // do stuff with selectedValue...
        }
    }
});

【讨论】:

  • 准确地说:“每次都会发生双击鼠标”
  • 如果我可以在这里问! subCategoryList.getModel().getElementAt(subCategoryList.locationToIndex(evt.getPoint())) 有什么区别;和 subCategoryList.getSelectedValue(); ???
  • 两者是等价的,getSelectedValue 更短,所以更新了这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多