Question

I have a JTable that's filled with the inventory data of a garage. It has parts, number in storage etc. At this moment i have a JOptionPane where you can enter the data if you want to add a new part to the inventory. that works fine but how do i refresh the JTable data after the part is added?

This is the code I use to fille the table initially :

private String[] kolommenOnderdelen = { "Nr", "Omschrijving", "Vooraad" };
private DefaultTableModel modelOnderdelen = new DefaultTableModel(kolommenOnderdelen, 0);

    for (Onderdeel a : voorraad.alleOnderdelen()) {
        modelOnderdelen.addRow(new String[] {
                Integer.toString(a.getOnderdeelNummer()),
                a.getOnderdeelOmschrijving(),
                Integer.toString(a.getOnderdeelVoorraad()) });
    }

    tabelOnderdelen = new JTable(modelOnderdelen)

and this is the code I have for the JOptionPane

        if (JOptionPane.showConfirmDialog(null, nieuwOnderdeel,
                "Voer de gegevens van het nieuwe onderdeel in",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
            if (!onderdeelOmschrijving.getText().equals("")
                    && !onderdeelNR.getText().equals("")
                    && !onderdeelVoorraad.getText().equals("")) {
                if (voorraad.voegOnderdeelToe(new Onderdeel(
                        onderdeelOmschrijving.getText(), Integer
                                .parseInt(onderdeelNR.getText()), Integer
                                .parseInt(onderdeelVoorraad.getText())))) {
                    JOptionPane.showMessageDialog(null,
                            "Het onderdeel is toegevoegd");
                }
            } else {
                JOptionPane.showMessageDialog(null,
                        "Niet volledig ingevuld", "Vul alle gegevens in!",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
Was it helpful?

Solution

You have to add the new row to the tableModel as you did when you init the table. Using modelOnderdelen.addRow(..) will work, this will notify listeners (the view) and the new row will be shown in JTable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top