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.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class home extends JFrame {
protected static final ListSelectionEvent ListSelectionEvent = 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("HOMEPAGE");
static JLabel l1 = new JLabel(
".............WELCOME 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(true);
b1.setSize(50, 100);
mainfram.setSize(800, 400);
mainfram.add(p1);
mainfram.setVisible(true);
// f2.add(lihp);
}
public static void main(String args[]) {
{
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mainfram.setVisible(false);
mainfram.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
Addingbook ab = new Addingbook();
// ab.setVisible(true);
// ab.setSize(800, 400);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mainfram.setVisible(false);
mainfram.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
listing li = new listing();
// li.setVisible(true);
// li.setSize(800, 400);
}
});
b3.addActionListener(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.getConnection("Jdbc: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").newInstance ();
Connection con = DriverManager.getConnection(cs, user,
pwd);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
// md.getServerExecutions();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(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.println(e1);
}
JTable table = new JTable(data, columnNames);
//data = getServerExecutions();
//table.fireTableDataChanged();
// table.getModel().setModel();
// table.valueChanged(ListSelectionEvent);
//data(set);
// columnNames();
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(999);
}
JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(750,
275));
p.setVisible(true);
p.add(l);
p.add(scrollPane);
// p.add(lihp);
f2.add(p);
f2.setSize(800, 400);
f2.setVisible(true);
}
});
f2.add(lihp);
lihp.setBounds(320, 320, 120, 30);
lihp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mainfram.setVisible(true);
f2.setVisible(false);
// f2.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
});
mainfram.setLayout(new FlowLayout());
// mainfram.imageUpdate(Blue, arg1, arg2, arg3, arg4, arg5)
mainfram.setVisible(true);
mainfram.setSize(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(new FlowLayout());
f2.setResizable(false);
}
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
}