1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hema Chandra
  • 363
  • 2
  • 4
  • 16
  • `c.add(new JScrollPane(table));`, you can rid of the other two `c.add` lines. `JTable#setFillsViewportHeight` might also be helpful – MadProgrammer Feb 24 '17 at 05:10
  • You mean [like this](http://stackoverflow.com/a/6175860/418556)? – Andrew Thompson Feb 24 '17 at 05:14
  • No, I mean like this: See the 2nd image of the table in the following link: [link](http://stackoverflow.com/questions/18274975/setpreferredscrollableviewportsize-vs-setfillsviewportheight-in-jtable) I want the same format for my table in the output – Hema Chandra Feb 24 '17 at 05:55

1 Answers1

6

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

image

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);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045