gpt4 book ai didi

swing - JTable 行数限制

转载 作者:行者123 更新时间:2023-12-01 10:42:01 26 4
gpt4 key购买 nike

我必须限制 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 showing another set of 10 records.

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

像这样

Pageable table

固定行表.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);
}
});
}
}

关于swing - JTable 行数限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6175139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com