今天我做了一些Web服务的实验。
一切都按预期工作。但现在我的gui有些问题。
我想向学生展示一个特定课程的表格。因此,我有一个组合框,包含所有可能的组和一个加载按钮。现在,我想在用户触发按钮后立即向学生展示所选课程。
我发现我的DAO对象工作正常,并且insertData方法使用正确的Vectors执行。不幸的是我的ScrollPane保持空白,没有显示任何表格。
每次选择新课程时,如何更改和显示表格?
package mycode.gui;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import javax.swing.SwingConstants;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
public class AbsenzenMelden extends JPanel {
private JComboBox klassenwahl;
private JButton laden;
private JTable tabelle;
private JScrollPane sp;
public AbsenzenMelden() {
JLabel headline = new JLabel("Bitte tragen Sie die Absenzen ein");
headline.setHorizontalAlignment(SwingConstants.CENTER);
headline.setFont(new Font("Tahoma", Font.PLAIN, 13));
JLabel lblKlasse = new JLabel("Klasse:");
lblKlasse.setFont(new Font("Tahoma", Font.PLAIN, 13));
klassenwahl = new JComboBox();
klassenwahl.setFont(new Font("Tahoma", Font.PLAIN, 13));
laden = new JButton("laden");
laden.setFont(new Font("Tahoma", Font.PLAIN, 13));
tabelle = new JTable();
sp = new JScrollPane(tabelle);
JButton okbutton = new JButton("speichern");
okbutton.setFont(new Font("Tahoma", Font.PLAIN, 13));
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(sp, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
.addComponent(headline, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(lblKlasse, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(klassenwahl, GroupLayout.PREFERRED_SIZE, 206, GroupLayout.PREFERRED_SIZE)
.addGap(31)
.addComponent(laden))
.addComponent(okbutton, Alignment.TRAILING))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(headline, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblKlasse, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(klassenwahl, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(laden))
.addGap(18)
.addComponent(sp, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
.addGap(10)
.addComponent(okbutton, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
setLayout(groupLayout);
}
public void insertKlassen(List<String>klassen)
{
for (String aktuell : klassen)
{
klassenwahl.addItem(aktuell);
}
}
public String getKlasse()
{
return (String) klassenwahl.getSelectedItem();
}
public void addLadenListener(ActionListener al)
{
laden.addActionListener(al);
}
public void insertData(Vector data, Vector head)
{
tabelle = new JTable(data, head);
tabelle.getColumn("id").setMinWidth(0);
tabelle.getColumn("id").setMaxWidth(0);
tabelle.repaint();
tabelle.revalidate();
sp.repaint();
sp.revalidate();
}
}