where
Main Topics
Browse All Topics
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.
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());
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can add it using:
http://javaalmanac.com/egs
Use table.getModel () to get the table-model.
actually in the code above,i create the table in TreeTableInternalFrame(fin
and i am adding into that table from anothe class.
how can i use table.getModel () in that other class to add
te code below is in another class which populates the new record in the db
ActionListener JBActionListener = new ActionListener()
{
public void actionPerformed(ActionEven
{
String srcObj = e.getActionCommand();
if(srcObj=="update")
{
if(RequiredFieldEmpty()==f
{
if(ADDING_STATE == true)
{
updateCityCollection(Owner
// Start Display the new record
int total =0;
total = clsPublicMethods.getMaxNum
if(total != 0)
{
System.out.print("TOTAL 1 "+total);
tf.reloadRecord();
}
private void updateCityCollection(Conne
{
Vector cityCollect = new Vector();
try
{
st = connect.createStatement();
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());
}
}
the problem is the table is not refreshing.
i did a test where the flag is false the first time i run the application.then i make the flag true the second time.the code comes inside the if loop,but the color does not change.what should i do to refresh.
if(flag)
{
System.out.println("INSIDE
cityTable.setForeground(Co
cityTable.repaint();
cityPane.revalidate();
cityPane.repaint();
tabPane.repaint();
tabPane.revalidate();
}
i did a System.out.println () after the call to revalidate () and the row was added properly.
if(flag)
{
System.out.println("INSIDE
cityTable.setForeground(Co
cityTable.repaint();
cityPane.revalidate();
cityPane.repaint();
tabPane.repaint();
tabPane.revalidate();
System.out.println("INSIDE
}
if you look at my code above in my first comment is the class which creates the table for the first time the application opens.then in my 4th comment,the code which i have put is where i am calling that class again to recreate the table.and that is where i create the object tf of my first class.
#what do i need to do to refer to that table or refresh.
i create the table in TreeTableInternalFrame(fin
i am adding into that table from anothe class.
the code below is in another class which populates the new record in the db
ActionListener JBActionListener = new ActionListener()
{
public void actionPerformed(ActionEven
{
String srcObj = e.getActionCommand();
if(srcObj=="update")
{
if(RequiredFieldEmpty()==f
{
if(ADDING_STATE == true)
{
updateCityCollection(Owner
// Start Display the new record
int total =0;
total = clsPublicMethods.getMaxNum
if(total != 0)
{
System.out.print("TOTAL 1 "+total);
tf.reloadRecord(); //object of the class which creates jtable and shows data
}
private void updateCityCollection(Conne
{
Vector cityCollect = new Vector();
try
{
st = connect.createStatement();
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());
}
}
this is the class below where i making an object called tf of class TreeTableInternalFrame(fin
public class Frm_add_edit_city extends JDialog
{
JButton JBUpdate = new JButton(new ImageIcon("images/save.png
JButton JBReset = new JButton("Reset",new ImageIcon("images/reset.pn
JButton JBCancel = new JButton("Cancel",new ImageIcon("images/cancel.p
JLabel JLPic1 = new JLabel();
JLabel JLBanner = new JLabel("Please fill-up all the required fields.");
JLabel JLCEngName = new JLabel("City Name:");
JLabel JLCFarName = new JLabel("City Name:");
JLabel JLCPrefix = new JLabel("City Prefix:");
JTextField JTFCEngName = new JTextField();
JTextField JTFCFarName = new JTextField();
JTextField JTFCPrefix = new JTextField();
Dimension screen = Toolkit.getDefaultToolkit(
Statement st;
boolean ADDING_STATE;
Connection connect;
Statement stAEC;
ResultSet rsAEC;
PictMenuFrameDemo OwnerForm;
TreeTableInternalFrame tf;
public Frm_add_edit_city(boolean ADD_STATE,PictMenuFrameDem
{
super(OwnerForm,true);
tf = new TreeTableInternalFrame(Own
////cnAEC = srcCN;
ADDING_STATE = ADD_STATE;
//JCBCountry = clsPublicMethods.fillCombo
JTFCEngName.setEditable(tr
JTFCFarName.setEnabled(tru
if(ADD_STATE==true)
{
JLPic1.setIcon(new ImageIcon("images/bNew.png
setTitle("Add Newwww City");
JBUpdate.setText("Update")
}
else
{
}
JPanel JPContainer = new JPanel();
JPContainer.setLayout(null
//-- Add the JLPic1
JLPic1.setBounds(5,5,32,32
JPContainer.add(JLPic1);
//-- Add the JLBanner
JLBanner.setBounds(55,5,26
JLBanner.setFont(new Font("Dialog",Font.PLAIN,1
JPContainer.add(JLBanner);
//******************** Start adding of input field
//-- Add Id Input Field
JLCEngName.setBounds(5,50,
JLCEngName.setFont(new Font("Dialog",Font.PLAIN,1
JTFCEngName.setBounds(110,
JTFCEngName.setFont(new Font("Dialog",Font.PLAIN,1
JPContainer.add(JLCEngName
JPContainer.add(JTFCEngNam
//-- Add Name Input Field
JLCFarName.setBounds(5,72,
JLCFarName.setFont(new Font("Dialog",Font.PLAIN,1
JTFCFarName.setBounds(110,
JTFCFarName.setFont(new Font("Dialog",Font.PLAIN,1
JPContainer.add(JLCFarName
JPContainer.add(JTFCFarNam
//-- Add Contact Name Input Field
JLCPrefix.setBounds(5,94,1
JLCPrefix.setFont(new Font("Dialog",Font.PLAIN,1
JTFCPrefix.setBounds(110,9
JTFCPrefix.setFont(new Font("Dialog",Font.PLAIN,1
JPContainer.add(JLCPrefix)
JPContainer.add(JTFCPrefix
//******************** End adding of input field
//-- Add the JBUpdate 5,318,105,25
JBUpdate.setBounds(5,135,1
JBUpdate.setFont(new Font("Dialog", Font.PLAIN, 12));
JBUpdate.setMnemonic(KeyEv
JBUpdate.addActionListener
JBUpdate.setActionCommand(
JPContainer.add(JBUpdate);
//-- Add the JBReset
JBReset.setBounds(112,135,
JBReset.setFont(new Font("Dialog", Font.PLAIN, 12));
JBReset.setMnemonic(KeyEve
JBReset.addActionListener(
JBReset.setActionCommand("
JPContainer.add(JBReset);
//-- Add the JBCancel
JBCancel.setBounds(212,135
JBCancel.setFont(new Font("Dialog", Font.PLAIN, 12));
JBCancel.setMnemonic(KeyEv
JBCancel.addActionListener
JBCancel.setActionCommand(
JPContainer.add(JBCancel);
getContentPane().add(JPCon
setSize(325,200);
setResizable(false);
setLocation((screen.width - 325)/2,((screen.height-383
}
private void clearFields()
{
JTFCEngName.setText("");
JTFCFarName.setText("");
JTFCPrefix.setText("");
}
private boolean RequiredFieldEmpty()
{
if(JTFCEngName.getText().e
{
JOptionPane.showMessageDia
JTFCEngName.requestFocus()
return true;
}else
{
return false;
}
}
private void updateCityCollection(Conne
{
Vector cityCollect = new Vector();
try
{
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);
//connect.close();
}
catch (SQLException sqle)
{
System.err.println("Can't get city collection from database : " + sqle.getMessage());
}
}
ActionListener JBActionListener = new ActionListener()
{
public void actionPerformed(ActionEven
{
String srcObj = e.getActionCommand();
if(srcObj=="update")
{
if(RequiredFieldEmpty()==f
{
if(ADDING_STATE == true)
{
updateCityCollection(Owner
// Start Display the new record
int total =0;
total = clsPublicMethods.getMaxNum
if(total != 0)
{
System.out.print("TOTAL 1 "+total);
tf.reloadRecord();
}
else
{
//FrmCustomer.reloadRecord
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();
}
}
else
{
//Update
}
}
}
else if(srcObj=="reset")
{
clearFields();
}
else if(srcObj=="cancel")
{
dispose();
}
}
};
}
> tf = new TreeTableInternalFrame(Own
thats your problem.
it creates a new frame so it doesn't reference the one you want to update.
it instead updates a table in a different frame (that is not ever made visible).
you need to change that to reference the actual framr you want to update. Looks like you'll need to pass the frame in the constructor to do that.
i have done what you mentioned
public Frm_add_edit_city(boolean ADD_STATE,PictMenuFrameDem
{
super(OwnerForm,true);
//tf = new TreeTableInternalFrame(Own
tf = tableFrame;
but then the above class is called in class TreeTableInternalFrame like this,what do i put in its third argument
private JToolBar createToolbar()
{
final JToolBar tb = new JToolBar();
//.. create tool bar buttons
JButton b1 = new JButton("Add City", testIcon);
b1.setFocusPainted(false);
b1.setToolTipText("Test toolbar button : icon + text");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
{
Connection cnCus;
JDialog JDAdd = new Frm_add_edit_city(true,JFP
JDAdd.setModal(false);
JDAdd.show();
}
});
tb.add(b1);
the class TreeInternalFrame is a JInternalFrame.in this class i have a button which when clicked opens a Jdialog.the code is this
public void actionPerformed(ActionEven
{
Connection cnCus;
JDialog JDAdd = new Frm_add_edit_city(true,JFP
JDAdd.setModal(false);
JDAdd.show();
}
now we have changed the JDialog class i.e. Frm_add_edit_city to have an instance of TreeInternalFrame
JDialog JDAdd = new Frm_add_edit_city(true,JFP
> JDialog JDAdd = new Frm_add_edit_city(true,JFP
thats the same as what you are doing before. That results in you updating the newly created frame instead of the existing one.
If that code is inside the frame that you want to update then pass 'this'
JDialog JDAdd = new Frm_add_edit_city(true,JFP
Business Accounts
Answer for Membership
by: mayankeaglePosted on 2006-05-09 at 23:17:04ID: 16645776
Maybe you need to call fireTableModelChanged ()