bircastri
bircastri

Reputation: 2159

How to change column model of my TableModel dynamically

I have a simple Java Desktop application that show a JTable with a custom TableModel.

In base of a boolean variables, I want to show a Table with different column. But I'm not able to do this.

This is my code:

static String[] ColName = { "Cod.Articolo","Nome Articolo","Cod.Barre", "Qtà.iniziale","Scontrini(-)","Bolla(-)","Fattura(-)","DDT(-)","NC(+)","Carico(+)"};
static String[] ColNameNero = { "Cod.Articolo","Nome Articolo","Cod.Barre", "Qtà.iniziale","Scontrini(-)","Scontrini Nero(-)","Bolla(-)","Fattura(-)","DDT(-)","NC(+)","Carico(+)"};


public void creaTabellaMerci(boolean isNero){
    try{
        if(isNero)
            tableMovimentiMagazzinoMerci = new MyTableModelMovimentiMagazzinoMerci(ColNameNero,isNero);
        else
            tableMovimentiMagazzinoMerci = new MyTableModelMovimentiMagazzinoMerci(ColName,isNero);

        tableMovimentiMerci = new DefaultTableCustom(tableMovimentiMagazzinoMerci);
        sorter = new TableRowSorter<MyTableModelMovimentiMagazzinoMerci>(tableMovimentiMagazzinoMerci);
        tableMovimentiMerci.setRowSorter(sorter);
        jScrollPaneAmministrazione = new javax.swing.JScrollPane();
        jScrollPaneAmministrazione.setViewportView(tableMovimentiMerci);
        jScrollPaneAmministrazione.setPreferredSize(dTabella2);
        jScrollPaneAmministrazione.getViewport().add(tableMovimentiMerci);
        tableMovimentiMagazzinoMerci.fireTableDataChanged();
        tableMovimentiMerci.repaint();
    }catch(Exception e){
        log.logStackTrace(e);
    }
}

Now at first time, I call the method with variables isNero = true. At the second time, I call the same method with variables isNero = false but the columns not changes.

How can I fix it ?

Upvotes: 0

Views: 210

Answers (1)

camickr
camickr

Reputation: 324128

jScrollPaneAmministrazione = new javax.swing.JScrollPane();

You create a new JScrollPane, but you never add the scroll pane to the frame. Changing the value of the reference variable does NOT add the component to the frame.

Don't create a new JTable or JScrollPane!

Instead you can just update the TableModel of the table that is currently display on the frame:

//tableMovimentiMerci = new DefaultTableCustom(tableMovimentiMagazzinoMerci);
tableMovimentiMerci.setModel( tableMovementiMagazzinoMerci );
sorter = new TableRowSorter<MyTableModelMovimentiMagazzinoMerci>(tableMovimentiMagazzinoMerci);
tableMovimentiMerci.setRowSorter(sorter);
//jScrollPaneAmministrazione = new javax.swing.JScrollPane();
//jScrollPaneAmministrazione.setViewportView(tableMovimentiMerci);
//jScrollPaneAmministrazione.setPreferredSize(dTabella2);
//jScrollPaneAmministrazione.getViewport().add(tableMovimentiMerci);
//tableMovimentiMagazzinoMerci.fireTableDataChanged();
//tableMovimentiMerci.repaint();

Upvotes: 1

Related Questions