0

The following code is used to setup the rows and columns of a JTable:

    private void createUIComponents() {
    ergebnisModel = (DefaultTableModel) setupColumnsAndRows();
    ergebnisTabelle = new JTable(ergebnisModel);
    ergebnisTabelle.setFont(new Font("Arial", Font.PLAIN, 15));

    //ergebnisModel.setRowCount(0);

    tabellenScroll = new JScrollPane(ergebnisTabelle);

    vergleichPlotter = new Plotter(calculator, 450, 400, Plotter.GraphType.VERGLEICH);
    stromPlotter = new Plotter(calculator, 180, 150, Plotter.GraphType.STROM);
    spannungPlotter = new Plotter(calculator, 180, 150, Plotter.GraphType.SPANNUNG);
    leistungPlotter = new Plotter(calculator, 180, 150, Plotter.GraphType.LEISTUNG);
}

private TableModel setupColumnsAndRows() {
    DefaultTableModel ergebnisModel = new DefaultTableModel();
    ergebnisModel.addColumn("Größen");
    ergebnisModel.addColumn("R in Ohm");
    ergebnisModel.addColumn("XL in Ohm");
    ergebnisModel.addColumn("XC in Ohm");

    ergebnisModel.addRow(new String[] {"Widerstand", String.valueOf(calculator.getWiderstand()),
            String.valueOf(calculator.calculateXL()), String.valueOf(calculator.calculateXC())});
    ergebnisModel.addRow(new String[]{"I1", String.valueOf(calculator.getSpannung() / calculator.getWiderstand()), null, null});
    ergebnisModel.addRow(new String[]{"I2", null, String.valueOf(calculator.getSpannung() / calculator.calculateXL()), null});
    ergebnisModel.addRow(new String[]{"I3", null, null, String.valueOf(calculator.getSpannung() / -calculator.calculateXC())});
    ergebnisModel.addRow(new String[] {null, null, null, null});
    ergebnisModel.addRow(new String[]{"P", String.valueOf(calculator.calculateP()), null, null});
    ergebnisModel.addRow(new String[]{"Q", String.valueOf(calculator.calculateQ()), null, null});
    ergebnisModel.addRow(new String[]{"S", String.valueOf(calculator.calculateS()), null, null});
    ergebnisModel.addRow(new String[]{"R Ges", String.valueOf(calculator.calculateGesWiderstand()), null, null});
    ergebnisModel.addRow(new String[]{"I Ges", String.valueOf(calculator.getGesamtstrom()), null, null});

    return ergebnisModel;
}

} where null represents a blank space. Calculator is -surprise, surprise- my calculator class which does all mathimatical functions. It works with numbers that were passed in previously through a GUI.

This table is part of a output-dialog. Now the problem I have encountered is that after opening the dialog a second time during the same session with different input from the first screen, the data is not refreshed. It still shows calculations that were done with the old input.

It can't be a problem of passing the calculator object since other components (the Plotter instances) relying on this object are always up to

Where is the problem?

And is there another -nicer- way of putting in data into the table?

EDIT

I'm using IntelliJ's GUI Builder which uses XML files to mark the componentbeans' state. Thus some components (see above) are created manually and others we the given XML files.

Wecherowski
  • 818
  • 11
  • 24

1 Answers1

2
model.fireTableDataChanged();

Don't invoke the method directly. It is the responsibility of the model to invoke the method when the data is changed.

after opening the dialog a second time during the same session with different input from the first screen, the data is not refreshed.

I'm guessing you are creating a new JTable, but that table is just sitting in memory and hasn't actually been added to the viewport of your scroll pane.

Don't create a new JTable and TableModel.

Instead just refresh the model with new data:

model.setRowCount(0);
model.addRow(...);
...
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you for your answer, where exactly do I have to put in the code to refresh the data? – Wecherowski May 16 '16 at 21:55
  • Because I see what you mean but where else can I place the creation of the JTable ? – Wecherowski May 16 '16 at 22:15
  • You create the JTable when you add all your other components to the frame. After that you just refresh the table by clearing the TableModel and than adding new rows of data to the model. – camickr May 17 '16 at 00:55
  • But it shouldn't matter whether I refresh the new data since the whole JDialog is newly created, should it? – Wecherowski May 24 '16 at 22:01