0

I'm having problem properly using JTable. This is run through a thread and I want the JTable to be able to update its values for each tick. The values (from physics) are updated at each tick. I have tried using

SpecialTable.repaint();
SpecialTable.revalidate();

which apparently does nothing. I have also tried SpecialTable.fireTableDataChanged() suggested in other thread: JTable How to refresh table model after insert delete or update the data.. This also does nothing since fireTableDataChanged(); is undefined for JTable. If I try casting it:

((AbstractTableModel) SpecialTable).fireTableDataChanged()

I get a compiler errror: Unresolved compilation problem: Cannot cast from JTable to AbstractTableModel

I have also tried reinitializing JTable every tick of the simluation by creating a remakeTable-method for my JPanel-class and calling that one every tick but it doesn't do anything either.

class SpecialTable extends AbstractTableModel {

Model physics;
Object[][] partikelinfo = new Object[10][4];
String[] columns = new String[4];

SpecialTable(Model m){
    this.physics = m;
    LinkedList<Particle> partiklar = physics.getParticles();

    String[] tmp  = { "Particle no.", "Coordinates", "Moving",
            "Tracking" };
    for (int i = 0; i < tmp.length; i++) {
        Arrays.fill(columns, i, i+1, tmp[i]);
    }

    Object[][] tmp2 = {

            { "1", partiklar.get(1).y + " , " + partiklar.get(1).x,
                    partiklar.get(1).isMoving, new Boolean(true) },
            { "2", partiklar.get(2).y + " , " + partiklar.get(2).x,
                    partiklar.get(2).isMoving, new Boolean(true) },
            { "3", partiklar.get(3).y + " , " + partiklar.get(3).x,
                    partiklar.get(3).isMoving, new Boolean(true) },
            { "4", partiklar.get(4).y + " , " + partiklar.get(4).x,
                    partiklar.get(4).isMoving, new Boolean(true) },
            { "5", partiklar.get(5).y + " , " + partiklar.get(5).x,
                    partiklar.get(5).isMoving, new Boolean(true) },
            { "6", partiklar.get(6).y + " , " + partiklar.get(6).x,
                    partiklar.get(6).isMoving, new Boolean(true) },
            { "7", partiklar.get(7).y + " , " + partiklar.get(7).x,
                    partiklar.get(7).isMoving, new Boolean(true) },
            { "8", partiklar.get(8).y + " , " + partiklar.get(8).x,
                    partiklar.get(8).isMoving, new Boolean(true) },
            { "9", partiklar.get(9).y + " , " + partiklar.get(9).x,
                    partiklar.get(9).isMoving, new Boolean(true) },
            { "10", partiklar.get(10).y + " , " + partiklar.get(10).x,
                    partiklar.get(10).isMoving, new Boolean(true) } 
                    };

    for (int i = 0; i < tmp2.length; i++) {
        Arrays.fill(partikelinfo, i, i+1, tmp2[i]);
    }
}

    public int getColumnCount() {
        return columns.length;
    }

    public int getRowCount() {
        return partikelinfo.length;
    }

    public String getColumnName(int col) {
        return columns[col];
    }

    public Object getValueAt(int row, int col) {
        return partikelinfo[row][col];
    }


    public Class<? extends Object> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public void setValueAt(Object value, int row, int col) {
        partikelinfo[row][col] = value;
        fireTableCellUpdated(row, col);
    }

    public boolean isCellEditable(int row, int col) {
        if (col < 3) {
            return false;
        } else {
            return true;
        }
    }
    //Don't know how to call this || if it works.
    public void updateValues(){
        fireTableDataChanged();
    }

}

I don't know whether the problem lies with SpecialTable not getting the updated values or whether they just don't display or if there's a third alternative.

Community
  • 1
  • 1
  • fireTableDataChanged should be called from outside of the context of the model. JTable can't be cast to TableModel, because it's not one, you'd have to use JTable#getModel, but see my first first comment – MadProgrammer Feb 25 '16 at 19:45
  • *"This is run through a thread"* Swing is not thread, so special consideration needs to made when updating it. The basic concept would be to update the values in the model and let the model trigger the events to which the JTable can respond to – MadProgrammer Feb 25 '16 at 19:49
  • If I were to let the model trigger the event how would I go about doing that? Do i create a TableListener or ActionEvent, are these to be put in the JFrame, JPanel or JTable? Or does the model itself need to create an event? I'm not very familiar with listerners. – Strange Brew Feb 25 '16 at 20:03
  • The JTable registers it self as TableModelListener automatically when you call setModel on the JTable – MadProgrammer Feb 25 '16 at 20:10
  • 1
    Maybe something like [this](http://stackoverflow.com/questions/26477744/how-to-search-subfolders-and-repaint-a-jtable-with-new-data-in-java/26478059#26478059) – MadProgrammer Feb 25 '16 at 20:57
  • BRILLIANT!!! Yes exactly this!!! I rewrote the getValuesAt method and now they update! Thank you so much :D – Strange Brew Feb 25 '16 at 21:39
  • Also (for anyone else with the same issue) in the thread-tick i am calling: ((AbstractTableModel).MyPanel.MyTable.getModel()).fireTableDataChanged(); – Strange Brew Feb 25 '16 at 21:48
  • Don't call any `fireXxx` methods on a `TableModel` from outside the context of the model itself, it's the model's responsibility to decide when these methods get called – MadProgrammer Feb 25 '16 at 21:52
  • Hmm, I'm not sure how to do that. Is it better to use repaint()? – Strange Brew Feb 25 '16 at 21:57
  • 1
    No, it's better to have a method in your `TableModel` which you can call which knows what it wants to do – MadProgrammer Feb 25 '16 at 21:59

0 Answers0