Note that there are some explanatory texts on larger screens.

plurals
  1. POJTable refresh issue
    primarykey
    data
    text
    <p>I'm having a hard time with a <code>JTable</code> data refreshing.</p> <p>I'm implementing my own <code>TableModel</code>, extending from <code>javax.swing.table.AbstractTableModel</code>, with a method that receives an <code>java.util.ArrayList&lt;&gt;</code> and process it to build an <code>Object[][]</code> from where the table data is read.</p> <p>The logic is as following:</p> <p>I'm implementing a JList with some record keys, when the user selects one, a method is fired that picks up the key and queries a MySQL database to fetch records related with that key. The final result is an ArrayList&lt;> that I passed to the TableModel to construct the data and then set it to my table.</p> <p>It works normally but after a few hits, it stops displaying the table data, and after a little time, it starts working again. I'm not sure if I'm having problem refreshing the table or the entire GUI.</p> <p>This is a excerpt of the code:</p> <p>Method for build the data of the table:</p> <pre><code>public void createTableData(java.util.ArrayList&lt;Lote&gt; alLote) { //Lote is an entity class modeling the database table. TABLE_DATA = new Object[alLote.size()][TABLE_COLUMNS.length]; //TABLE_DATA is an Object array, TABLE_COLUMNS is a String[] containing the names of the columns. int i = 0; for (Lote element : alLote) { int j = 0; TABLE_DATA[i][j++] = element.getFirstColumnValue(); //and so son i++; } super.fireTableDataChanged(); } </code></pre> <p>When launching the GUI, it starts with an empty ArrayList&lt;>:</p> <pre><code>LoteTableModel ltm = new LoteTableModel(); arrayList = new ArrayList&lt;Lote&gt;(); ltm.createTableData(arrayList); myTable = new JTable(); myTable.setModel(ltm); scrollPane = new JScrollPane(myTable); </code></pre> <p>I'm using DesignGridLayout btw, a RowGroup which hides when the table has no records and shows when it has results, and a JScrollPane as table container.</p> <pre><code>dgl = new DesignGridLayout(panel); dgl.row().center().group(group).add(scrollPaneContainingTable).fill(); centerPanel.add(panel, BorderLayout.CENTER); getContentPane().add(centerPanel); centerPanel.setVisible(true); </code></pre> <p>Then, everytime the user selects an item from a JList, I query the database and fetch records related to the selected value:</p> <pre><code>public void listSelectionChanged() { bloque = alBloque.get(listBloque.getSelectedIndex()); //bloque is another entity, alBloque is an ArrayList&lt;Bloque&gt; from which the JList (named listBloque) ListModel is constructed. try { //database query here, omitted if (result.getStatus() == SUCCESS) { //the query returned a populated ArrayList&lt;Lote&gt; alLote = result.getArray(); ((LoteTableModel) myTable.getModel()).createTableData(alLote); myTable.revalidate(); } } catch (Exception e) {} } </code></pre> <p>As I stated before, it works fine a couple of times, clicking the values on the list displays their related records on the table, but then after clicking several ones, it stops displaying data at all. Leaving it for a moment and then clicking on some value again, will display the correct data again, so as I told before, I'm not sure if is more like a GUI refreshing issue.</p> <p>Any help will be greatly appreciated.</p>
    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.
 

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