package tablerefresh;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.awt.event.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JComboBox;
//import richwood.firstChild;
import javax.swing.table.DefaultTableModel;
public class itemMaster extends JFrame
implements ActionListener, ListSelectionListener
{
private Connection con;
private Statement st;
private ResultSet rs;
JPanel mainPane,Pane1,Pane2,Pane3;
JLabel rich,title1,title2,icon,date,current,itemMaster,itemSpecLabel,itemDesLabel,UOMLabel;
JComboBox UOMCBox;
JTextField itemSpecText,itemDesText;
JButton add,save,delete,exit;
JTable myTable;
JScrollPane myPane;
ResultsModel myModel;
public itemMaster() {
super("Item Master");
setLocation(130,60);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
initComponents();
connectToDB();
}
private void initComponents()
{
try {
con = DriverManager.getConnection("jdbc:mysql://system22/richwoods?user=root&password=jrt");
st = con.createStatement();
mainPane = new JPanel(new BorderLayout());
setContentPane(mainPane);
Pane2 = new JPanel();
GridBagLayout gb2 = new GridBagLayout();
GridBagConstraints gbc2 = new GridBagConstraints();
Pane2.setLayout(gb2);
itemSpecLabel = new JLabel("Item Spec ID");
//gbc2.insets=new Insets(0,100,10,0);
//gbc2.weightx=1.0;
gbc2.fill = GridBagConstraints.BOTH;
gbc2.gridwidth = GridBagConstraints.RELATIVE;
gbc2.gridx = 0;
gbc2.gridy = 0;
Pane2.add(itemSpecLabel, gbc2);
itemSpecText = new JTextField(10);
gbc2.gridwidth = GridBagConstraints.REMAINDER;
gbc2.insets = new Insets(0, 0, 10, 100);
gbc2.gridx = 1;
gbc2.gridy = 0;
Pane2.add(itemSpecText, gbc2);
itemDesLabel = new JLabel("Item Description");
gbc2.gridwidth = GridBagConstraints.RELATIVE;
//gbc2.insets=new Insets(10,0,0,0);
gbc2.gridx = 0;
gbc2.gridy = 1;
Pane2.add(itemDesLabel, gbc2);
itemDesText = new JTextField(10);
gbc2.gridwidth = GridBagConstraints.REMAINDER;
//gbc2.insets=new Insets(0,10,0,0);
gbc2.gridx = 1;
gbc2.gridy = 1;
Pane2.add(itemDesText, gbc2);
UOMLabel = new JLabel("UOM");
gbc2.gridwidth = GridBagConstraints.RELATIVE;
gbc2.gridx = 0;
gbc2.gridy = 2;
Pane2.add(UOMLabel, gbc2);
UOMCBox = new JComboBox();
gbc2.gridwidth = GridBagConstraints.REMAINDER;
UOMCBox.setPreferredSize(new Dimension(20, 20));
gbc2.gridx = 1;
gbc2.gridy = 2;
UOMCBox.setEditable(true);
Pane2.add(UOMCBox, gbc2);
myModel = new ResultsModel();
myTable = new JTable(myModel);
myTable.setModel(myModel);
rs = st.executeQuery("select * from mast_item");
myModel.setResultSet(rs);
myTable.setPreferredScrollableViewportSize(new Dimension(500, 150));
myTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
myPane = new JScrollPane(myTable);
myTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
}
}
});
ListSelectionModel listMod = myTable.getSelectionModel();
listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listMod.addListSelectionListener(this);
gbc2.insets = new Insets(30, 0, 0, 0);
// gbc2.weightx=1.0;
//gbc2.ipady=30;
gbc2.gridx = 0;
gbc2.gridy = 4;
//gbc2.ipadx=250;
//gbc2.ipady=100;
Pane2.add(myPane, gbc2);
mainPane.add(Pane2, BorderLayout.CENTER);
Pane3 = new JPanel();
Pane3.setBackground(new java.awt.Color(210, 182, 138));
GridBagLayout g1 = new GridBagLayout();
GridBagConstraints gbc1 = new GridBagConstraints();
Pane3.setLayout(g1);
add = new JButton("NEW");
gbc1.insets = new Insets(20, 5, 20, 5);
gbc1.gridx = 0;
gbc1.gridy = 0;
add.setActionCommand("add");
add.addActionListener(this);
Pane3.add(add, gbc1);
save = new JButton("SAVE");
gbc1.gridx = 1;
gbc1.gridy = 0;
save.setEnabled(false);
save.setActionCommand("save");
save.addActionListener(this);
Pane3.add(save, gbc1);
delete = new JButton("DELETE");
gbc1.gridx = 2;
gbc1.gridy = 0;
delete.setActionCommand("delete");
delete.addActionListener(this);
Pane3.add(delete, gbc1);
exit = new JButton("EXIT");
gbc1.gridx = 3;
gbc1.gridy = 0;
exit.addActionListener(this);
exit.setActionCommand("exit");
Pane3.add(exit, gbc1);
mainPane.add(Pane3, BorderLayout.SOUTH);
} catch (SQLException ex) {
Logger.getLogger(itemMaster.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void connectToDB() {
try {
con = DriverManager.getConnection("jdbc:mysql://SYSTEM22/richwoods?user=root&password=jrt");
st = con.createStatement();
} catch (SQLException connectException) {
System.out.println(connectException.getMessage());
System.out.println(connectException.getSQLState());
System.out.println(connectException.getErrorCode());
System.exit(1);
}
}
public void actionPerformed(ActionEvent e) {
if ("add".equals(e.getActionCommand()))
{
myModel.setRowCount( myModel.getRowCount() + 1 );
int row = myTable.getRowCount() - 1;
myTable.changeSelection(row, row, false, false);
myTable.requestFocusInWindow();
save.setEnabled(true);
add.setEnabled(false);
save.setActionCommand("save");
save.setText("save") ;
itemSpecText.requestFocus();
}
else if ("save".equals(e.getActionCommand()))
{
try {
//System.out.println(save.getLabel());
//String password = new String (passText.getPassword());
con = DriverManager.getConnection("jdbc:mysql://system22/richwoods?user=root&password=jrt");
st = con.createStatement();
String query = "select * from mast_item";
ResultSet rs = st.executeQuery(query);
int i = st.executeUpdate("INSERT INTO mast_item VALUES('"
+ itemSpecText.getText() + "', " + "'"
+ itemDesText.getText() + "', " + "'"
+ (String)UOMCBox.getSelectedItem() + "') " );
JOptionPane.showMessageDialog (this, "Record Saved");
itemSpecText.setText(""); itemDesText.setText(""); UOMCBox.setSelectedItem("");
save.setEnabled(false);
add.setEnabled(true);
myModel.fireTableDataChanged();
rs.close();
st.close();
}catch (SQLException insertException) {
System.out.println(insertException);
}
}
else if("exit".equals(e.getActionCommand())){
setVisible(false);
}
else if ("update".equals(e.getActionCommand())) {
try {
//String password = new String (passText.getPassword());
con = DriverManager.getConnection("jdbc:mysql://system22/richwoods?user=root&password=jrt");
st = con.createStatement();
String query = "select * from mast_item";
ResultSet rs = st.executeQuery(query);
st.execute("UPDATE mast_item "
//+"SET PartyID='" + idText.getText() + "', "
+ "SET itemdesc='" + itemDesText.getText() + "', "
+ "uom='" + UOMCBox.getSelectedItem()+ "' "
+ "WHERE itemid='" + itemSpecText.getText()+ "'");
JOptionPane.showMessageDialog (this, "Record Updated");
itemSpecText.setText("");
itemDesText.setText("");
UOMCBox.setSelectedItem("");
save.setEnabled(false);
add.setEnabled(true);
rs.close();
st.close();
}catch (SQLException insertException) {
System.out.println(insertException);
}
}
}
public void valueChanged(ListSelectionEvent e) {
int maxRows = 0;
int[] selRows;
Object value = null;
if (!e.getValueIsAdjusting()) {
selRows = myTable.getSelectedRows();
selRows = myTable.getSelectedRows();
// get Table data
ResultsModel tm = (ResultsModel) myTable.getModel();
String t1= (String) tm.getValueAt(selRows[0],0);
String t2 =(String) tm.getValueAt(selRows[0],1);
String t3= (String) tm.getValueAt(selRows[0],2);
itemSpecText.setText(t1);
itemDesText.setText(t2);
UOMCBox.setSelectedItem(t3);
save.setActionCommand("update");
save.setText("Update") ;
save.setEnabled(true);
add.setEnabled(false);
}
}
public static void main(String arg[])
{
itemMaster im=new itemMaster();
im.setVisible(true);
}
}
In this Frame i've a Jtable that displays the data from the database, when i click the data in the table, the data will be displayed in the textfield and their i made some changes and clicked the update button. the modified value will be refreshed. but what i need is after clicking the update button the value displayed in the JTable ..
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.table.*;
public class ResultsModel extends DefaultTableModel {
public void setResultSet(ResultSet results) {
try {
ResultSetMetaData metadata = results.getMetaData();
int columns = metadata.getColumnCount();
// Get the column names and set header names
for(int i = 0; i < columns; i++) {
addColumn(metadata.getColumnLabel(i+1));
}
// Get all rows
while(results.next()) {
String[] rowData = new String[columns]; // Create array to hold the data
for(int i = 0; i < columns; i++) { // For each column
rowData[ i ] = results.getString(i+1); // retrieve the data item
}
addRow(rowData); // Add a row
}
fireTableChanged(null); // Signal the table there is new model data
} catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
}
}
Thanks in advance.. plz help me. guys