JScrollPane调整监听器和scrollRectToVisible [英] JScrollPane adjustmentListener and scrollRectToVisible

查看:239
本文介绍了JScrollPane调整监听器和scrollRectToVisible的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户滚动事件时,我需要处理JScrollPane中的事件.在此处中,建议使用addAdjustmentListener表示JViewport或Vertical/Horizo​​ntalScrollBar.

I need to handle event from JScrollPane when user scrolls it. In here it is suggested to use addAdjustmentListener to either JViewport or Vertical/HorizontalScrollBar.

有什么区别,我应该选择哪种方法?

Is there any difference, which method should I choose?

我也想对同一JScrollPane使用scrollRectToVisible,我是否应该期望AdjustmentListener用于此方法?

I would like also to use scrollRectToVisible for the same JScrollPane, should I anticipate AdjustmentListener working for this method?

我还想知道scrollRectToVisible是尝试进行最小滚动以使所需的矩形可见还是在JViewport的中间使其可见?

Also I wonder whether scrollRectToVisible tries to do minimum scrolling to make required rectangular visible or it tries to make it visible in the middle of JViewport ?

UPD:要求:

1)有一个带有一个JPanel的JScrollPane,其中有许多JLabel(ImageIcon),因此其中一些不可见.

1) There a JScrollPane with one JPanel which has many JLabel(ImageIcon), so some of them are not visible.

2)当发生网络事件时,我需要向用户显示JLabel之一(使其可见).如果我本人不可见,则JScrollPane应该自动滚动.这就是为什么我提到scrollRectToVisible.

2) When a network event comes I need to show one of JLabel (make it visible) to user. If I is not visible originally then JScrollPane should scroll automatically. That's why I mention scrollRectToVisible.

3)在那上面带有ImageIcon的JLabel上,我需要显示一条消息来解释该元素发生了什么.由于JLayeredPane在层次结构上要高得多,因此该消息当前被实现为另一个带有浮点数的JLabel.现在的问题是,如果用户滚动JScrollPane,则浮动JLabel应该相应地移动到相应JLabel(ImageIcon)的顶部.

3) Above that JLabel with ImageIcon inside I need to show a message which would explain what has happened to this element. The message currently is implemented as another JLabel with floats thanks to JLayeredPane much higher in hierarchy. The problem now is that if user scrolls JScrollPane that floating JLabel should move accordingly to be on top of correponding JLabel(ImageIcon).

UDP2:SSCCE

我还没有实现浮动JLabel,但是我不喜欢索引11的标签对scrollRectToVisible的反应,因为它被裁剪了一半.

I have not implemented floating JLabel yet, but already I don't like how label with index 11 reacts to scrollRectToVisible since it is half cropped.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class JScrollPaneTest {
    protected ArrayList<JLabel> labels = new ArrayList<JLabel>();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JScrollPaneTest();
            }
        });
    }

    public JScrollPaneTest() {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                JPanel panel1 = new JPanel ();

                panel1.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();

                for (int i = 0; i < 20; i++) {
                    JLabel label = new JLabel("  | Label" + i + " |  ");
                    panel1.add(label, gbc);
                    labels.add(label);
                }

                panel1.addMouseListener(new MouseAdapter(){
                    public void mousePressed (MouseEvent me) {
                        JLabel label = labels.get(11);
                        label.scrollRectToVisible(label.getBounds());
                    }
                });

                JScrollPane pane = new JScrollPane(panel1) {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(600, 400);
                    }
                };

                pane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                    @Override
                    public void adjustmentValueChanged(AdjustmentEvent e) {
                        System.out.println("adjustmentValueChanged: " + e);
                    }
                });

                frame.getContentPane().add(pane);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

推荐答案

部分答案(注释中的代码只是...,并注意到我在注释中错了;-)

Part of the answer (code in comments is just ... and noticed I had it wrong in the comment ;-)

scrollRectToVisible方法在被调用的组件的坐标系中采用Rectangle.要滚动一个孩子使其完全可见,基本上有两个选项(使用一个):

The method scrollRectToVisible takes a Rectangle in the coordinate system of the component it is called upon. To scroll a child such that it is completely visible, there are basically two options (use one of them):

// either call on child with zero offset
child.scrollRectToVisible(new Rectangle(child.getSize());
// or call on child's parent with child bounds
child.getParent().scrollRectToVisible(child.getBounds());

这篇关于JScrollPane调整监听器和scrollRectToVisible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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