简体   繁体   中英

How to remove extra white space from the bottom of the 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 Program:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM