JTable行限制 [英] JTable row limitation

查看:128
本文介绍了JTable行限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须限制 JTable 中的行数。如果我有100条记录,我需要在初始加载JTable时显示10条。我希望按下next按钮,每次点击后它会显示另一组10条记录。

I have to limit the number of rows in a JTable. If I have 100 records I need to display 10 on the initial loading of JTable. I wish to put a button like "next", and after each click it shows another set of 10 records.

推荐答案


我必须限制JTable中的行数。如果我有100条记录,我需要在初始加载JTable时显示10条。

I have to limit the number of rows in a JTable. If i have 100 records i need to display 10 on the initial loading of JTable.

使用首选大小(+适当的布局和布局约束)来修复大小。

Use preferred size (+ an appropriate layout and layout constraint) to fix the size.


我想放一个像下一个的按钮,每次点击后它会显示另一组10记录。

I wish to put a button like "next", and after each click it showing another set of 10 records.

删除滚动窗格的RHS上的滚动条。然后使用按钮代替下一个/上一个的效果。

Remove the scroll bar on the RHS of the scroll pane. Then use buttons instead for the effect of 'next/previous'.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

class FixedRowsTable {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                String[] columns = {"1","2","3","4","5","6","7"};
                Integer[][] data = new Integer[1000][columns.length];
                for (int xx=0; xx<data.length; xx++) {
                    for (int yy=0; yy<data[0].length; yy++) {
                        data[xx][yy] = new Integer((xx+1)*(yy+1));
                    }
                }
                final int rows = 11;

                JPanel gui = new JPanel(new BorderLayout(3,3));

                final JTable table = new JTable(
                    new DefaultTableModel(data, columns));

                final JScrollPane scrollPane = new JScrollPane(
                    table,
                    JScrollPane.VERTICAL_SCROLLBAR_NEVER,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                Dimension d = table.getPreferredSize();
                scrollPane.setPreferredSize(
                    new Dimension(d.width,table.getRowHeight()*rows));

                JPanel navigation = new JPanel(
                    new FlowLayout(FlowLayout.CENTER));
                JButton next = new JButton(">");
                next.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        int height = table.getRowHeight()*(rows-1);
                        JScrollBar bar = scrollPane.getVerticalScrollBar();
                        bar.setValue( bar.getValue()+height );
                    }
                } );
                JButton previous = new JButton("<");
                previous.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        int height = table.getRowHeight()*(rows-1);
                        JScrollBar bar = scrollPane.getVerticalScrollBar();
                        bar.setValue( bar.getValue()-height );
                    }
                } );

                navigation.add(previous);
                navigation.add(next);

                gui.add(scrollPane, BorderLayout.CENTER);
                gui.add(navigation, BorderLayout.SOUTH);

                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

这篇关于JTable行限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