class ListExample extends JFrame implements ActionListener {
private JList list = null;
private int pos = 0;
public ListExample() {
setTitle( "ListExample" );
String[] entries = {"Entry 1", "Entry 2", "Entry 3",
"Entry 4", "Entry 5", "Entry 6",
"Entry 7", "Entry 8", "Entry 9",
"Entry10", "Entry11", "Entry12",
"Entry13", "Entry14", "Entry15",
"Entry16", "Entry17", "Entry18",
"Entry19", "Entry20", "Entry21",
"Entry22", "Entry23", "Entry24"};
list = new JList( entries );
JPanel p= new JPanel();
list.setVisibleRowCount(5);
p.setLayout( new BorderLayout() );
JButton btn1 = new JButton( ">>" );
JButton btn2 = new JButton( "<<" );
p.add( btn1, BorderLayout.NORTH );
p.add( btn2, BorderLayout.SOUTH );
p.add( new JScrollPane( list, JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ), BorderLayout.CENTER );
btn1.addActionListener( this );
btn2.addActionListener( this );
getContentPane().add(p);
setVisible( true );
pack();
}
public void actionPerformed( ActionEvent arg0 ) {
int min = 0;
int max = list.getModel().getSize()-list.getVisibleRowCount();
if ( ">>".equals(arg0.getActionCommand())) {
pos--;
} else {
pos++;
}
if ( pos < min) pos = min;
if ( pos >= max) pos = max;
list.scrollRectToVisible(list.getCellBounds(pos,pos+4));
}
public static void main( String[] args ) {
new ListExample();
}
}