Java JTable set preferred scrollable viewport size

Description

Java JTable set preferred scrollable viewport size


import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main {
   public static void main(String args[]) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      DefaultTableModel model = new DefaultTableModel();
      model.setColumnIdentifiers(new Object[] { "Column 1", "Column 2", "Column 3" });

      JTable table = new JTable(model);
      for (int count = 0; count < 3; count++) {
         model.insertRow(count, new Object[] { count, "name", "age" });
      }//from  w  w  w  .  j a  v a 2s  . co  m

      table.setPreferredScrollableViewportSize(new Dimension(500, 70));
      table.setFillsViewportHeight(true);
      
      frame.add(new JScrollPane(table));
      frame.setLocationByPlatform(true);
      frame.pack();
      frame.setVisible(true);
      

   }
}



PreviousNext

Related