0

I have created a DefaultTableModel that is showing a very basic user league table in a JTabbedPane.

I want to add a row using some data I collect from a different class. This code will not run but here is a sample:

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Statistics extends JPanel {
    public Object[][] data;

public Statistics() {
    super(new GridLayout(1,0));
    String[] columnNames = {"Name", "Games Played", "Games Won"};
    Object[][] data = {
            {"Tom", new Integer(5), new Integer(2)},
            {"Steve", new Integer(2), new Integer(0)},
    };
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model);
    table.setFillsViewportHeight(true);
    table.setVisible(true);
    table.setEnabled(false);

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
}


}

then I call this from my main class:

...
stats = new JPanel(); //create a new JPanel for table to go on
...
tp.addTab ("Statistics", stats); // add panel to JTabbedPane
..
leagueTable = new Statistics();// add the table to the stats panel
       stats.add(leagueTable);

this is showing up and its fine, but can anyone guide me as to what syntax I use to add a row, I have tried:

 leagueTable.addRow(table.getRowCount(), new Object[]{"ange", 5, 3});

but this does not work, eclipse asks me to add a method called 'addRow' to Statistics class, but I thought 'addRow' was already a method of DefaultTableModel, so I am very confused. Can anyone help me out on how to add a row of data to the table? Thanks IA

jzd
  • 23,473
  • 9
  • 54
  • 76
Ange King
  • 147
  • 1
  • 2
  • 12

2 Answers2

2

addRow is a method of the TableModel but you're invoking the method on the Statistics class. You could create a new method to add the data:

public class Statistics extends JPanel {
    private DefaultTableModel model

    public Statistics() {
       super(new GridLayout(1,0));
       model = new DefaultTableModel(data, columnNames);
       ...
    }

    public void addData(Object[] data) {
       model.addRow(data);
    }
}

Side note: Typically you don't want to extend JPanel here if not adding new functionality so a redesign is probably in order

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I would create a new method in `Statistics` for adding the row data – Reimeus Oct 22 '13 at 12:33
  • I have tried to create a method, but not exactly sure what I am supposed to write in the method. I have tried several things but nothing so far has worked. – Ange King Oct 22 '13 at 12:36
  • I have already tried this method, I get a nullpointerexception... It seems like model isn't getting initialised.. but it must be because the table is showing in the gui. – Ange King Oct 22 '13 at 12:46
  • That's because you're shadowing the `model` variable in the constructor - remove the initlial `DefaultTableModel` type name – Reimeus Oct 22 '13 at 12:48
  • OMG that worked!! thank you so much, I knew it was something simple.. can't see the forest through the trees sometimes :-). – Ange King Oct 22 '13 at 12:51
2

leagueTable is an instance of your Statistics class. So it doesn't have an addRow() method.

Possible solution:

  • Make your DefaultTableModel a variable in your Statistics class.
  • Create a method in Statistics that interacts with the table model by calling addRow() on the model.
jzd
  • 23,473
  • 9
  • 54
  • 76
  • Thank you. when you say 'save a reference' - what does that mean? as in, declare it in the main class? I have tried to create a method to call but not sure what it is supposed to return. I have tried a lot of different things but keep getting the dreaded red squiggly lines! – Ange King Oct 22 '13 at 12:34
  • Yes save it as a variable in the Statistics class you created. – jzd Oct 22 '13 at 12:38
  • Thank you, your comments helped a lot :-) – Ange King Oct 22 '13 at 12:52