JList List<E> Page 分页

package view.retrieve.comps.center;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListModel;

/**
 * @Description
 * @Author zhangzuoqiang
 * @Date 2012-3-30 | 下午9:28:05
 */
public class PaginatedList extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main(String args[]) throws Exception {

		// create 100 elements of dummy data.
		Integer[] data = new Integer[100];
		for (int i = 0; i < data.length; i++) {
			data[i] = i + 1;
		}

		// create a paginated list with page size 20
		PaginatedList list = new PaginatedList(new JList(data), 20);

		// add it to a frame
		JFrame f = new JFrame();
		f.add(list);
		f.setSize(100, 100);
		f.pack();
		f.setVisible(true);
	}

	private final int pageSize;
	private final JList list;
	private final ListModel model;

	private final int lastPageNum;
	private int currPageNum;
	private JLabel countLabel;
	private JButton first, prev, next, last;

	/**
	 * @param list
	 *            the jlist
	 * @param pageSize
	 *            the number of rows visible in the jlist
	 */
	public PaginatedList(JList list, int pageSize) {
		super();
		this.pageSize = pageSize;
		this.list = list;
		this.model = list.getModel();

		// work out how many pages there are
		this.lastPageNum = model.getSize() / pageSize
				+ (model.getSize() % pageSize != 0 ? 1 : 0);
		this.currPageNum = 1;

		setLayout(new BorderLayout());
		countLabel = new JLabel();
		add(countLabel, BorderLayout.NORTH);
		add(list, BorderLayout.CENTER);
		add(createControls(), BorderLayout.SOUTH);
		updatePage();
	}

	private JPanel createControls() {
		first = new JButton(new AbstractAction("<<") {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				currPageNum = 1;
				updatePage();
			}
		});

		prev = new JButton(new AbstractAction("<") {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				if (--currPageNum <= 0)
					currPageNum = 1;
				updatePage();
			}
		});

		next = new JButton(new AbstractAction(">") {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				if (++currPageNum > lastPageNum)
					currPageNum = lastPageNum;
				updatePage();

			}
		});

		last = new JButton(new AbstractAction(">>") {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				currPageNum = lastPageNum;
				updatePage();
			}
		});

		JPanel bar = new JPanel(new GridLayout(1, 4));
		bar.add(first);
		bar.add(prev);
		bar.add(next);
		bar.add(last);
		return bar;
	}

	private void updatePage() {

		// replace the list's model with a new model containing
		// only the entries in the current page.
		final DefaultListModel page = new DefaultListModel();
		final int start = (currPageNum - 1) * pageSize;
		int end = start + pageSize;
		if (end >= model.getSize()) {
			end = model.getSize();
		}
		for (int i = start; i < end; i++) {
			page.addElement(model.getElementAt(i));
		}
		list.setModel(page);

		// update the label
		countLabel.setText("Page " + currPageNum + "/" + lastPageNum);

		// update buttons
		final boolean canGoBack = currPageNum != 1;
		final boolean canGoFwd = currPageNum != lastPageNum;
		first.setEnabled(canGoBack);
		prev.setEnabled(canGoBack);
		next.setEnabled(canGoFwd);
		last.setEnabled(canGoFwd);
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值