title: How to set size to Jtable using GridBagLayOut
body: <p>I am using GridBagLayout to position GUI components. I have used a Jtable inside a JFrame. But the table is not fitting to the frame. It appears as follows. When I drag the frame it shows the table.</p>
  
  <p><a href="https://i.stack.imgur.com/SerWM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SerWM.jpg" alt="enter image description here"></a></p>
  
  <p>Here is my code. I used this as a pop up Jframe to a button listner.</p>
  
  <pre><code>public class ViewPrescriptionHistory extends JFrame{
  
      private GridBagConstraints gridbBagConstraints;
      private JTable drugDetailsTable;
      HistroyTableModel drugDetailsTabelModel = new HistroyTableModel();
  
      public ViewPrescriptionHistory() {
          this.setLayout(new GridBagLayout());
          this.setSize(500, 500);
  
          this.pack();
          initialize();
      }
  
      private void initialize() {
          gridbBagConstraints = new GridBagConstraints();
          gridbBagConstraints.insets = new Insets(10, 0, 10, 0);
          gridbBagConstraints.ipady = 10;
          gridbBagConstraints.ipadx = 10;
  
          drugDetailsTable = new JTable(drugDetailsTabelModel);
          drugDetailsTable.setPreferredScrollableViewportSize(new Dimension(600, 100));
          drugDetailsTable.setFillsViewportHeight(true);
  
          JScrollPane scrollPane = new JScrollPane(drugDetailsTable);
          scrollPane.setBounds(5, 218, 884, 194);
  
  
          this.add(scrollPane);
  
      }
  }
  
  Here is my table model. I used Abstract Table model.
  
      import java.util.ArrayList;
      import java.util.List;
      import javax.swing.table.AbstractTableModel; 
      import assignment3.prescriptionsystem.model.HistoryModel;
  
      public class HistroyTableModel extends AbstractTableModel{
  
          private String[] columnNames = { "Doctor Name", "Date", "Drug Name", "Form" , "Dosage" };
          List&lt;HistoryModel&gt; historyList = new ArrayList&lt;HistoryModel&gt;();
  
          public String getColumnName(int column) {
              return columnNames[column];
  
          }
  
          public int getColumnCount() {
  
              return columnNames.length;
          }
  
          public int getRowCount() {
  
              return historyList.size();
          }
  
          public void reloadDrugList(List&lt;HistoryModel&gt; drugsData) {
              historyList= drugsData;
              fireTableDataChanged();
          }
  
          public Object getValueAt(int rowIndex, int columnIndex) {
              HistoryModel drugData = historyList.get(rowIndex);
              switch (columnIndex) {
              case 0:
                  return drugData.getDocName();
              case 1:
                  return drugData.getDate();
              case 2:
                  return drugData.getDrugName();
              case 3:
                  return drugData.getFrom();
              case 4:
                  return drugData.getDosage();
              default:
                  return null;
  
              }
  
          }
  
  
  }
  </code></pre>
  
  <p>Here is my model class</p>
  
  <pre><code>public class HistoryModel {
      String docName;
      String date;
      String drugName;
      String dosage;
      String from;
      public String getDocName() {
          return docName;
      }
      public void setDocName(String docName) {
          this.docName = docName;
      }
      public String getDate() {
          return date;
      }
      public void setDate(String date) {
          this.date = date;
      }
      public String getDrugName() {
          return drugName;
      }
      public void setDrugName(String drugName) {
          this.drugName = drugName;
      }
      public String getDosage() {
          return dosage;
      }
      public void setDosage(String dosage) {
          this.dosage = dosage;
      }
      public String getFrom() {
          return from;
      }
      public void setFrom(String from) {
          this.from = from;
      }
  }
  </code></pre>
  
  <p>How to fix this issue?</p>