我正在使用GridBagLayout來定位GUI組件。我在JFrame中使用了Jtable。但桌子不適合框架。它顯示如下。當我拖動框架時,它會顯示錶格。如何使用GridBagLayOut將大小設置爲Jtable
這裏是我的代碼。我用它作爲彈出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;
}
}
如何解決這個問題?
我們不能編譯這段代碼,因爲我們沒有'HistroyTableMo del'。見[mcve]。 – user1803551