0

Today I did some experiments with a web service.

Everything works as expected. But now I have some issues with my gui.

I would like to show a table with students of a certain course. Therefore I have got a combobox with all possible groups and a loading-button. Now I want to show the students of the selected course as soon as the user triggered the button.

I found out that my DAO Object works fine and the insertData method gets executed with the correct Vectors. Unfortunately my ScrollPane stays empty and no table gets displayed.

How can I change and display the table, everytime a new course gets selected?

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

    }
}
Michael Konz
  • 170
  • 15
  • If you want the table to remain, but its data to change, simply give the JTable a new TableModel when a new course is selected. the JTable `.setModel(...)` method would solve this. – Hovercraft Full Of Eels Jan 04 '18 at 19:11
  • 1
    Your own code creates a totally new JTable object but this of course will not display as the new object is never added to your GUI. But having said this, I again don't recommend that you go this route, and instead simply swap or update the model that the current JTable already has. – Hovercraft Full Of Eels Jan 04 '18 at 19:27
  • Thank you. The following code did the trick:public void insertData(Vector data, Vector head) { tabelle.setModel(new DefaultTableModel(data, head)); tabelle.getColumn("id").setMinWidth(0); tabelle.getColumn("id").setMaxWidth(0); } – Michael Konz Jan 04 '18 at 19:33

0 Answers0