سؤال

So I have a swing application where a button opens up a window. It is pretty simple, to open it I use:

private static logPicker logWindow;
static boolean logViewerOpen = false;

if (!logViewerOpen) {
    logWindow = new logPicker();
    logWindow.frmOpenLog.setVisible(true);
    logViewerOpen = true;
}
else {
    logWindow.frmOpenLog.requestFocus();
}

I also have a window listener to know when the viewer is closed:

frmOpenLog.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent arg0) {
        indexPage.logViewerOpen = false;
        frmOpenLog.dispose();
    }
});

I do this because I want to keep track on whether or not the window is already open, because if it is then I have to update information. The window I open has a list of logs that a user can double click on to view the information about that log. The problem right now is, when a user double clicks on the list it gets called however many times I have opened and closed that window. example: I open the log picker window, and then close it. I open it again and double click on the log I want to view, and it will open 2 of those. I have the double click simple do a .doClick() on the Open Log button. The weird thing is, when I use the button to open the log, it does not do this. It will only open the log once. Here is the code for the double click event and the Open Log button.

@Override
public void mouseClicked(MouseEvent arg0) {
    if (arg0.getClickCount() == 2) {
        btnOpenLog.doClick();
    }
}

btnOpenLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
        logViewer window = new logViewer(log.getSelectedValue());
        window.frmLogViewer.setVisible(true);
    }
});
هل كانت مفيدة؟

المحلول

@LiverpoolFTW: Please provide a SSCCE demonstrating the problem. Absent sufficient code, I speculate you're (re-)adding the MouseListener/MouseAdapter each time your window is opened. The following example works as desired as-is, incrementing the clickCount once per button press or label double-click. But if you uncomment the indicated section, you'll see that the doClick() is executed twice when you double-click the label. If you have, for example, some component to which you're adding a listener each time the window opens, each of those listeners will be executed.

package example.stackoverflow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class ClickCheck extends JFrame
{
    private static final long serialVersionUID = -6446528001976145548L;
    private static final JButton btnOpenLog = new JButton("Open Log");
    public ClickCheck()
    {
        JLabel label = new JLabel("Double-Click Me");
        label.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                if (arg0.getClickCount() == 2) {
                    btnOpenLog.doClick();
                }
            }
        });

        // Uncomment to demonstrate the effect of multiple listeners
//        label.addMouseListener(new MouseAdapter()
//        {
//            @Override
//            public void mouseClicked(MouseEvent arg0) {
//                if (arg0.getClickCount() == 2) {
//                    btnOpenLog.doClick();
//                }
//            }
//        });

        btnOpenLog.addActionListener(new ActionListener() {
            private int clickCount = 0;
            public void actionPerformed(ActionEvent e) {
                    System.out.println(++clickCount + ": Button clicked");
                }
            });


        setSize(200, 200);
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        add(btnOpenLog);
        add(label);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                ClickCheck c = new ClickCheck();
                c.setVisible(true);
            }
        });
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top