2017-08-03 105 views
0

我正在使用GridBagLayout來定位GUI組件。我在JFrame中使用了Jtable。但桌子不適合框架。它顯示如下。當我拖動框架時,它會顯示錶格。如何使用GridBagLayOut將大小設置爲Jtable

enter image description here

這裏是我的代碼。我用它作爲彈出Jframe到按鈕列表器。

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<HistoryModel> historyList = new ArrayList<HistoryModel>(); 

     public String getColumnName(int column) { 
      return columnNames[column]; 

     } 

     public int getColumnCount() { 

      return columnNames.length; 
     } 

     public int getRowCount() { 

      return historyList.size(); 
     } 

     public void reloadDrugList(List<HistoryModel> 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; 

      } 

     } 


} 

這裏是我的模型類

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

如何解決這個問題?

+0

我們不能編譯這段代碼,因爲我們沒有'HistroyTableMo del'。見[mcve]。 – user1803551

回答

0

首先,您應該將您的組件添加到JFrame的內容窗格上,並將其設置爲GridBagLayout,而不是JFrame本身。考慮從Oracle教程所採取的圍堵層次圖像:

Container heirarchy

你也應該調用pack()後您添加了組件,前:

Pack方法調整框架的大小以使其所有內容達到或超過其首選大小。 pack的一個替代方法是通過調用setSize或setBounds(它也設置幀的位置)來顯式地建立一個幀的大小。一般來說,使用包比調用setSize更好,因爲包離開了框架佈局管理器負責框架大小,並且佈局管理器善於調整平臺依賴性和影響組件大小的其他因素。

因爲pack()處理JFrame的大小,你並不需要顯式調用setSize()

所以,你的代碼應該是這樣的:

getContentPane().setLayout(new GridBagLayout()); // Set the layout on the content pane rather than the JFrame 

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

getContentPane().add(scrollPane); // Add the scroll pane to the content pane rather than the JFrame 

pack(); // Call pack at the end 

,並將所得GUI的樣子:

GUI result

+0

它工作正常。非常感謝你。 – user3724599

+1

@ user3724599如果這個答案已經解決了你的問題,請考慮通過點擊複選標記來接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲您提供了一些信譽,併爲提供解決方案的用戶提供了一些信譽。沒有義務這樣做。 – explv

相關問題