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?

SAndroid
  • 35
  • 9
  • *"jScrollpane which contains 3 jPanels"* That is `JScrollpane` and `JPanel` Note the capital `J`. Unless you are referring to a poorly named 3rd party class, use the correct case.. – Andrew Thompson Jan 11 '13 at 09:42
  • `JScrollPane.getViewPortView()`? – Guillaume Polet Jan 11 '13 at 09:52
  • JScrollPane.getViewPortView().getName(); always returns null...why? – SAndroid Jan 11 '13 at 12:43
  • still not sure I understand what your setup: a JScrollPane can have exactly one viewportView - so your three panels are added to some other panel which is that single viewportView? And its unitIncrement is the (same for all 3) height of the childPanels? – kleopatra Jan 11 '13 at 13:06
  • @kleopatra yes, you are true – SAndroid Jan 11 '13 at 13:43
  • @kleopatra what do you think, how to do that? – SAndroid Jan 11 '13 at 13:48
  • FYI, in case you didn't received proper notification, your other question about images stored in a database was closed as a duplicate of this one: http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – Aaron Bertrand Jan 11 '13 at 20:01

4 Answers4

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.

Community
  • 1
  • 1
Vinay
  • 6,891
  • 4
  • 32
  • 50
  • 3
    +1 [more in question](http://stackoverflow.com/questions/10880326/jpanel-which-one-of-listeners-is-proper-for-visibility-is-changed), please change link Java1.4 to Java7, add – mKorbel Jan 11 '13 at 09:44
  • 1
    this doesn't work in my case. this method always returns true for me, because the panels all are visible but only one of them really can bee seen, but as they are visible and are added to the JScrollpane which it's self is visible, too, it returns true... – SAndroid Jan 11 '13 at 12:39
  • 1
    -1 that's simply wrong (at least as I understand the OP's setup ;-) ... please _read_ the api doc of isShowing to understand the why ... – kleopatra Jan 11 '13 at 13:09
1

You can request currently shown component from JScrollPane.

scrollPaneObject.getViewPort().getView();
Yogesh Ralebhat
  • 1,376
  • 1
  • 13
  • 29
  • I really didn't understand what it return ! scrollPaneObject.getViewPort().getView().getName(); always returned null...why? – SAndroid Jan 11 '13 at 12:42
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;
    }

}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
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

Walter Laan
  • 2,956
  • 17
  • 15