为什么多次调用viewport changelistener

时间:2014-01-23 06:40:59

标签: java swing changelistener jviewport

我有一个视口,我已经附加了一个更改侦听器。每当我滚动浏览视口时,我的更改侦听器会被调用大约四次。我不确定如何避免这种情况;我只想让电话发生一次?

2 个答案:

答案 0 :(得分:2)

没有办法解决这个问题,JViewport会触发多个stateChanged事件,因为它会提供有关多个属性更改的通知......

来自JavaDocs ...

  

ChangeListener添加到每个通知的列表中   视图的大小,位置或视口大小所具有的时间   改变。

此时,由于我们不知道您要尝试实现的目标,因此很难知道建议的内容,但是,如果您必须使用ChangeListener,则可以设置合并机制。也就是说,而不是响应每个事件,你基本上等到事件之间发生了足够长的延迟,然后再响应它......

例如......

public class DelayedChangeHandler implements ChangeListener {

    private Timer timer;
    private ChangeEvent last;

    public DelayedChangeHandler() {
        timer = new Timer(250, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stableStateChanged();
            }
        });
        timer.setRepeats(false);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        last = e;
        timer.restart();
    }

    protected void stableStateChanged() {
        System.out.println("Finally...");
    }

}

基本上,这是ChangeListener实现,它使用具有短延迟的非重复javax.swing.Timer。每次调用stateChanged时,计时器都会重新启动。最后,当允许计时器“勾选”时,它会调用stableStateChanged,表示自上次事件被引发以来已经过了足够的时间。

这假设您不太关心导致事件的原因,只关心事件发生...

一个可运行的例子......

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestViewport {

    public static void main(String[] args) {
        new TestViewport();
    }

    public TestViewport() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel pane = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(1000, 1000);
                    }
                };

                JScrollPane sp = new JScrollPane(pane);
                sp.getViewport().addChangeListener(new DelayedChangeHandler());
                sp.getViewport().addPropertyChangeListener(new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        System.out.println(evt.getPropertyName());
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(sp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class DelayedChangeHandler implements ChangeListener {

        private Timer timer;
        private ChangeEvent last;

        public DelayedChangeHandler() {
            timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    stableStateChanged();
                }
            });
            timer.setRepeats(false);
        }

        @Override
        public void stateChanged(ChangeEvent e) {
            last = e;
            timer.restart();
        }

        protected void stableStateChanged() {
            System.out.println("Finally...");
        }

    }

}

答案 1 :(得分:2)

您可以尝试使用AdjustmentListener获取一次滚动事件,请尝试下一步:

import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.UnsupportedEncodingException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Example {

    public static void main(String[] args) throws UnsupportedEncodingException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JScrollPane pane = new JScrollPane(new JTextArea());
        pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                if(e.getValueIsAdjusting()){
                    return;
                }
                System.out.println("vertical scrolled");
                System.out.println("bar value = " + e.getValue());
            }
        });
        frame.setContentPane(pane);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

这是另一个example