简体   繁体   中英

Array data in a JTable

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM