Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tested your code and while I didn't get the exact same error, there's a couple of weird things:</p> <p>First, you are calling both <code>jMyTable.addColumn(...)</code> and <code>model.addColumn(...)</code>. Which is a mistake. You only need to call the model's addColumn. Adding also a column with the JTable method will create inconsistencies, because the TableModel and the TableColumnModel are not synchronized.</p> <p>Your code to add a new column into the model, and the calculation of the maximums is wrong. <code>i</code> should move through rows, and <code>j</code> should move through columns, and then check if we should do the <code>MAX[i] = ndata[i][j];</code> assignment. And while passing <code>new Object[0]</code> doesn't appear to cause any issues (because that constructor with a null value is valid) it doesn't make any sense.</p> <p>It should be something like this:</p> <pre><code>private void jFindmaximumActionPerformed(java.awt.event.ActionEvent evt) { DefaultTableModel model = (DefaultTableModel)jMYtable.getModel(); String headerLabel = "Maximum"; Object[] MAX = new Object[ndata.length]; for(int i = 0; i &lt; ndata.length; i++){ MAX[i] = ndata[i][0]; for(int j = 0 ; j &lt; ndata[0].length; j ++){ if(ndata[i][j] &gt; MAX[i]){ MAX[i] = ndata[i][j]; } } } model.addColumn(headerLabel, MAX); } </code></pre> <p>On a side note, if you ever were to implement your own TableModel, extending AbstractTableModel, when the memory structure (created and handled by yourself) that contains the table data changes, you have to update the table with the new values or new column/rows changes using an assortment of methods in the <code>AbstractTableModel</code> class to notify the JTable: <code>fireTableDataChanged()</code>, <code>fireRowsInserted()</code>, <code>fireTableStructureChanged()</code>...</p> <p><strong>UPDATE:</strong></p> <p>Here's a simple test code that adds columns to a JTable.</p> <pre><code>import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel; public class SimpleColumnAdderFrame { private DefaultTableModel _model; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SimpleColumnAdderFrame().initUI(); } }); } public void initUI() { JFrame frame = new JFrame(); Object[][] data = { {1,2,3}, {10,20,30} }; String[] columnNames = {"Column 1", "Column 2", "Column 3"}; _model = new DefaultTableModel(data, columnNames); JTable table = new JTable(_model); JButton addColumnButton = new JButton("Add column"); addColumnButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int columns = _model.getColumnCount(); int rows = _model.getRowCount(); Object[] columnData = new Object[rows]; for (int i = 0; i &lt; rows; ++i) { columnData[i] = (int) ((columns + 1)*(Math.pow(10, i))); // &lt;- Unnecessarily complicated cell value } _model.addColumn("New Column " + (columns + 1), columnData); } }); JButton addRowButton = new JButton("Add row"); addRowButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int columns = _model.getColumnCount(); int rows = _model.getRowCount(); Object[] rowData = new Object[columns]; for (int i = 0; i &lt; columns; ++i) { rowData[i] = (int) ((i+1)*(Math.pow(10, rows))); // &lt;- Unnecessarily complicated cell value } _model.addRow(rowData); } }); JPanel buttonPanel = new JPanel(new GridLayout(1,2,2,2)); buttonPanel.add(addColumnButton); buttonPanel.add(addRowButton); frame.add(new JScrollPane(table), BorderLayout.CENTER); frame.add(buttonPanel, BorderLayout.PAGE_END); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.pack(); frame.setVisible(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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