如何从JTable的底部删除多余的空白? [英] How to remove extra white space from the bottom of the JTable?

查看:82
本文介绍了如何从JTable的底部删除多余的空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的程序来创建一个空的JTable.但是,当我显示表格时,表格下方会显示一个额外的空白.现在,我要删除表格行下方的空白.

I have written a simple program that creates an empty JTable. But when I display the Table an extra white space is visible below the Table.Now I want to remove that whitespace below the rows of table.

JTable程序:

public class Table extends JFrame {

Table() 
{
      int numColoumns=5;
      int numRows=10 ;
      String[] colHeadings = {"COLUMN1","COLUMN2","COLUMN3","COLUMN4","COLUMN5"};

      DefaultTableModel model = new DefaultTableModel(numRows, numColoumns) ;
      model.setColumnIdentifiers(colHeadings);

      JTable table = new JTable(model);
      JTableHeader head = table.getTableHeader();

      Container c = getContentPane();
      c.setLayout(new BorderLayout());
      c.add("North", head);
      c.add("Center",table);
}       
public static void main(String[] args) throws NumberFormatException, IOException
{
    Table t = new Table();
    t.setSize(500,400);
    t.setVisible(true);
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}

程序给了我一张桌子,但是行没有完全填充到窗口中.它显示了一个具有给定的高度和宽度(以像素为单位)以及相应的行数的窗口.在创建的行下方,由于行未填充到窗口中,因此显示了额外的空间.任何人都可以帮助我从窗口中删除多余的空间.

The program gives me a table,but the rows are not filled completely to the window. It shows a window with given height and width in pixels and respective number of rows.Below the rows created it shows an extra space as the rows are not filled to the window. Can anyone help me in removing the extra space from the window.

抱歉,我创建了输出的图像,但是上传图像时出现问题.

Sorry I created an image of the output but there is a problem in uploading the image.

推荐答案

此处所示,您可以覆盖 Scrollable 方法 pack() 封闭的窗口,使此Window的大小适合其子组件的首选大小和布局."

As shown here, you can override the Scrollable method getPreferredScrollableViewportSize() to return a value that reflects your initial desired row count. Each time a row is added, pack() the enclosing window, which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/**
 * @see https://stackoverflow.com/a/37343900/230513
 * @see https://stackoverflow.com/a/37318673/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        class MyTableModel extends AbstractTableModel {

            private int n = 8;

            private void addRow() {
                n++;
                fireTableRowsInserted(n - 1, n - 1);
            }

            @Override
            public int getRowCount() {
                return n;
            }

            @Override
            public int getColumnCount() {
                return 4;
            }

            @Override
            public Object getValueAt(int rowIndex, int colIndex) {
                return "R" + rowIndex + ":C" + colIndex;
            }
        };
        MyTableModel model = new MyTableModel();
        JTable table = new JTable(model) {
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(super.getPreferredSize().width,
                    getRowHeight() * getRowCount());
            }
        };
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        f.add(new JScrollPane(table));
        f.add(new JButton(new AbstractAction("Add Row") {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.addRow();
                f.pack();
            }
        }), BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

这篇关于如何从JTable的底部删除多余的空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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