如何从JTable的底部删除额外的空白?

时间:2021-05-12 06:09:00

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的简单程序。但当我显示该表时,可以看到该表下面有一个额外的空白。现在我要删除表行下面的空格。

JTable Program:

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.

抱歉,我创建了输出的图像,但是上传图像有问题。

1 个解决方案

#1


3  

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."

如图所示,您可以覆盖可滚动的方法getPreferredScrollableViewportSize(),以返回一个值,该值反映您最初所需的行数。每次添加一行时,都要打包()封闭窗口,这将“使该窗口的大小适合其子组件的首选大小和布局”。

如何从JTable的底部删除额外的空白?

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://*.com/a/37343900/230513
 * @see https://*.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);
    }
}

#1


3  

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."

如图所示,您可以覆盖可滚动的方法getPreferredScrollableViewportSize(),以返回一个值,该值反映您最初所需的行数。每次添加一行时,都要打包()封闭窗口,这将“使该窗口的大小适合其子组件的首选大小和布局”。

如何从JTable的底部删除额外的空白?

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://*.com/a/37343900/230513
 * @see https://*.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);
    }
}