import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
/**
* @author Darimont<br>
* @version 1.0<br>
* Date of creation: 06.04.2004<br>
* File: ScollableList.java<br>
* Modifier: Darimont<brScrollableListn: Revision<br>
* State: Germany<br>
*/
public class ScrollableList extends JPanel {
int idx;
int selIdx;
final int maxVisible = 10;
private JButton btn_UP;
private JButton btn_DOWN;
private JList list;
private Object[] listData;
private Object[] tmp = new Object[maxVisible];
private DefaultListModel model;
public ScrollableList(final Object[] listData) {
super();
this.listData = listData;
model = new DefaultListModel();
list = new JList(model);
btn_UP = new JButton("up");
btn_UP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
idx--;
if (idx < 0)
idx = 0;
makeVisible(idx);
}
});
btn_DOWN = new JButton("down");
btn_DOWN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
idx++;
if (idx > listData.length - maxVisible)
idx = listData.length - maxVisible;
makeVisible(idx);
}
});
add(btn_UP);
add(list);
add(btn_DOWN);
makeVisible(idx);
}
private void makeVisible(int idx) {
// TODO Auto-generated method stub
System.out.println("Idx: " + idx);
System.arraycopy(listData, idx, tmp, 0, maxVisible);
model.clear();
for (int i = 0; i < tmp.length; i++) {
model.add(i, tmp[i]);
}
list.updateUI();
}
public static void main(String[] args) {
JFrame frm = new JFrame("Scrollable JList");
frm.setSize(320, 240);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] data =
new String[] {
"Str 1 ",
"Str 2 ",
"Str 3 ",
"Str 4 ",
"Str 5 ",
"Str 6 ",
"Str 7 ",
"Str 8 ",
"Str 9 ",
"Str 10 ",
"Str 11 ",
"Str 12 ",
"Str 13 ",
"Str 14 ",
"Str 15 ",
"Str 16 ",
"Str 17 ",
"Str 18 ",
"Str 19 ",
"Str 20 ",
"Str 21 ",
"Str 22 ",
"Str 23 ",
"Str 24 ",
"Str 25 ",
"Str 26 ",
"Str 27 ",
"Str 28 ",
"Str 29 ",
"Str 30 " };
ScrollableList sl = new ScrollableList(data);
frm.getContentPane().add(sl);
frm.show();
}
}