Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

refresh my JTable


Hello there,

I have a JTable with database records in it.when i add a new record i want to reload the table dynamically to show the new record,instead of closing and opening the frame again to show the new record.


if(total != 0)
{
      tf.reloadRecord();
}


 public void reloadRecord()
    {
          initCollections(JFParentFrame.getDBConnection());
          jspCity.getViewport().remove(cityTable);
          createCityTable();
          jspCity.getViewport().add(cityTable);
          cityPane.repaint();
    }

this is the method i call.but the table does not reload.

please help.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you don't need to remove/add the table.
You need your model to fire the appropriate event when you add data to it.
How are you adding the record? Are you saying you want the actual stored data in the db to change?
Avatar of Zolf

ASKER

private void createCityTable()
    {
          String[] columnNames = {"City ID",
                        "City Farsi Name",
                        "City English Name",
                        "Prefix"};

          model = new DefaultTableModel(columnNames, 0);

          cityTable = new JTable(model);

          cityTable.getColumnModel().getColumn(0).setMaxWidth(60);
          cityTable.getColumnModel().getColumn(0).setPreferredWidth(60);
          cityTable.getColumnModel().getColumn(3).setMaxWidth(200);
          cityTable.getColumnModel().getColumn(3).setPreferredWidth(200);
          
          
          for (int i = 0; i < cityCollect.size(); i++)
          {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)cityCollect.get(i);
                CityRow row = (CityRow)node.getUserObject();
                System.out.println(row);
                model.addRow(new Object[] {row.getCityId(),
                              row.getCityFarsiName(),
                              row.getCityEngName(),
                              row.getPrefix()
                        });
          }

          cityTable.setEnabled(false);
          
    }
Avatar of Zolf

ASKER


i commented the add and remove method.but did not help
After

model.addRow(..

call

model.fireTableDataChanged();
Avatar of Zolf

ASKER



when i add a new data to the db,i want it to show in my Jtable
you don't need to recreate the table (now would you want to), you can just change the model

private void createCityTable()
    {
         String[] columnNames = {"City ID",
                    "City Farsi Name",
                    "City English Name",
                    "Prefix"};

         model = new DefaultTableModel(columnNames, 0);
         for (int i = 0; i < cityCollect.size(); i++)
         {
              DefaultMutableTreeNode node = (DefaultMutableTreeNode)cityCollect.get(i);
              CityRow row = (CityRow)node.getUserObject();
              System.out.println(row);
              model.addRow(new Object[] {row.getCityId(),
                         row.getCityFarsiName(),
                         row.getCityEngName(),
                         row.getPrefix()
                    });
         }


         if (cityTable==null) {
           cityTable = new JTable(model);

           cityTable.getColumnModel().getColumn(0).setMaxWidth(60);
           cityTable.getColumnModel().getColumn(0).setPreferredWidth(60);
           cityTable.getColumnModel().getColumn(3).setMaxWidth(200);
           cityTable.getColumnModel().getColumn(3).setPreferredWidth(200);
         cityTable.setEnabled(false);
         
         } else {
            cityTable.setModel(model);
         }  
         
    }

if you're only adding rows you don't need to recreate the model each time either, just justy row inserted event

>>cityTable.setEnabled(false);

Not sure why you're calling that. If you don't want the user to change it, you need to override isCellEditable in your model
Avatar of Zolf

ASKER


i add model.fireTableDataChanged(); after coming out of the for loop.but no use.

i also changed my codee to what objects pasted.but no use.

please help me.i need to make this work.

> i also changed my codee to what objects pasted.but no use.

so whats happening?
did you also remove the code to remove/add the tabel?  you need to

 public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         createCityTable();
    }
You've now changed

>>
 public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         jspCity.getViewport().remove(cityTable);
         createCityTable();
         jspCity.getViewport().add(cityTable);
         cityPane.repaint();
    }
>>


to

 public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());

         createCityTable();

    }

