java JTable 行限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6175139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:40:49  来源:igfitidea点击:

JTable row limitation

swingjtablejava

提问by Milin

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 条记录。我想放一个像 的按钮"next",每次点击后它会显示另一组 10 条记录。

回答by Andrew Thompson

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.

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

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

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

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

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

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

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

Like this

像这样

Pageable table

可分页表

FixedRowsTable.java

固定行表.java

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

回答by Costis Aivalis

If you use an AbstractTableModelyou can display millions of records. The idea is that your model will be loading whatever records are needed for the view, on demand.

如果您使用AbstractTableModel,您可以显示数百万条记录。这个想法是您的模型将按需加载视图所需的任何记录。

Here you have such a Model. It's not my best code, but will do :-) ...

在这里你有这样一个模型。这不是我最好的代码,但会做:-) ...

public class SomeTableModel extends AbstractTableModel {

    public SomeTableModel(ResultSet rs) {
        this.rs = rs;
        try {
            pos = this.rs.getRow();
            System.out.println(String.valueOf(pos));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }

    }

    public int getRowCount() {
        int cnt = 0;
        int apos = 0;
        try {
            apos = rs.getRow();
            rs.last();
            cnt = rs.getRow();
            if (apos > 0)
                rs.absolute(apos);
        } catch (SQLException ex) {
            System.out.println("getRowCount: " + ex);
        }

        return cnt;
    }

    public int getColumnCount() {
        return 3;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        // make it jump back to pos !!
        Object val = null;
        Integer intVal;

        try {
            if (rowIndex == 0) {
                pos = rs.getRow();
                total = getRowCount();
            }
            rs.absolute(rowIndex + 1);
            switch (columnIndex) {
                case 0: intVal = rs.getInt(1); val = intVal; break;
                case 1: val = rs.getString(2); break;
                case 2: val = rs.getString(3); break;
                default: val = "error";
            }
            rs.absolute(pos);
        } catch (SQLException sqle) {
            JOptionPane.showMessageDialog(null, "Trouble in model");
        }
        return val;
    }

    private ResultSet rs;
    private int pos, total;
}

回答by Olantobi

If you are loading the data from a database table, I think the best way to go is to limit the data coming from the database. Then apply a simple algorithm for the next and previous buttons.

如果您从数据库表加载数据,我认为最好的方法是限制来自数据库的数据。然后为下一个和上一个按钮应用一个简单的算法。