1

I have a JScrollpane which contains a big JPanel and the JPanel it's self contains 3 JPanels. each of those 3 JPanels has the same size as the JScrollpane. the user can not scroll. he can clicks on a button and sees the next or previous JPanel (only 1 panel can bee seen at a moment and he can not see a part of one panel and part of other...).

How can I understand which panel is being seen right now?

4

4 回答 4

2

There is a method isShowing() which Determines whether this component is showing on screen. This means that the component must be visible, and it must be in a container that is visible and showing. More here.

Example:

JPanel p= new JPanel();

if(p.isShowing()) {

}

Have a look at this discussion it will help you in a way. You can even use a ComponentListener to listen to about which is being visible. Know more about it here.

于 2013-01-11T09:39:46.097 回答
1

You can request currently shown component from JScrollPane.

scrollPaneObject.getViewPort().getView();
于 2013-01-11T10:45:09.477 回答
1

Assuming the setup as outlined in my comment to your question, the basic approach is to compare the parent's (the component that contains the 3 panels) visibleRect with its children's bounds. Something like:

final JComponent parent = new JPanel(); // new PageScrollable();
parent.setLayout(new BoxLayout(parent, BoxLayout.PAGE_AXIS));
Color[] color = new Color[] {Color.YELLOW, Color.RED, Color.GREEN}; 
for (int i = 0; i < color.length; i++) {
    JTable table = new JTable(10, 5);
    // color it to see some difference
    table.setBackground(color[i]);
    // set a name for logging
    table.setName("table at: " + i);
    parent.add(table);
}

JScrollPane scrollPane = new JScrollPane(parent);
Action visible = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Rectangle rect = parent.getVisibleRect();
        for (int i = 0; i < parent.getComponentCount(); i++) {
            // implement logic as needed to compare the parent's visible rect
            // with the children's bounds
            if (rect.intersects(parent.getComponent(i).getBounds())) {
                System.out.println("found: " + parent.getComponent(i).getName());
            }
        }
    }

};
frame.add(scrollPane); 
frame.add(new JButton(visible), BorderLayout.SOUTH);

As an aside: to fine-tune scrolling behaviour you might consider a custom panel which implements Scrollable, something like:

/**
 * Implement a panel of type Scrollable to fine-tune its scrolling behaviour.
 * This implements the prefScrollableSize to the prefSize of the first child
 * and both block/unit increment to the height of the prefScrollable.
 */
public static class PageScrollable extends JPanel implements Scrollable {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        if (getComponentCount() > 0) {
            return getComponent(0).getPreferredSize();
        }
        return super.getPreferredSize();
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect,
            int orientation, int direction) {
        return getPreferredScrollableViewportSize().height;
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
            int orientation, int direction) {
        return getPreferredScrollableViewportSize().height;
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

}
于 2013-01-11T13:46:02.047 回答
0

the user can not scroll. he can clicks on a button and sees the next or previous JPanel (only 1 panel can bee seen at a moment and he can not see a part of one panel and part of other

You should be using a JPanel with a CardLayout, not a JScrollPane.

See http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

于 2013-01-11T11:26:09.467 回答