have you?
> i also changed my codee to what objects pasted.but no use.

did you also remove the code to ermove/add table u need to

 public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         createCityTable();
    }
Avatar of Zolf

ASKER


yes i changed the code to this
also inserted

public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         createCityTable();
    }

and added model.fireTableDataChanged(); after the for loop


private void createCityTable()
    {
         String[] columnNames = {"City ID",
                    "City Farsi Name",
                    "City English Name",
                    "Prefix"};

         model = new DefaultTableModel(columnNames, 0);
         for (int i = 0; i < cityCollect.size(); i++)
         {
              DefaultMutableTreeNode node = (DefaultMutableTreeNode)cityCollect.get(i);
              CityRow row = (CityRow)node.getUserObject();
              System.out.println(row);
              model.addRow(new Object[] {row.getCityId(),
                         row.getCityFarsiName(),
                         row.getCityEngName(),
                         row.getPrefix()
                    });
             
         }
         model.fireTableDataChanged();

         if (cityTable==null) {
           cityTable = new JTable(model);

           cityTable.getColumnModel().getColumn(0).setMaxWidth(60);
           cityTable.getColumnModel().getColumn(0).setPreferredWidth(60);
           cityTable.getColumnModel().getColumn(3).setMaxWidth(200);
           cityTable.getColumnModel().getColumn(3).setPreferredWidth(200);
         cityTable.setEnabled(false);
         
         } else {
            cityTable.setModel(model);
         }  
         
    }
> and added model.fireTableDataChanged(); after the for loop

you don't need (or want) that, it does nothing where it is.
If you want to fire an event it should be *after* it has been added to the table


Avatar of Zolf

ASKER

