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
al PictMenuFrameDemo frame)
{
super("InternalFrame",
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
Dimension deskSize = frame.getDesktop().getSize
();
setSize((int)deskSize.getW
idth() - 20, (int)deskSize.getHeight() - 20);
//Set the window's location.
setLocation(10, 10);
JFParentFrame = frame;
//get database result in to collections
initCollections(frame.getD
BConnectio
n());
testIcon = new ImageIcon(Toolkit.getDefau
ltToolkit(
).createIm
age("image
s/dslamIco
n.png"));
cityIcon = new ImageIcon(Toolkit.getDefau
ltToolkit(
).createIm
age("image
s/cityIcon
.png"));
coIcon = new ImageIcon(Toolkit.getDefau
ltToolkit(
).createIm
age("image
s/coIcon.p
ng"));
dslamIcon = new ImageIcon(Toolkit.getDefau
ltToolkit(
).createIm
age("image
s/dslamIco
n.png"));
linecardIcon = new ImageIcon(Toolkit.getDefau
ltToolkit(
).createIm
age("image
s/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_SCROL
LBAR_AS_NE
EDED,
JScrollPane.HORIZONTAL_SCR
OLLBAR_NEV
ER);
//create tab panel
tabPane = new JTabbedPane();
cityPane = new JPanel(new BorderLayout());
createCityTable();
jspCity = new JScrollPane(cityTable, JScrollPane.VERTICAL_SCROL
LBAR_AS_NE
EDED,
JScrollPane.HORIZONTAL_SCR
OLLBAR_NEV
ER);
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
LBAR_AS_NE
EDED,
JScrollPane.HORIZONTAL_SCR
OLLBAR_NEV
ER);
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
LBAR_AS_NE
EDED,
JScrollPane.HORIZONTAL_SCR
OLLBAR_NEV
ER);
dslamPane.add(jspDslam, BorderLayout.CENTER);
tabPane.addTab(" Dslam ", dslamIcon, dslamPane);
JPanel linecardPane = new JPanel(new BorderLayout());
createlinecardTable();
JScrollPane jspLinecard = new JScrollPane(linecardTable,
JScrollPane.VERTICAL_SCROL
LBAR_AS_NE
EDED,
JScrollPane.HORIZONTAL_SCR
OLLBAR_NEV
ER);
linecardPane.add(jspLineca
rd, BorderLayout.CENTER);
tabPane.addTab(" Linecard ", linecardIcon, linecardPane);
splitPane = new JSplitPane(JSplitPane.HORI
ZONTAL_SPL
IT, jsp, tabPane);
cp.add(splitPane, BorderLayout.CENTER);
addInternalFrameListener(n
ew InternalFrameAdapter() {
public void internalFrameClosing(Inter
nalFrameEv
ent 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(JFParentFr
ame.getDBC
onnection(
));
//javax.swing.JOptionPane.
showMessag
eDialog(nu
ll, "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(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("city "+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());
}
}
Start Free Trial