Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

swing - JTable row limitation

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

Like this

Pageable table

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...