Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you need a simple working solution, try using <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html" rel="noreferrer">DefaultTableModel</a>.</p> <p>If you have created your own table model, that extends AbstractTableModel, then you should also implement removeRow() method. The exact implementation depends on the underlying structure, that you have used to store data.</p> <p>For example, if you have used Vector, then it may be something like this:</p> <pre><code>public class SimpleTableModel extends AbstractTableModel { private Vector&lt;String&gt; columnNames = new Vector&lt;String&gt;(); // Each value in the vector is a row; String[] - row data; private Vector&lt;String[]&gt; data = new Vector&lt;String[]&gt;(); ... public String getValueAt(int row, int col) { return data.get(row)[col]; } ... public void removeRow(int row) { data.removeElementAt(row); } } </code></pre> <p>If you have used List, then it would be very much alike:</p> <pre><code>// Each item in the list is a row; String[] - row data; List&lt;String[]&gt; arr = new ArrayList&lt;String[]&gt;(); public void removeRow(int row) { data.remove(row); } </code></pre> <p>HashMap:</p> <pre><code>//Integer - row number; String[] - row data; HashMap&lt;Integer, String[]&gt; data = new HashMap&lt;Integer, String[]&gt;(); public void removeRow(Integer row) { data.remove(row); } </code></pre> <p>And if you are using arrays like this one</p> <pre><code>String[][] data = { { "a", "b" }, { "c", "d" } }; </code></pre> <p>then you're out of luck, because there is no way to dynamically remove elements from arrays. You may try to use arrays by storing separately some flags notifying which rows are deleted and which are not, or by some other devious way, but I would advise against it... That would introduce unnecessary complexity, and would in fact just be <em>solving a problem by creating another</em>. That's a sure-fire way to end up <a href="http://thedailywtf.com/" rel="noreferrer">here</a>. Try one of the above ways to store your table data instead.</p> <p>For better understanding of how this works, and what to do to make your own model work properly, I <b>strongly</b> advise you to refer to <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html" rel="noreferrer">Java Tutorial</a>, <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html" rel="noreferrer">DefaultTableModel API</a> and it's <a href="http://developer.classpath.org/doc/javax/swing/table/DefaultTableModel-source.html" rel="noreferrer">source code</a>.</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