Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

I need to scroll my JMenu

Gen.JavaAug 29 2010 — edited Sep 7 2010
Hi all,

I need to provide simple scrolling to my JMenu. The API shows the use of setAutoscrolls() and scrollRectToVisible() to do that. The following is my code that doesn't scroll.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Menu
{
public static void main(String...a)
{
 JFrame     f;
 JButton    b;
 JPopupMenu p;
 JMenu      m;

 m   = new JMenu ("Long menu");
 for ( int i=1; i < 100; i++ )
       m.add ( "Long "+i );
 m.setAutoscrolls ( true );
 m.addMouseMotionListener ( new mml() );

 p = new JPopupMenu ();
 p.add ( m );

 b = new JButton ( "Dummy" );
 b.addActionListener ( new al(b,p) );

 f = new JFrame ();
 f.add ( b);
 f.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
 f.pack();
 f.setVisible ( true );
}

private static class al implements ActionListener
{
 JButton    b;
 JPopupMenu p;

 public al(JButton b,JPopupMenu p)
 {
  this.b=b;
  this.p=p;
 }

 public void actionPerformed(ActionEvent e)
 {
  p.show ( b,b.getX()+b.getWidth(),b.getY() );
 }
}

private static class mml extends MouseMotionAdapter
{
 public void mouseDragged ( MouseEvent e )
 {
  Rectangle r = new Rectangle ( e.getX(),e.getY(),1,1 );
  ((JMenu)e.getSource()).scrollRectToVisible ( r );
 }
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 5 2010
Added on Aug 29 2010
21 comments
2,594 views