Zolf
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(JFParentFr
jspCity.getViewport().remo
createCityTable();
jspCity.getViewport().add(
cityPane.repaint();
}
this is the method i call.but the table does not reload.
please help.
How are you adding the record? Are you saying you want the actual stored data in the db to change?
ASKER
private void createCityTable()
{
String[] columnNames = {"City ID",
"City Farsi Name",
"City English Name",
"Prefix"};
model = new DefaultTableModel(columnNa mes, 0);
cityTable = new JTable(model);
cityTable.getColumnModel() .getColumn (0).setMax Width(60);
cityTable.getColumnModel() .getColumn (0).setPre ferredWidt h(60);
cityTable.getColumnModel() .getColumn (3).setMax Width(200) ;
cityTable.getColumnModel() .getColumn (3).setPre ferredWidt h(200);
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci tyCollect. get(i);
CityRow row = (CityRow)node.getUserObjec t();
System.out.println(row);
model.addRow(new Object[] {row.getCityId(),
row.getCityFarsiName(),
row.getCityEngName(),
row.getPrefix()
});
}
cityTable.setEnabled(false );
}
{
String[] columnNames = {"City ID",
"City Farsi Name",
"City English Name",
"Prefix"};
model = new DefaultTableModel(columnNa
cityTable = new JTable(model);
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci
CityRow row = (CityRow)node.getUserObjec
System.out.println(row);
model.addRow(new Object[] {row.getCityId(),
row.getCityFarsiName(),
row.getCityEngName(),
row.getPrefix()
});
}
cityTable.setEnabled(false
}
ASKER
i commented the add and remove method.but did not help
After
model.addRow(..
call
model.fireTableDataChanged ();
model.addRow(..
call
model.fireTableDataChanged
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(columnNa mes, 0);
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci tyCollect. get(i);
CityRow row = (CityRow)node.getUserObjec t();
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).setMax Width(60);
cityTable.getColumnModel() .getColumn (0).setPre ferredWidt h(60);
cityTable.getColumnModel() .getColumn (3).setMax Width(200) ;
cityTable.getColumnModel() .getColumn (3).setPre ferredWidt h(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
private void createCityTable()
{
String[] columnNames = {"City ID",
"City Farsi Name",
"City English Name",
"Prefix"};
model = new DefaultTableModel(columnNa
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci
CityRow row = (CityRow)node.getUserObjec
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()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
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
> when i add a new data to the db,i want it to show in my Jtable
If you know what was added then just add the new data, and fire event
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/DefaultTableModel.html#addRow(java.lang.Object[]), or
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/AbstractTableModel.html#fireTableRowsInserted(int,%20int)
If you know what was added then just add the new data, and fire event
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/DefaultTableModel.html#addRow(java.lang.Object[]), or
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/AbstractTableModel.html#fireTableRowsInserted(int,%20int)
>>cityTable.setEnabled(fal se);
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
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
ASKER
i add model.fireTableDataChanged
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(JFParentFr ame.getDBC onnection( ));
createCityTable();
}
so whats happening?
did you also remove the code to remove/add the tabel? you need to
public void reloadRecord()
{
initCollections(JFParentFr
createCityTable();
}
You've now changed
>>
public void reloadRecord()
{
initCollections(JFParentFr ame.getDBC onnection( ));
jspCity.getViewport().remo ve(cityTab le);
createCityTable();
jspCity.getViewport().add( cityTable) ;
cityPane.repaint();
}
>>
to
public void reloadRecord()
{
initCollections(JFParentFr ame.getDBC onnection( ));
createCityTable();
}
have you?
>>
public void reloadRecord()
{
initCollections(JFParentFr
jspCity.getViewport().remo
createCityTable();
jspCity.getViewport().add(
cityPane.repaint();
}
>>
to
public void reloadRecord()
{
initCollections(JFParentFr
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(JFParentFr ame.getDBC onnection( ));
createCityTable();
}
did you also remove the code to ermove/add table u need to
public void reloadRecord()
{
initCollections(JFParentFr
createCityTable();
}
ASKER
yes i changed the code to this
also inserted
public void reloadRecord()
{
initCollections(JFParentFr
createCityTable();
}
and added model.fireTableDataChanged
private void createCityTable()
{
String[] columnNames = {"City ID",
"City Farsi Name",
"City English Name",
"Prefix"};
model = new DefaultTableModel(columnNa
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci
CityRow row = (CityRow)node.getUserObjec
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()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
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
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
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()==f alse)
{
if(ADDING_STATE == true)
{
initCollections(OwnerForm. getDBConne ction());
// Start Display the new record
int total =0;
total = clsPublicMethods.getMaxNum ("SELECT * FROM city ORDER BY city_id ASC",OwnerForm.getDBConnec tion(),"ci ty_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.showMessageDia log(null," New record has been successfully added.","Naparansoft Inventory System",JOptionPane.INFORM ATION_MESS AGE);
String ObjButtons[] = {"Yes","No"};
int PromptResult = JOptionPane.showOptionDial og(null,"D o you want add another record?","Naparansoft Inventory System",JOptionPane.DEFAUL T_OPTION,J OptionPane .QUESTION_ MESSAGE,nu ll,ObjButt ons,ObjBut tons[0]);
if(PromptResult==0)
{
clearFields();
JTFCEngName.requestFocus(t rue);
}else
{
dispose();
}
}
if(srcObj=="update")
{
if(RequiredFieldEmpty()==f
{
if(ADDING_STATE == true)
{
initCollections(OwnerForm.
// Start Display the new record
int total =0;
total = clsPublicMethods.getMaxNum
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.showMessageDia
String ObjButtons[] = {"Yes","No"};
int PromptResult = JOptionPane.showOptionDial
if(PromptResult==0)
{
clearFields();
JTFCEngName.requestFocus(t
}else
{
dispose();
}
}
If you place
javax.swing.JOptionPane.sh owMessageD ialog(null , "Called");
at the end of that method, what do you see?
javax.swing.JOptionPane.sh
at the end of that method, what do you see?
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(columnNa mes, 0);
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci tyCollect. get(i);
CityRow row = (CityRow)node.getUserObjec t();
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).setMax Width(60);
cityTable.getColumnModel() .getColumn (0).setPre ferredWidt h(60);
cityTable.getColumnModel() .getColumn (3).setMax Width(200) ;
cityTable.getColumnModel() .getColumn (3).setPre ferredWidt h(200);
cityTable.setEnabled(false );
}
why do you reaload the whole table btw, instead of just adding the new row?
private void createCityTable()
{
String[] columnNames = {"City ID",
"City Farsi Name",
"City English Name",
"Prefix"};
model = new DefaultTableModel(columnNa
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci
CityRow row = (CityRow)node.getUserObjec
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()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.setEnabled(false
}
why do you reaload the whole table btw, instead of just adding the new row?
ASKER
whcih method
>>ok i have deleted that statement
That won't make any difference, but it is redundant
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.
That way you wouldn't need to reload the table, which you should avoid doing anywaty.
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
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.
though I'd suggest instead just adding the new row insteqad of reloading the whole table.
((DefaultTableModel)(cityT able.getMo del())).ad dRow(..... ...
ASKER
cehj i added javax.swing.JOptionPane.sh
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_SCROL
JScrollPane.HORIZONTAL_SCR
cityPane.add(jspCity, BorderLayout.CENTER);
tabPane.addTab(" City ", cityIcon, cityPane);
So precisely what *do* you see afterwards - an empty table or what?
ASKER
this is what i did
public void reloadRecord()
{
initCollections(JFParentFr
createCityTable();
javax.swing.JOptionPane.sh
}
then i just see that message dialog and i click ok.nothing special hapens.
No
>>javax.swing.JOptionPane. showMessag eDialog(nu ll, "Called");
can go now - it's served its purpose. I mean what do you see in your table?
>>javax.swing.JOptionPane.
can go now - it's served its purpose. I mean what do you see in your table?
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()
});
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()
});
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());
}
}
private void initCollections(Connection
{
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());
}
}
ASKER
private void updateCityCollection(Conne ction connect)
{
Vector cityCollect = new Vector();
try
{
Statement st = connect.createStatement();
//"INSERT INTO CITY (CITY_ID,CITY_ENG_NAME,CIT Y_FARSI_NA ME,PREFIX) VALUES (city_id_seq.nextval,'"+JT FCEngName. getText()+ "','"+JTFC FarName.ge tText()+"' ,'"+JTFCPr efix.getTe xt()+"')") ;
//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,CIT Y_FARSI_NA ME,PREFIX) VALUES (city_id_seq.nextval,'"+JT FCEngName. getText()+ "','"+JTFC FarName.ge tText()+"' ,'"+JTFCPr efix.getTe xt()+"')";
st.executeUpdate(query);
}
catch (SQLException sqle)
{
System.err.println("Can't get city collection from database : " + sqle.getMessage());
}
}
private void initCollections(Connection connect)
{
updateCityCollection(conne ct);
}
{
Vector cityCollect = new Vector();
try
{
Statement st = connect.createStatement();
//"INSERT INTO CITY (CITY_ID,CITY_ENG_NAME,CIT
//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,CIT
st.executeUpdate(query);
}
catch (SQLException sqle)
{
System.err.println("Can't get city collection from database : " + sqle.getMessage());
}
}
private void initCollections(Connection
{
updateCityCollection(conne
}
ASKER
public void actionPerformed(ActionEven t e)
{
String srcObj = e.getActionCommand();
if(srcObj=="update")
{
if(RequiredFieldEmpty()==f alse)
{
if(ADDING_STATE == true)
{
initCollections(OwnerForm. getDBConne ction());
// Start Display the new record
int total =0;
total = clsPublicMethods.getMaxNum ("SELECT * FROM city ORDER BY city_id ASC",OwnerForm.getDBConnec tion(),"ci ty_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();
}
{
String srcObj = e.getActionCommand();
if(srcObj=="update")
{
if(RequiredFieldEmpty()==f
{
if(ADDING_STATE == true)
{
initCollections(OwnerForm.
// Start Display the new record
int total =0;
total = clsPublicMethods.getMaxNum
if(total != 0)
{
//FrmCustomer.reloadRecord
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) )
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.
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.
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
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
or if the dialog has a reference to the framer it can access the model from there
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(columnNa mes, 0);
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci tyCollect. get(i);
CityRow row = (CityRow)node.getUserObjec t();
System.out.println(row);
model.addRow(new Object[] {row.getCityId(),
row.getCityFarsiName(),
row.getCityEngName(),
row.getPrefix()
});
}
cityTable = new JTable(model);
cityTable.getColumnModel() .getColumn (0).setMax Width(60);
cityTable.getColumnModel() .getColumn (0).setPre ferredWidt h(60);
cityTable.getColumnModel() .getColumn (3).setMax Width(200) ;
cityTable.getColumnModel() .getColumn (3).setPre ferredWidt h(200);
cityTable.setEnabled(false );
} else {
JOptionPane.showMessageDia log(null, "Clearing table");
cityTable.setModel(new DefaultTableModel());
}
}
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(columnNa
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci
CityRow row = (CityRow)node.getUserObjec
System.out.println(row);
model.addRow(new Object[] {row.getCityId(),
row.getCityFarsiName(),
row.getCityEngName(),
row.getPrefix()
});
}
cityTable = new JTable(model);
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.setEnabled(false
} else {
JOptionPane.showMessageDia
cityTable.setModel(new DefaultTableModel());
}
}
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();
cityPane.revalidate();
cityPane.repaint();
ASKER
where do i add this code in my constr??
ASKER
i mean do i add this bit in my constr or in the reload method
in the relaod() after calling setModel()
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(JFParentFr
//javax.swing.JOptionPane.
createCityTable();
createAndInitTree();
tabPane.repaint();
}
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?
whats that doing? Does it do anything to table?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
then comment out as much other code as possible to minimise possible side effects.
ASKER
ok
ASKER
this is the constr of the class where the table is
public TreeTableInternalFrame(fin
{
super("InternalFrame",
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
Dimension deskSize = frame.getDesktop().getSize
setSize((int)deskSize.getW
//Set the window's location.
setLocation(10, 10);
JFParentFrame = frame;
//get database result in to collections
initCollections(frame.getD
testIcon = new ImageIcon(Toolkit.getDefau
cityIcon = new ImageIcon(Toolkit.getDefau
coIcon = new ImageIcon(Toolkit.getDefau
dslamIcon = new ImageIcon(Toolkit.getDefau
linecardIcon = new ImageIcon(Toolkit.getDefau
//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_SCROL
JScrollPane.HORIZONTAL_SCR
//create tab panel
tabPane = new JTabbedPane();
cityPane = new JPanel(new BorderLayout());
createCityTable();
jspCity = new JScrollPane(cityTable, JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
cityPane.add(jspCity, BorderLayout.CENTER);
tabPane.addTab(" City ", cityIcon, cityPane);
JPanel coPane = new JPanel(new BorderLayout());
createCoTable();
JScrollPane jspCo = new JScrollPane(coTable, JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
coPane.add(jspCo, BorderLayout.CENTER);
tabPane.addTab(" Co ", coIcon, coPane);
JPanel dslamPane = new JPanel(new BorderLayout());
createDslamTable();
JScrollPane jspDslam = new JScrollPane(dslamTable, JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
dslamPane.add(jspDslam, BorderLayout.CENTER);
tabPane.addTab(" Dslam ", dslamIcon, dslamPane);
JPanel linecardPane = new JPanel(new BorderLayout());
createlinecardTable();
JScrollPane jspLinecard = new JScrollPane(linecardTable,
JScrollPane.HORIZONTAL_SCR
linecardPane.add(jspLineca
tabPane.addTab(" Linecard ", linecardIcon, linecardPane);
splitPane = new JSplitPane(JSplitPane.HORI
cp.add(splitPane, BorderLayout.CENTER);
addInternalFrameListener(n
public void internalFrameClosing(Inter
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(JFParentFr
//javax.swing.JOptionPane.
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
if (cityTable==null)
{
String[] columnNames = {"City ID",
"City Farsi Name",
"City English Name",
"Prefix"};
model = new DefaultTableModel(columnNa
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci
CityRow row = (CityRow)node.getUserObjec
System.out.println("city "+row);
model.addRow(new Object[] {row.getCityId(),
row.getCityFarsiName(),
row.getCityEngName(),
row.getPrefix()
});
}
cityTable = new JTable(model);
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.setEnabled(false
}
else
{
JOptionPane.showMessageDia
cityTable.setModel(new DefaultTableModel());
}
}
ASKER
please help
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.
if you can post a small example that I can run that reproduces the prob I'll try it here.
ASKER
please help
ASKER
could you find why the table does not refresh
ASKER
can anybody help me to get this thing working
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(fin
{
super("InternalFrame",
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
Dimension deskSize = frame.getDesktop().getSize
setSize((int)deskSize.getW
//Set the window's location.
setLocation(10, 10);
JFParentFrame = frame;
//get database result in to collections
initCollections(frame.getD
testIcon = new ImageIcon(Toolkit.getDefau
cityIcon = new ImageIcon(Toolkit.getDefau
coIcon = new ImageIcon(Toolkit.getDefau
dslamIcon = new ImageIcon(Toolkit.getDefau
linecardIcon = new ImageIcon(Toolkit.getDefau
//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_SCROL
JScrollPane.HORIZONTAL_SCR
//create tab panel
tabPane = new JTabbedPane();
cityPane = new JPanel(new BorderLayout());
createCityTable();
jspCity = new JScrollPane(cityTable, JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
cityPane.add(jspCity, BorderLayout.CENTER);
tabPane.addTab(" City ", cityIcon, cityPane);
JPanel coPane = new JPanel(new BorderLayout());
createCoTable();
JScrollPane jspCo = new JScrollPane(coTable, JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
coPane.add(jspCo, BorderLayout.CENTER);
tabPane.addTab(" Co ", coIcon, coPane);
JPanel dslamPane = new JPanel(new BorderLayout());
createDslamTable();
JScrollPane jspDslam = new JScrollPane(dslamTable, JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
dslamPane.add(jspDslam, BorderLayout.CENTER);
tabPane.addTab(" Dslam ", dslamIcon, dslamPane);
JPanel linecardPane = new JPanel(new BorderLayout());
createlinecardTable();
JScrollPane jspLinecard = new JScrollPane(linecardTable,
JScrollPane.HORIZONTAL_SCR
linecardPane.add(jspLineca
tabPane.addTab(" Linecard ", linecardIcon, linecardPane);
splitPane = new JSplitPane(JSplitPane.HORI
cp.add(splitPane, BorderLayout.CENTER);
addInternalFrameListener(n
public void internalFrameClosing(Inter
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(JFParentFr
//javax.swing.JOptionPane.
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
if (cityTable==null)
{
String[] columnNames = {"City ID",
"City Farsi Name",
"City English Name",
"Prefix"};
model = new DefaultTableModel(columnNa
for (int i = 0; i < cityCollect.size(); i++)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ci
CityRow row = (CityRow)node.getUserObjec
System.out.println("city "+row);
model.addRow(new Object[] {row.getCityId(),
row.getCityFarsiName(),
row.getCityEngName(),
row.getPrefix()
});
}
cityTable = new JTable(model);
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.getColumnModel()
cityTable.setEnabled(false
}
else
{
JOptionPane.showMessageDia
cityTable.setModel(new DefaultTableModel());
}
}
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()...
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()...
ASKER
did you mean cityTable.updateUI();
> tf.reloadRecord();
check that tf is referencing the frame that contains your table
check that tf is referencing the frame that contains your table
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how can i check that tf is refering to that frame
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
You need your model to fire the appropriate event when you add data to it.