matel matel - 2 months ago 13
Java Question

JFrame doesn't paint after receiving an event

I have my code below, I am trying to receive m_rows which is an array. It looks like it stays empty and my frame stays blank. I can see the class has received the event. I think my class Table works ok. Do you see anything obviously wrong in the code below between the receipt of m_rows from the listener and passing it to TableModel(m_rows)?

Thanks

class Frame extends JFrame implements FlowListener {

private List<Candle> m_rows = new ArrayList<Candle>();

public Frame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Ma premiere fenetre");
setBounds(50, 100, 1000, 800);

Container conteneur = new JPanel();
TableModel tableModel = new TableModel(m_rows);
JTable table = new JTable(tableModel);

JPanel tablePanel = new JPanel();

tablePanel.add(table);

conteneur.setLayout(new GridLayout(2, 1));
conteneur.add(tablePanel);

this.add(conteneur);
this.setVisible(true);

}

@Override
public void updateOnFlow(List<Candle> newFlow) {
m_rows = newFlow;
this.repaint();

}

}

Answer

I think the problem is that assigning something to m_row has no effect on the TableModel. Try this:

class Frame extends JFrame implements FlowListener {
    private JTable table;
    ...

    public Frame() {
        ...
        TableModel tableModel = new TableModel(m_rows);  // Did you mean DefaultTableModel?
        table = new JTable(tableModel);
        ...
    }

    @Override
    public void updateOnFlow(List<Candle> newFlow) {
        m_rows = newFlow;
        TableModel tableModel = new TableModel(m_rows);
        table.setModel(tableModel);
        this.repaint();
    }
}
Comments