let me again explain what i am trying to do.I have a JTable which gets populated from the db.i have a button which lets me add new data to the db which works fine.but the thing is i have to close the JInternalFrame and again open it to view the new data which i inserted into the db.now i want to be abale to see the new data as soon as i insert the data.the code is below
if(srcObj=="update")
                        {
                              if(RequiredFieldEmpty()==false)
                              {
                                    if(ADDING_STATE == true)
                                    {
                                                initCollections(OwnerForm.getDBConnection());      
                                                   // Start Display the new record
                                                   int total =0;
                                                   total = clsPublicMethods.getMaxNum("SELECT * FROM city ORDER BY city_id ASC",OwnerForm.getDBConnection(),"city_id");
                                                   if(total != 0)
                                                   {
                                                                                                                  System.out.print("TOTAL  1 "+total);
                                                                                                                  tf.reloadRecord();
                                                   }
                                                   else
                                                   {
                                                                                                                  System.out.print("TOTAL 2 "+total);

                                                         tf.reloadRecord("SELECT * FROM city ORDER BY city_id ASC");
                                                   }
                                                   total =0;
                                                   // End Display the new record
                                                   JOptionPane.showMessageDialog(null,"New record has been successfully added.","Naparansoft Inventory System",JOptionPane.INFORMATION_MESSAGE);
                                                   String ObjButtons[] = {"Yes","No"};
                                                int PromptResult = JOptionPane.showOptionDialog(null,"Do you want add another record?","Naparansoft Inventory System",JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE,null,ObjButtons,ObjButtons[0]);
                                                
                                                if(PromptResult==0)
                                                {
                                                      clearFields();
                                                      JTFCEngName.requestFocus(true);
                                                }else
                                                {
                                                      dispose();
                                                }
                                    }
If you place

javax.swing.JOptionPane.showMessageDialog(null, "Called");

at the end of that method, what do you see?
Avatar of Zolf

ASKER



ok i have deleted that statement
You need to call any processing - intensive tasks from a separate thread. You could use a SwingWorker
try this, and tell me what happens

private void createCityTable()
    {
         String[] columnNames = {"City ID",
                    "City Farsi Name",
                    "City English Name",
                    "Prefix"};

         model = new DefaultTableModel(columnNames, 0);
         for (int i = 0; i < cityCollect.size(); i++)
         {
              DefaultMutableTreeNode node = (DefaultMutableTreeNode)cityCollect.get(i);
              CityRow row = (CityRow)node.getUserObject();
              System.out.println(row);
              model.addRow(new Object[] {row.getCityId(),
                         row.getCityFarsiName(),
                         row.getCityEngName(),
                         row.getPrefix()
                    });
             
         }
         model.fireTableDataChanged();

         if (cityTable==null) {
           cityTable = new JTable(model);        
         } else {
            cityTable.setModel(model);
         }  
           cityTable.getColumnModel().getColumn(0).setMaxWidth(60);
           cityTable.getColumnModel().getColumn(0).setPreferredWidth(60);
           cityTable.getColumnModel().getColumn(3).setMaxWidth(200);
           cityTable.getColumnModel().getColumn(3).setPreferredWidth(200);
           cityTable.setEnabled(false);
         
    }


why do you reaload the whole table btw, instead of just adding the new row?
Avatar of Zolf

ASKER


whcih method
>>ok i have deleted that statement

That won't make any difference, but it is redundant
Easiest would be to add the new row to the model at the same time that you add it to the database.
That way you wouldn't need to reload the table, which you should avoid doing anywaty.
Avatar of Zolf

ASKER


i changed the createCityTable() but still it does not show me the new data which i inserted into the db.only when i close and open the frame i can see the new record
Avatar of Zolf

ASKER


could you please guide me how to do that
try calling revalidate() and repaint() on the container that the table is in.

though I'd suggest instead just adding the new row insteqad of reloading the whole table.
((DefaultTableModel)(cityTable.getModel())).addRow(........
Avatar of Zolf

ASKER


cehj i added javax.swing.JOptionPane.showMessageDialog(null, "Called"); at the end and it pops up after i insert new data


actually i also call createCityTable() in the constr of the class to create the table the first time i start the swing application.

///////////////constr////////////////////

//create tab panel
        tabPane = new JTabbedPane();
       
        cityPane = new JPanel(new BorderLayout());
        createCityTable();
        jspCity = new JScrollPane(cityTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        cityPane.add(jspCity, BorderLayout.CENTER);
        tabPane.addTab("  City  ", cityIcon, cityPane);

So precisely what *do* you see afterwards - an empty table or what?
Avatar of Zolf

ASKER


this is what i did

public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         createCityTable();
         javax.swing.JOptionPane.showMessageDialog(null, "Called");
     }

then i just see that message dialog and i click ok.nothing special hapens.

No

>>javax.swing.JOptionPane.showMessageDialog(null, "Called");

can go now - it's served its purpose. I mean what do you see in your table?
Avatar of Zolf

ASKER


i only see my old records
where do you add the record?
you should just be able to do the following and not call createCityTable() at all

model.addRow(new Object[] {newrow.getCityId(),
                         newrow.getCityFarsiName(),
                         newrow.getCityEngName(),
                         newrow.getPrefix()
                    });
Avatar of Zolf

ASKER

here i get the data from the db and save it in a vector.

private void initCollections(Connection connect)
    {
          cityCollect = new Vector();
          try
          {
                Statement st = connect.createStatement();
                
                  String query = "SELECT city_id, city_eng_name, city_farsi_name, prefix FROM city ORDER BY city_id";
                  ResultSet result = st.executeQuery(query);

                  while (result.next())
                  {
                        CityRow row = new CityRow(result.getInt(1),
                                                      result.getString(2),
                                                      result.getString(3),
                                                      result.getString(4));
                        
                        cityCollect.add(new DefaultMutableTreeNode(row));
                  }
          }
          catch (SQLException sqle)
          {
                System.err.println("Can't get city collection from database : " + sqle.getMessage());
          }
    }
Avatar of Zolf

ASKER

private void updateCityCollection(Connection connect)
            {
                Vector cityCollect = new Vector();
                try
                {
                      Statement st = connect.createStatement();
                      //"INSERT INTO CITY (CITY_ID,CITY_ENG_NAME,CITY_FARSI_NAME,PREFIX) VALUES (city_id_seq.nextval,'"+JTFCEngName.getText()+"','"+JTFCFarName.getText()+"','"+JTFCPrefix.getText()+"')");
                        //String query = "SELECT city_id, city_eng_name, city_farsi_name, prefix FROM city ORDER BY city_id";
                      String query = "INSERT INTO CITY (CITY_ID,CITY_ENG_NAME,CITY_FARSI_NAME,PREFIX) VALUES (city_id_seq.nextval,'"+JTFCEngName.getText()+"','"+JTFCFarName.getText()+"','"+JTFCPrefix.getText()+"')";
                        st.executeUpdate(query);

                }
                catch (SQLException sqle)
                {
                      System.err.println("Can't get city collection from database : " + sqle.getMessage());
                }
          }
      
          
          private void initCollections(Connection connect)
          {
                updateCityCollection(connect);
          }
Avatar of Zolf

ASKER

public void actionPerformed(ActionEvent e)
                  {
                        String srcObj = e.getActionCommand();
                        
                        if(srcObj=="update")
                        {
                              if(RequiredFieldEmpty()==false)
                              {
                                    if(ADDING_STATE == true)
                                    {
                                                initCollections(OwnerForm.getDBConnection());      
                                                   // Start Display the new record
                                                   int total =0;
                                                   total = clsPublicMethods.getMaxNum("SELECT * FROM city ORDER BY city_id ASC",OwnerForm.getDBConnection(),"city_id");
                                                   if(total != 0)
                                                   {
                                                         //FrmCustomer.reloadRecord("SELECT * FROM tblCustomer WHERE CustomerIndex = " + total + " ORDER BY CompanyName ASC");
                                                         System.out.print("TOTAL  1 "+total);
                                                         //tf.reloadRecord("SELECT * FROM city WHERE City_id = " + total + " ORDER BY city_id ASC");
                                                         tf.reloadRecord();
                                                   }
>>if(srcObj=="update")

should be


if("update".equals(srcObj))
>                     st.executeUpdate(query);

after the update you should just be able to add your row and avoid reloading completely

model.addRow(new Object[] {id,
                         JTFCFarName.getText(),
                         JTFCEngName.getText(),
                         JTFCPrefix.getText()
                    });


the other thing to check is that somewhere else in your code isn't changing nthe value of cityTabel such so that you're updating the wronmg table.
Avatar of Zolf

ASKER

the update is in Frm_add_edit_city class extends JDialog

st.executeUpdate(query);

and i create the UI i.e the table and other things in another class TreeTableInternalFrame extends JInternalFrame.


can i call the model.addRow(new Object[] {id,
                         JTFCFarName.getText(),
                         JTFCEngName.getText(),
                         JTFCPrefix.getText()
                    });
 

in Frm_add_edit_city class extends JDialog
yes, you'd just need to pass a reference to the model.
or if the dialog has a reference to the framer it can access the model from there
Avatar of Zolf

ASKER


i cannot make those changes you mentioned because then i have to change a lot of things in my application which is becoming tedious.can i not just change the above codes to make it work

What I posted above should work.
I suspect something else in your code may be causing the problem

Perhaps try simplifying things in an attempt to pinpoint the problem
Following should clear the table

private void createCityTable()
    {

         if (cityTable==null) {
            String[] columnNames = {"City ID",
                       "City Farsi Name",
                       "City English Name",
                       "Prefix"};
            model = new DefaultTableModel(columnNames, 0);
            for (int i = 0; i < cityCollect.size(); i++)
            {
                 DefaultMutableTreeNode node = (DefaultMutableTreeNode)cityCollect.get(i);
                 CityRow row = (CityRow)node.getUserObject();
                 System.out.println(row);
                 model.addRow(new Object[] {row.getCityId(),
                            row.getCityFarsiName(),
                            row.getCityEngName(),
                            row.getPrefix()
                       });
             
         }
           cityTable = new JTable(model);        
           cityTable.getColumnModel().getColumn(0).setMaxWidth(60);
           cityTable.getColumnModel().getColumn(0).setPreferredWidth(60);
           cityTable.getColumnModel().getColumn(3).setMaxWidth(200);
           cityTable.getColumnModel().getColumn(3).setPreferredWidth(200);
           cityTable.setEnabled(false);
         } else {
            JOptionPane.showMessageDialog(null, "Clearing table");
            cityTable.setModel(new DefaultTableModel());
         }  
         
    }
Avatar of Zolf

ASKER


when i add new data i get that dialog box saying clearing table,but i dont see any change in it.what do you think.as you mentioned i will try to check the code line by line.
have you tried revalidating the tables container as I suggested earlier?

cityPane.revalidate();
cityPane.repaint();
Avatar of Zolf

ASKER


where do i add this code in my constr??
Avatar of Zolf

ASKER


i mean do i add this bit in my constr or in the reload method
in the relaod() after calling setModel()
Avatar of Zolf

ASKER


hi objects,

i figured out one thing.when i comment the createCityTable method from the constructor,the table is not build the first time i open the frame(which is obvious),but then when i add a new data and call reloadRecord.it does not create the table still even though my reloadrecord is like this

public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         //javax.swing.JOptionPane.showMessageDialog(null, "Called");
         createCityTable();
         createAndInitTree();
         tabPane.repaint();
    }
Avatar of Zolf

ASKER


in the reload method can i just call its contructor again to build the complete frame from new
> createAndInitTree();

whats that doing? Does it do anything to table?
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER


i am sorry that is the JTree bit which should also show the new data inside it but does not show the new data in the tree too.forget that createAndInitTree().now i am only focusing on the table bit
Avatar of Zolf

ASKER


i have a jtree on the left of the frame and the jtable on the right.the add bit should add the new data to both sides.but for now i am just want the table part to work
> ut for now i am just want the table part to work

then comment out as much other code as possible to minimise possible side effects.
Avatar of Zolf

ASKER


ok
Avatar of Zolf

ASKER



this is the constr of the class where the table is

public TreeTableInternalFrame(final PictMenuFrameDemo frame)
    {
        super("InternalFrame",
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable

        Dimension deskSize = frame.getDesktop().getSize();
        setSize((int)deskSize.getWidth() - 20, (int)deskSize.getHeight() - 20);
        //Set the window's location.
        setLocation(10, 10);
        JFParentFrame = frame;
        //get database result in to collections
        initCollections(frame.getDBConnection());
       
        testIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/dslamIcon.png"));
       
          cityIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/cityIcon.png"));
          coIcon =  new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/coIcon.png"));
          dslamIcon =  new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/dslamIcon.png"));
          linecardIcon =  new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/cardIcon.png"));

       
        //set layout for frame content panel
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
       
        //create toolbar
        JToolBar toolbar = createToolbar();
        cp.add(toolbar, BorderLayout.NORTH);

        // create popup menu for tree
        //treePopupMenu = createPopupMenuDslam();
       
        // create tree element
        createAndInitTree();
              
        JScrollPane jsp = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
       
       
        //create tab panel
        tabPane = new JTabbedPane();
       
        cityPane = new JPanel(new BorderLayout());
       
        createCityTable();

        jspCity = new JScrollPane(cityTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        cityPane.add(jspCity, BorderLayout.CENTER);
        tabPane.addTab("  City  ", cityIcon, cityPane);
       
        JPanel coPane = new JPanel(new BorderLayout());
        createCoTable();
        JScrollPane jspCo = new JScrollPane(coTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        coPane.add(jspCo, BorderLayout.CENTER);
        tabPane.addTab("   Co   ", coIcon, coPane);
       
        JPanel dslamPane = new JPanel(new BorderLayout());
        createDslamTable();
        JScrollPane jspDslam = new JScrollPane(dslamTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        dslamPane.add(jspDslam, BorderLayout.CENTER);
        tabPane.addTab("  Dslam  ", dslamIcon, dslamPane);
       
       
        JPanel linecardPane = new JPanel(new BorderLayout());
        createlinecardTable();
        JScrollPane jspLinecard = new JScrollPane(linecardTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        linecardPane.add(jspLinecard, BorderLayout.CENTER);
        tabPane.addTab("  Linecard  ", linecardIcon, linecardPane);
               
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jsp, tabPane);
        cp.add(splitPane, BorderLayout.CENTER);
       
        addInternalFrameListener(new InternalFrameAdapter() {
              public void internalFrameClosing(InternalFrameEvent ife) {
                    frame.setInnerFrameNull();  
              }
        });
    }

and this is the method in the above class which is called in another class when i enter new data to be saved in db

public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         //javax.swing.JOptionPane.showMessageDialog(null, "Called");
         createCityTable();
         cityPane.revalidate();
         cityPane.repaint();
    }


and this is the method which is called the first time in the constr of the above class and the second time it is called in the reloadRecord method

private void createCityTable()
    {
          System.out.println("Inside createCityTable() ");
         if (cityTable==null)
         {
            String[] columnNames = {"City ID",
                       "City Farsi Name",
                       "City English Name",
                       "Prefix"};
            model = new DefaultTableModel(columnNames, 0);
           
            for (int i = 0; i < cityCollect.size(); i++)
            {
                 DefaultMutableTreeNode node = (DefaultMutableTreeNode)cityCollect.get(i);
                 CityRow row = (CityRow)node.getUserObject();
                 System.out.println("city "+row);
                 model.addRow(new Object[] {row.getCityId(),
                            row.getCityFarsiName(),
                            row.getCityEngName(),
                            row.getPrefix()
                       });
             
            }
           cityTable = new JTable(model);        
           cityTable.getColumnModel().getColumn(0).setMaxWidth(60);
           cityTable.getColumnModel().getColumn(0).setPreferredWidth(60);
           cityTable.getColumnModel().getColumn(3).setMaxWidth(200);
           cityTable.getColumnModel().getColumn(3).setPreferredWidth(200);
           cityTable.setEnabled(false);
         }
         else
         {
            JOptionPane.showMessageDialog(null, "Clearing table");
            cityTable.setModel(new DefaultTableModel());
         }  
         
    }
Avatar of Zolf

ASKER


please help
Avatar of Zolf

ASKER


somewhere i read that the connection should be closed or else the table will not refresh.does that matter.??
you shouldn't need to close the connection.

if you can post a small example that I can run that reproduces the prob I'll try it here.
Avatar of Zolf

ASKER


please help
Avatar of Zolf

ASKER


could you find why the table does not refresh
Avatar of Zolf

ASKER


can anybody help me to get this thing working
Avatar of Zolf

ASKER


hello there,

these are the class and methods that is responsible for the jtable
this is the constr of the class where the table is

public TreeTableInternalFrame(final PictMenuFrameDemo frame)
    {
        super("InternalFrame",
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable

        Dimension deskSize = frame.getDesktop().getSize();
        setSize((int)deskSize.getWidth() - 20, (int)deskSize.getHeight() - 20);
        //Set the window's location.
        setLocation(10, 10);
        JFParentFrame = frame;
        //get database result in to collections
        initCollections(frame.getDBConnection());
       
        testIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/dslamIcon.png"));
       
         cityIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/cityIcon.png"));
         coIcon =  new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/coIcon.png"));
         dslamIcon =  new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/dslamIcon.png"));
         linecardIcon =  new ImageIcon(Toolkit.getDefaultToolkit().createImage("images/cardIcon.png"));

       
        //set layout for frame content panel
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
       
        //create toolbar
        JToolBar toolbar = createToolbar();
        cp.add(toolbar, BorderLayout.NORTH);

        // create popup menu for tree
        //treePopupMenu = createPopupMenuDslam();
       
        // create tree element
        createAndInitTree();
             
        JScrollPane jsp = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
       
       
        //create tab panel
        tabPane = new JTabbedPane();
       
        cityPane = new JPanel(new BorderLayout());
       
        createCityTable();

        jspCity = new JScrollPane(cityTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        cityPane.add(jspCity, BorderLayout.CENTER);
        tabPane.addTab("  City  ", cityIcon, cityPane);
       
        JPanel coPane = new JPanel(new BorderLayout());
        createCoTable();
        JScrollPane jspCo = new JScrollPane(coTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        coPane.add(jspCo, BorderLayout.CENTER);
        tabPane.addTab("   Co   ", coIcon, coPane);
       
        JPanel dslamPane = new JPanel(new BorderLayout());
        createDslamTable();
        JScrollPane jspDslam = new JScrollPane(dslamTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        dslamPane.add(jspDslam, BorderLayout.CENTER);
        tabPane.addTab("  Dslam  ", dslamIcon, dslamPane);
       
       
        JPanel linecardPane = new JPanel(new BorderLayout());
        createlinecardTable();
        JScrollPane jspLinecard = new JScrollPane(linecardTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        linecardPane.add(jspLinecard, BorderLayout.CENTER);
        tabPane.addTab("  Linecard  ", linecardIcon, linecardPane);
               
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jsp, tabPane);
        cp.add(splitPane, BorderLayout.CENTER);
       
        addInternalFrameListener(new InternalFrameAdapter() {
             public void internalFrameClosing(InternalFrameEvent ife) {
                  frame.setInnerFrameNull();  
             }
        });
    }

and this is the method in the above class which is called in another class when i enter new data to be saved in db

public void reloadRecord()
    {
         initCollections(JFParentFrame.getDBConnection());
         //javax.swing.JOptionPane.showMessageDialog(null, "Called");
         createCityTable();
         cityPane.revalidate();
         cityPane.repaint();
    }


and this is the method which is called the first time in the constr of the above class and the second time it is called in the reloadRecord method

private void createCityTable()
    {
         System.out.println("Inside createCityTable() ");
         if (cityTable==null)
         {
            String[] columnNames = {"City ID",
                       "City Farsi Name",
                       "City English Name",
                       "Prefix"};
            model = new DefaultTableModel(columnNames, 0);
           
            for (int i = 0; i < cityCollect.size(); i++)
            {
                 DefaultMutableTreeNode node = (DefaultMutableTreeNode)cityCollect.get(i);
                 CityRow row = (CityRow)node.getUserObject();
                 System.out.println("city "+row);
                 model.addRow(new Object[] {row.getCityId(),
                            row.getCityFarsiName(),
                            row.getCityEngName(),
                            row.getPrefix()
                       });
             
            }
           cityTable = new JTable(model);        
           cityTable.getColumnModel().getColumn(0).setMaxWidth(60);
           cityTable.getColumnModel().getColumn(0).setPreferredWidth(60);
           cityTable.getColumnModel().getColumn(3).setMaxWidth(200);
           cityTable.getColumnModel().getColumn(3).setPreferredWidth(200);
           cityTable.setEnabled(false);
         }
         else
         {
            JOptionPane.showMessageDialog(null, "Clearing table");
            cityTable.setModel(new DefaultTableModel());
         }  
         
    }
Avatar of riaancornelius
riaancornelius

after you add the rows (or make any change to the model), try calling
cityTable.repaintUI();

It's unlikely that it will solve the problem, but if you change some of the renderers, it is sometimes necessary to call updateUI()...
Avatar of Zolf

ASKER



did you mean cityTable.updateUI();
>          tf.reloadRecord();

check that tf is referencing the frame that contains your table
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER


how can i check that tf is refering to that frame
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)