This is my code, i want to know by adding or modifying what can i have my code refresh the JTable everytime its accessed.
package library2;
import java.awt.Dimens ion;
import java.awt.FlowLa yout;
import java.awt.GridLa yout;
import java.awt.Image;
import java.awt.event. ActionEvent;
import java.awt.event. ActionListener;
import java.sql.Connec tion;
import java.sql.Driver Manager;
import java.sql.Result Set;
import java.sql.Result SetMetaData;
import java.sql.Statem ent;
import java.util.Vecto r;
import javax.swing.JBu tton;
import javax.swing.JFr ame;
import javax.swing.JLa bel;
import javax.swing.JPa nel;
import javax.swing.JSc rollPane;
import javax.swing.JTa ble;
import javax.swing.eve nt.ListSelectio nEvent;
import javax.swing.eve nt.TableModelEv ent;
import javax.swing.eve nt.TableModelLi stener;
import javax.swing.tab le.DefaultTable Model;
import javax.swing.tab le.TableColumn;
public class home extends JFrame {
protected static final ListSelectionEv ent ListSelectionEv ent = null;
static JFrame mainfram = new JFrame("welcome to library");
static JFrame f2 = new JFrame("LIST OF THE BOOKS");
static JButton b1 = new JButton("Add A New Book");
static JButton b2 = new JButton("Modify The Book Details");
static JButton b3 = new JButton("List Of Available Books");
static JButton lihp = new JButton("HOMEPA GE");
static JLabel l1 = new JLabel(
".............W ELCOME TO LIBRARY........ ......", JLabel.CENTER);
static JLabel l2 = new JLabel("HAVE A GREAT DAY :)))", JLabel.CENTER);
static JPanel p1 = new JPanel(new GridLayout(5, 3, 40, 40));
public home() {
p1.setVisible(t rue);
b1.setSize(50, 100);
mainfram.setSiz e(800, 400);
mainfram.add(p1 );
mainfram.setVis ible(true);
// f2.add(lihp);
}
public static void main(String args[]) {
{
b1.addActionLis tener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
mainfram.setVis ible(false);
mainfram.setDef aultCloseOperat ion(JFrame.EXIT _ON_CLOSE);
Addingbook ab = new Addingbook();
// ab.setVisible(t rue);
// ab.setSize(800, 400);
}
});
b2.addActionLis tener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
mainfram.setVis ible(false);
mainfram.setDef aultCloseOperat ion(JFrame.EXIT _ON_CLOSE);
listing li = new listing();
// li.setVisible(t rue);
// li.setSize(800, 400);
}
});
b3.addActionLis tener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
Vector columnNames = new Vector();
Vector data = new Vector();
data.clear();
JPanel p = new JPanel(); /* (new GridLayout(2,1, 10,10) */
p.setBounds(0, 0, 400, 300);
JLabel l = new JLabel();
try {
// Connection con =
// DriverManager.g etConnection("J dbc:Odbc:test") ;
String sql = "select *from books";
String cs = "jdbc:mysql ://localhost:3306/test";
String user = "root";
String pwd = "thbs123!";
Class.forName(" com.mysql.jdbc. Driver").newIns tance();
Connection con = DriverManager.g etConnection(cs , user,
pwd);
Statement stmt = con.createState ment();
ResultSet rs = stmt.executeQue ry(sql);
ResultSetMetaDa ta md = rs.getMetaData( );
// md.getServerExe cutions();
int columns = md.getColumnCou nt();
for (int i = 1; i <= columns; i++) {
columnNames.add Element(md.getC olumnName(i));
}
while (rs.next()) {
Vector row = new Vector(columns) ;
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement (row);
}
rs.close();
stmt.close();
} catch (Exception e1) {
System.out.prin tln(e1);
}
JTable table = new JTable(data, columnNames);
//data = getServerExecut ions();
//table.fireTable DataChanged();
// table.getModel( ).setModel();
// table.valueChan ged(ListSelecti onEvent);
//data(set);
// columnNames();
TableColumn col;
for (int i = 0; i < table.getColumn Count(); i++) {
col = table.getColumn Model().getColu mn(i);
col.setMaxWidth (999);
}
JScrollPane scrollPane = new JScrollPane(tab le);
table.setPrefer redScrollableVi ewportSize(new Dimension(750,
275));
p.setVisible(tr ue);
p.add(l);
p.add(scrollPan e);
// p.add(lihp);
f2.add(p);
f2.setSize(800, 400);
f2.setVisible(t rue);
}
});
f2.add(lihp);
lihp.setBounds( 320, 320, 120, 30);
lihp.addActionL istener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
mainfram.setVis ible(true);
f2.setVisible(f alse);
// f2.setDefaultCl oseOperation(EX IT_ON_CLOSE);
}
});
mainfram.setLay out(new FlowLayout());
// mainfram.imageU pdate(Blue, arg1, arg2, arg3, arg4, arg5)
mainfram.setVis ible(true);
mainfram.setSiz e(800, 400);
p1.add(new JLabel(""));
p1.add(l1);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b1);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b2);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b3);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(l2);
p1.add(new JLabel(""));
mainfram.add(p1 );
// f2.setLayout(ne w FlowLayout());
f2.setResizable (false);
}
}
public void setVisible(bool ean b) {
// TODO Auto-generated method stub
}
}
package library2;
import java.awt.Dimens ion;
import java.awt.FlowLa yout;
import java.awt.GridLa yout;
import java.awt.Image;
import java.awt.event. ActionEvent;
import java.awt.event. ActionListener;
import java.sql.Connec tion;
import java.sql.Driver Manager;
import java.sql.Result Set;
import java.sql.Result SetMetaData;
import java.sql.Statem ent;
import java.util.Vecto r;
import javax.swing.JBu tton;
import javax.swing.JFr ame;
import javax.swing.JLa bel;
import javax.swing.JPa nel;
import javax.swing.JSc rollPane;
import javax.swing.JTa ble;
import javax.swing.eve nt.ListSelectio nEvent;
import javax.swing.eve nt.TableModelEv ent;
import javax.swing.eve nt.TableModelLi stener;
import javax.swing.tab le.DefaultTable Model;
import javax.swing.tab le.TableColumn;
public class home extends JFrame {
protected static final ListSelectionEv ent ListSelectionEv ent = null;
static JFrame mainfram = new JFrame("welcome to library");
static JFrame f2 = new JFrame("LIST OF THE BOOKS");
static JButton b1 = new JButton("Add A New Book");
static JButton b2 = new JButton("Modify The Book Details");
static JButton b3 = new JButton("List Of Available Books");
static JButton lihp = new JButton("HOMEPA GE");
static JLabel l1 = new JLabel(
".............W ELCOME TO LIBRARY........ ......", JLabel.CENTER);
static JLabel l2 = new JLabel("HAVE A GREAT DAY :)))", JLabel.CENTER);
static JPanel p1 = new JPanel(new GridLayout(5, 3, 40, 40));
public home() {
p1.setVisible(t rue);
b1.setSize(50, 100);
mainfram.setSiz e(800, 400);
mainfram.add(p1 );
mainfram.setVis ible(true);
// f2.add(lihp);
}
public static void main(String args[]) {
{
b1.addActionLis tener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
mainfram.setVis ible(false);
mainfram.setDef aultCloseOperat ion(JFrame.EXIT _ON_CLOSE);
Addingbook ab = new Addingbook();
// ab.setVisible(t rue);
// ab.setSize(800, 400);
}
});
b2.addActionLis tener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
mainfram.setVis ible(false);
mainfram.setDef aultCloseOperat ion(JFrame.EXIT _ON_CLOSE);
listing li = new listing();
// li.setVisible(t rue);
// li.setSize(800, 400);
}
});
b3.addActionLis tener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
Vector columnNames = new Vector();
Vector data = new Vector();
data.clear();
JPanel p = new JPanel(); /* (new GridLayout(2,1, 10,10) */
p.setBounds(0, 0, 400, 300);
JLabel l = new JLabel();
try {
// Connection con =
// DriverManager.g etConnection("J dbc:Odbc:test") ;
String sql = "select *from books";
String cs = "jdbc:mysql ://localhost:3306/test";
String user = "root";
String pwd = "thbs123!";
Class.forName(" com.mysql.jdbc. Driver").newIns tance();
Connection con = DriverManager.g etConnection(cs , user,
pwd);
Statement stmt = con.createState ment();
ResultSet rs = stmt.executeQue ry(sql);
ResultSetMetaDa ta md = rs.getMetaData( );
// md.getServerExe cutions();
int columns = md.getColumnCou nt();
for (int i = 1; i <= columns; i++) {
columnNames.add Element(md.getC olumnName(i));
}
while (rs.next()) {
Vector row = new Vector(columns) ;
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement (row);
}
rs.close();
stmt.close();
} catch (Exception e1) {
System.out.prin tln(e1);
}
JTable table = new JTable(data, columnNames);
//data = getServerExecut ions();
//table.fireTable DataChanged();
// table.getModel( ).setModel();
// table.valueChan ged(ListSelecti onEvent);
//data(set);
// columnNames();
TableColumn col;
for (int i = 0; i < table.getColumn Count(); i++) {
col = table.getColumn Model().getColu mn(i);
col.setMaxWidth (999);
}
JScrollPane scrollPane = new JScrollPane(tab le);
table.setPrefer redScrollableVi ewportSize(new Dimension(750,
275));
p.setVisible(tr ue);
p.add(l);
p.add(scrollPan e);
// p.add(lihp);
f2.add(p);
f2.setSize(800, 400);
f2.setVisible(t rue);
}
});
f2.add(lihp);
lihp.setBounds( 320, 320, 120, 30);
lihp.addActionL istener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {
mainfram.setVis ible(true);
f2.setVisible(f alse);
// f2.setDefaultCl oseOperation(EX IT_ON_CLOSE);
}
});
mainfram.setLay out(new FlowLayout());
// mainfram.imageU pdate(Blue, arg1, arg2, arg3, arg4, arg5)
mainfram.setVis ible(true);
mainfram.setSiz e(800, 400);
p1.add(new JLabel(""));
p1.add(l1);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b1);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b2);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b3);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(l2);
p1.add(new JLabel(""));
mainfram.add(p1 );
// f2.setLayout(ne w FlowLayout());
f2.setResizable (false);
}
}
public void setVisible(bool ean b) {
// TODO Auto-generated method stub
}
}
Comment