Note that there are some explanatory texts on larger screens.

plurals
  1. POaddRow to DefaultTableModel in a separate class Eclipse
    text
    copied!<p>I have created a DefaultTableModel that is showing a very basic user league table in a JTabbedPane.</p> <p>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:</p> <pre><code>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); } } </code></pre> <p>then I call this from my main class:</p> <pre><code>... 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); </code></pre> <p>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:</p> <pre><code> leagueTable.addRow(table.getRowCount(), new Object[]{"ange", 5, 3}); </code></pre> <p>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</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload