[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

refresh my JTable

Posted on 2006-05-06
71
Medium Priority
?
933 Views
Last Modified: 2009-01-12

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.
0
Comment
Question by:zolf
  • 35
  • 21
  • 13
  • +1
71 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16620793
you don't need to remove/add the table.
You need your model to fire the appropriate event when you add data to it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620794
How are you adding the record? Are you saying you want the actual stored data in the db to change?
0
 

Author Comment

by:zolf
ID: 16620816
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);
          
    }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:zolf
ID: 16620824

i commented the add and remove method.but did not help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620825
After

model.addRow(..

call

model.fireTableDataChanged();
0
 

Author Comment

by:zolf
ID: 16620830


when i add a new data to the db,i want it to show in my Jtable
0
 
LVL 92

Expert Comment

by:objects
ID: 16620840
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

0
 
LVL 92

Expert Comment

by:objects
ID: 16620843
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620846
>>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
0
 

Author Comment

by:zolf
ID: 16620862

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.

0
 
LVL 92

Expert Comment

by:objects
ID: 16620869
> 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();
    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620873
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 16620874
> 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();
    }
0
 

Author Comment

by:zolf
ID: 16620894

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);
         }  
         
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 16620895
> 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


0
 

Author Comment

by:zolf
ID: 16620901
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();
                                                }
                                    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620902
If you place

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

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

Author Comment

by:zolf
ID: 16620904


ok i have deleted that statement
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620906
You need to call any processing - intensive tasks from a separate thread. You could use a SwingWorker
0
 
LVL 92

Expert Comment

by:objects
ID: 16620908
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?
0
 

Author Comment

by:zolf
ID: 16620909

whcih method
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620912
>>ok i have deleted that statement

That won't make any difference, but it is redundant
0
 
LVL 92

Expert Comment

by:objects
ID: 16620917
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.
0
 

Author Comment

by:zolf
ID: 16620924

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
0
 

Author Comment

by:zolf
ID: 16620926

could you please guide me how to do that
0
 
LVL 92

Expert Comment

by:objects
ID: 16620930
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620931
See my comment at

http:Q_21840490.html#16620902
0
 
LVL 92

Expert Comment

by:objects
ID: 16620937
((DefaultTableModel)(cityTable.getModel())).addRow(........
0
 

Author Comment

by:zolf
ID: 16620956

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);

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620966
So precisely what *do* you see afterwards - an empty table or what?
0
 

Author Comment

by:zolf
ID: 16620977

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.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16620983
No

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

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

Author Comment

by:zolf
ID: 16620987

i only see my old records
0
 
LVL 92

Expert Comment

by:objects
ID: 16620990
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()
                    });
0
 

Author Comment

by:zolf
ID: 16621005
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());
          }
    }
0
 

Author Comment

by:zolf
ID: 16621007
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);
          }
0
 

Author Comment

by:zolf
ID: 16621009
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();
                                                   }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16621011
>>if(srcObj=="update")

should be


if("update".equals(srcObj))
0
 
LVL 92

Expert Comment

by:objects
ID: 16621016
>                     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.
0
 

Author Comment

by:zolf
ID: 16621048
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
0
 
LVL 92

Expert Comment

by:objects
ID: 16621054
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
0
 

Author Comment

by:zolf
ID: 16628009

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

0
 
LVL 92

Expert Comment

by:objects
ID: 16628058
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());
         }  
         
    }
0
 

Author Comment

by:zolf
ID: 16628142

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.
0
 
LVL 92

Expert Comment

by:objects
ID: 16628308
have you tried revalidating the tables container as I suggested earlier?

cityPane.revalidate();
cityPane.repaint();
0
 

Author Comment

by:zolf
ID: 16628484

where do i add this code in my constr??
0
 

Author Comment

by:zolf
ID: 16628493

i mean do i add this bit in my constr or in the reload method
0
 
LVL 92

Expert Comment

by:objects
ID: 16628752
in the relaod() after calling setModel()
0
 

Author Comment

by:zolf
ID: 16628949

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();
    }
0
 

Author Comment

by:zolf
ID: 16628974

in the reload method can i just call its contructor again to build the complete frame from new
0
 
LVL 92

Expert Comment

by:objects
ID: 16628982
> createAndInitTree();

whats that doing? Does it do anything to table?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 120 total points
ID: 16629063
Shouldn't you be calling createAndInitTree() first - since the data come from the tree and then get put in the table?
0
 

Author Comment

by:zolf
ID: 16629085

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
0
 

Author Comment

by:zolf
ID: 16629106

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
0
 
LVL 92

Expert Comment

by:objects
ID: 16629113
> 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.
0
 

Author Comment

by:zolf
ID: 16629142

ok
0
 

Author Comment

by:zolf
ID: 16629198


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());
         }  
         
    }
0
 

Author Comment

by:zolf
ID: 16629340

please help
0
 

Author Comment

by:zolf
ID: 16636413

somewhere i read that the connection should be closed or else the table will not refresh.does that matter.??
0
 
LVL 92

Expert Comment

by:objects
ID: 16636443
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.
0
 

Author Comment

by:zolf
ID: 16636637

please help
0
 

Author Comment

by:zolf
ID: 16636717

could you find why the table does not refresh
0
 

Author Comment

by:zolf
ID: 16638269

can anybody help me to get this thing working
0
 

Author Comment

by:zolf
ID: 16645179

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());
         }  
         
    }
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 16647387
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()...
0
 

Author Comment

by:zolf
ID: 16647463


did you mean cityTable.updateUI();
0
 
LVL 92

Expert Comment

by:objects
ID: 16647701
>          tf.reloadRecord();

check that tf is referencing the frame that contains your table
0
 
LVL 9

Assisted Solution

by:riaancornelius
riaancornelius earned 80 total points
ID: 16647810
>> did you mean cityTable.updateUI();
sorry, yes
0
 

Author Comment

by:zolf
ID: 16655663

how can i check that tf is refering to that frame
0
 
LVL 92

Accepted Solution

by:
objects earned 1800 total points
ID: 16655949
> how can i check that tf is refering to that frame

have a look wherte it gets set.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16673796
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question