0

I have a Java Swing application, and i have created a special sub JPopupMenu for it that is scrollable so a user can simply scroll through it and select one item from it, like in this screenshot :

enter image description here

I have used the code from this post and i have pasted it here so you can check it: http://codeshare.io/Jgqa7

Now if the user has opened this sub-menu for an item that he has already made a selection for it before from that sub-menu then i want to automatically scroll to the selected item to show it, just like the ensureIndexIsVisible(...) method for a JList. I have spent some time trying to figure this out but with no progress. So, is there a way to accomplish this ?

--------------------------------------------------------> Edit: The code i'm using :

I have tried using this code to force scroll to the item "invented" in the scrollable menu but it failed :

JScrollPopupMenu pm = (JScrollPopupMenu)myPopupMenu.getPopupMenu();

for( Component comp: myPopupMenu.getMenuComponents() ) {
    if( comp instanceof JRadioButtonMenuItem ) {
        JRadioButtonMenuItem rb = (JRadioButtonMenuItem)comp;

        if( rb.getText().equals( "invented" ) ) {
            myPopupMenu.scrollRectToVisible( rb.getBounds() );  // Does nothing.
            pm.setSelected( rb );  // Does nothing.
        }
    }
}

For some reason it doesn't scroll to the item i want !

Community
  • 1
  • 1
Brad
  • 4,457
  • 10
  • 56
  • 93
  • `JPopupMenu.setSelected(Component componentToSelect)`? – DavidPostill Aug 04 '14 at 10:11
  • [`parent.scrollRectToVisible(child.getBounds())`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#scrollRectToVisible(java.awt.Rectangle)) or `child.scrollRectToVisible(new Rectangle(0, 0, child.getWidth(), child.getHeight())`. All `JComponent`s can do this. Just use the menu item as `child`. – Holger Aug 04 '14 at 10:17
  • I have tried both solutions but none is working ! ... When i get the bounds of the child it always returns Zero values ! – Brad Aug 07 '14 at 05:14

1 Answers1

0

I needed a solution to scroll as well. Since I did not find a scrollpane, I needed to implement my own scrollRectToVisible method in JScrollPopupMenu:

@Override
public void scrollRectToVisible(Rectangle t){
    // need to adjust scrollbar position
    if (t.getY()<0){
        // scroll up
        JScrollBar scrollBar = getScrollBar();
        scrollBar.setValue(scrollBar.getValue() + (int)(t.getY()));
    }else if (t.getY()+t.getHeight()>getBounds().getHeight()){
        // scroll down
        JScrollBar scrollBar = getScrollBar();
        scrollBar.setValue(scrollBar.getValue() - (int)(getBounds().getHeight()-t.getY()-t.getHeight()));
    }
    doLayout();
    repaint();
}

Calling this with the bounds of the clicked JMenuItem (I used a mouselistener with mouseentered) scrolls the panel so that the item is visible.

Hope it helps!

RamenChef
  • 5,557
  • 11
  • 31
  • 43
Michael
  • 16