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 );
}
}
}