Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to add contents to display on a <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JTable.html" rel="noreferrer"><code>JTable</code></a>, one uses the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/TableModel.html" rel="noreferrer"><code>TableModel</code></a> to add items to display.</p> <p>One of a way to add a row of data to the <code>DefaultTableModel</code> is by using the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html#addRow(java.lang.Object[])" rel="noreferrer"><code>addRow</code></a> method which will take an array of <code>Object</code>s that represents the objects in the row. Since there are no methods to directly add contents from an <code>ArrayList</code>, one can create an array of <code>Object</code>s by accessing the contents of the <code>ArrayList</code>.</p> <p>The following example uses a <code>KeyValuePair</code> class which is a holder for data (similar to your <code>Track</code> class), which will be used to populate a <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html" rel="noreferrer"><code>DefaultTableModel</code></a> to display a table as a <code>JTable</code>:</p> <pre><code>class KeyValuePair { public String key; public String value; public KeyValuePair(String k, String v) { key = k; value = v; } } // ArrayList containing the data to display in the table. ArrayList&lt;KeyValuePair&gt; list = new ArrayList&lt;KeyValuePair&gt;(); list.add(new KeyValuePair("Foo1", "Bar1")); list.add(new KeyValuePair("Foo2", "Bar2")); list.add(new KeyValuePair("Foo3", "Bar3")); // Instantiate JTable and DefaultTableModel, and set it as the // TableModel for the JTable. JTable table = new JTable(); DefaultTableModel model = new DefaultTableModel(); table.setModel(model); model.setColumnIdentifiers(new String[] {"Key", "Value"}); // Populate the JTable (TableModel) with data from ArrayList for (KeyValuePair p : list) { model.addRow(new String[] {p.key, p.value}); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    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