![](/img/trans.png)
[英]I have to resize my JFrame for the contents to show up. How can i fix that?
[英]DefaultTableModel, titles wont show up. How can I fix this?
这是我的学生小组的开始。 我试过将JScrollPanes添加到它中,从我读过的内容来看,应该可以使它工作,但它根本不会显示标题。 我可能会错误地添加它们,但我目前还不确定。 任何帮助将不胜感激。 谢谢!
//Start of student panel
JPanel stuPanel = new JPanel();
tabbedPane.addTab("Student", stuPanel);
JButton stuButton = new JButton("Add Student");
Object[] stuColumnNames = {"First Name", "Last Name", "Major", "Minor", "GPA"};
JTable stuTable = new JTable(new DefaultTableModel(data, stuColumnNames)){
/**
*
*/
private static final long serialVersionUID = 1L;
//Making it so the cells I want are editable.
@Override
public boolean isCellEditable(int row, int column) {
if (column == 0){
return false;
}
else
return true;
}
};
DefaultTableModel stuModel = (DefaultTableModel)stuTable.getModel();
for(Student tmpS : stuList){
stuModel.addRow(new Object[]{tmpS.getFirstName(),tmpS.getLastName(), tmpS.getMajor(), tmpS.getMinor(), tmpS.getGpa()});
}
stuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String fName,lName,major,minor;
float gpa;
System.out.println("Please enter a first name:");
fName = in.nextLine();
System.out.println("Please enter a last name:");
lName = in.nextLine();
System.out.println("Please enter a major:");
major = in.nextLine();
System.out.println("Please enter a minor:");
minor = in.nextLine();
System.out.println("Please enter a GPA:");
gpa = in.nextFloat();
in.nextLine();
stuList.add(new Student(fName, lName, major, minor, gpa));
stuModel.addRow(new Object[]{fName, lName, major, minor, gpa});
stuModel.fireTableDataChanged();
}
});
GroupLayout gl_stuPanel = new GroupLayout(stuPanel);
gl_stuPanel.setHorizontalGroup(
gl_stuPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_stuPanel.createSequentialGroup()
.addGroup(gl_stuPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_stuPanel.createSequentialGroup()
.addGap(155)
.addComponent(stuButton))
.addGroup(gl_stuPanel.createSequentialGroup()
.addContainerGap()
.addComponent(stuTable, GroupLayout.PREFERRED_SIZE, 408, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
gl_stuPanel.setVerticalGroup(
gl_stuPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_stuPanel.createSequentialGroup()
.addContainerGap()
.addComponent(stuButton)
.addPreferredGap(ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
.addComponent(stuTable, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))
);
stuPanel.setLayout(gl_stuPanel);
//End of student panel
JTable
列标题显示在JScrollPane
的标题部分中
您需要将JTable
包装在JScrollPane
,类似...
JScrollPane scrollPane = new JScrollPane(stuTable);
gl_stuPanel.setVerticalGroup(
gl_stuPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_stuPanel.createSequentialGroup()
.addContainerGap()
.addComponent(scrollPane)
.addPreferredGap(ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))
);
例如
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.