Why cannot add row to jtable?

105 Views Asked by At

I need some help with addrow to jTable.

I have 2 jFrames, first one has jTable1 and second one has jTextfields. I would like to add data (string) from second frame to jTable1 in first frame. I tried many versions and watch many tutorials..but no success.

in first frame where i have jTable1 i have SwingWorker and i wrote:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         jButton1.setEnabled(false);
         jButton2.setEnabled(true);
         Boolean praznaMapa = false;         
         worker = new SwingWorker<Void, String>(){
            @Override
            protected Void doInBackground() throws Exception {
 
                if(isCancelled())
                {
                    return null;
                }
                AddRowToTable(new Object[]
                {
                    PrviStolpec,DrugiStolpec,TretjiStolpec,timeStamp,
                });
}}
worker.execute();}

     public static void AddRowToTable (Object [] data)
    {
       
        DefaultTableModel tblmodel = (DefaultTableModel)jTable1.getModel();
        tblmodel.insertRow(0, data);
    }

and in the second frame where i have textfields i wrote:

String PrviStolpec = FIRSTFRAME_Okno.withoutExtension;
String DrugiStolpec = "OK";
String TretjiStolpec = FIRSTFRAME_Okno.stSledenja;
String timeStamp = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
                FIRSTFRAME_Okno.AddRowToTable(new Object[]
                {
                    PrviStolpec,DrugiStolpec,TretjiStolpec,timeStamp,
                });     
                FIRSTFRAME_Okno.jTable1.setSelectionBackground(Color.green);

but when i run program, nothing happen. No error and no data in jTable1 :(

Any suggestion?

Thank you!

1

There are 1 best solutions below

0
Blaz Cus On

I found solution!

It is really stupid one, but anyway...

I have in second class:

FIRSTFRAME_Okno = new FIRSTFRAME_Okno();

Because of that jTable1 was not updated.

Every function which i call from second class i use:

FIRSTFRAME_Okno.functionName();

Tnx!