How to refresh the JTable

I have a Form having the JTable which displays records from Database. when i add new record the JTable should show the Newly added record.I don't know how to refresh the JTable. Please help meThank you for your service
[240 byte] By [jofin123a] at [2007-11-26 18:00:17]
# 1
The DefaultTableModel has an addRow(..) method that will add the data to the TableModel and cause the JTable to be repainted to show the new row of data.
camickra at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 2

Dear Sir Thank you for your reply

I have got some idea. and working on it. Now the problem is

There are Two Forms Lets say Form1 and Form2 , now the Form1 is having JTable which displays the database records. The Form2 is a having the JTextFields Where the Users Enters the data to store it in the database.

Now when i click on the OK Button which is in the Form2 , The system should show the Newly entered record in the JTable which is in the Form1.

Please provide me some samples so that i can Understand

Thank you for your service

Cheers

jofin

jofin123a at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 3

I can see two scenarios here:

1)You will first add data and then click on button to focus the second form,which is already open-

It can be done in two steps.

a) When you click on Button in Form2,save all the values in a Vector/arraylist or ant array. Then Onclick of First button open the second Form.

b)With addRow() method update the table.

2)You will first add data and then click on button to open the second form with updated data.

Then you add these details into database and then load data in table from database.

Please let me know which scenario it is?

Sruthi.ma at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thank you sir for your Quick reply

As you said in the 2nd senario.

1) Login to the system

2) I get a HomePageForm with some Data Displayed on the JTable.

3) Click on Button called Add Members

4) It displays Add Member Form(Contains: MemberCode, Name,DateOfBirth,FatherName)

5)Click on Save Button

Now the System should display the record in the HomePageForm and also store it in the database

Please help me sir

I am very new to this

Thank you for your Service

Cheers

Jofin

jofin123a at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 5
This posting shows how to add a new row of data to the table: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=577919
camickra at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 6

I am very sorry for troubling you again and again

I am trying to create a system Visitors Log

For that i need to do the following operation

1) Login to the system

2) I get a HomePageForm with some Data Displayed on the JTable.

3) Click on Button called Add Visitor

4) It displays Add Visitor Form(Contains: Code, Name,From, Intime, Outtime,Reason)

5)Enter the details and Click on Save Button

Now the System should display the record in the JTable which is Placed in the HomePageForm and also store it in the database.

Please give me some sample code and help me.

I have searched for the right content on This But i am not getting it.

Please help me sir

I am very new to this

Thank you for your Service

Cheers

Jofin

jofin123a at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 7
> Please give me some sample code and help me.You where given some sample code. The example uses hard coded data, but its no different if you where to get the data from a form. You would obviously need to add the SQL code as well to update your database.
camickra at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 8

Hi

here i am posting my code :

Please check it and give me Better solution,

Code for Visitors.java

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

import java.sql.*;

public class Visitors extends JFrame implements ActionListener

{

JTable table;

JPanel buttonPanel;

JButton button;

private Connection con;

private Statement st;

Addvisitor av;

public Visitors()

{

Vector columnNames = new Vector();

Vector data = new Vector();

// Create table

// Add table and a Button panel to the frame

buttonPanel = new JPanel();

button = new JButton( "Add Visitor" );

button.addActionListener(this);

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

table = new JTable(7,6);

JPanel pnl2 = new JPanel();

pnl2.add(table);

buttonPanel.add( table );

buttonPanel.add( button );

getContentPane().add(pnl2, BorderLayout.NORTH);

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

try

{

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

String loc = "jdbc:odbc:visitors";

con = DriverManager.getConnection (loc);

st=con.createStatement();

ResultSet rs=st.executeQuery("Select * from Books");

ResultSetMetaData md= rs.getMetaData();

int columns =md.getColumnCount();

String booktblheading[]={"VistorName","IN TIME","OUT TIME","REASON"};

for(int i=1; i<= booktblheading.length;i++)

{

columnNames.addElement(booktblheading[i-1]);

}

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();

st.close();

}

catch (ClassNotFoundException cnf) {

JOptionPane.showMessageDialog (null, "Driver not Loaded...");

System.exit (0);

}

catch (SQLException sqlex) {

JOptionPane.showMessageDialog (null, "Unable to Connect to Database...");

System.exit (0);

}

}

public void actionPerformed (ActionEvent ae)

{

Object obj = ae.getSource();

if (obj == button)

{

av = new Addvisitor();

}

}

public static void main(String[] args)

{

Visitors frame = new Visitors();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setSize(700,500);

frame.setVisible(true);

}

}

Code for Addvisitor.java

/****************************************************************/

/* Addvisitor*/

/* */

/****************************************************************/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* Summary description for Addvisitor

*

*/

public class Addvisitor extends JDialog

{

// Variables declaration

private JLabel lblname;

private JLabel lblintime;

private JLabel lblouttime;

private JLabel lblreason;

private JTextField jTextField1;

private JTextField jTextField2;

private JTextField jTextField3;

private JTextField jTextField4;

private JButton btnsave;

private JButton btncancel;

private JPanel contentPane;

// End of variables declaration

public Addvisitor()

{

initializeComponent();

this.setVisible(true);

}

private void initializeComponent()

{

lblname = new JLabel();

lblintime = new JLabel();

lblouttime = new JLabel();

lblreason = new JLabel();

jTextField1 = new JTextField();

jTextField2 = new JTextField();

jTextField3 = new JTextField();

jTextField4 = new JTextField();

btnsave = new JButton();

btncancel = new JButton();

contentPane = (JPanel)this.getContentPane();

lblname.setText("Visitor name");

lblintime.setText("In Time");

lblouttime.setText("Out Time");

lblreason.setText("Reason");

btnsave.setText("Save");

btnsave.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btnsave_actionPerformed(e);

}

});

btncancel.setText("Cancel");

btncancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btncancel_actionPerformed(e);

}

});

contentPane.setLayout(null);

addComponent(contentPane, lblname, 28,43,74,18);

addComponent(contentPane, lblintime, 30,75,60,20);

addComponent(contentPane, lblouttime, 29,113,60,18);

addComponent(contentPane, lblreason, 30,148,60,16);

addComponent(contentPane, jTextField1, 118,42,155,22);

addComponent(contentPane, jTextField2, 118,75,73,22);

addComponent(contentPane, jTextField3, 118,111,75,22);

addComponent(contentPane, jTextField4, 118,147,160,22);

addComponent(contentPane, btnsave, 138,191,83,28);

addComponent(contentPane, btncancel, 234,190,83,28);

this.setTitle("Addvisitor - extends JDialog");

this.setLocation(new Point(10, 10));

this.setSize(new Dimension(360, 260));

this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

private void btnsave_actionPerformed(ActionEvent e)

{

System.out.println("\nbtnsave_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

private void btncancel_actionPerformed(ActionEvent e)

{

System.out.println("\nbtncancel_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

}

please sir go through my coding and give me a better solution. Because i am working on the for more than one week.

Thank you for your service

Cheers

Jofin

jofin123a at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 9

Hi!

I have a workaround for this.I did not try doing it using Dialog.

First: Set a status and data vector variable in the second form which has the status of the new value added and values entered in the text boxes. Then add a FocusListener to the button of first form which instaciates the second form ,In Focus gained check for the value of status of second form.and when it is 1 then get the vector data and add it to the table .

IMP:I am not adding the details entered to database,If you go thru the method I have written a comment after which you can add ur code to add to databse.

Here are the classes:

Visitor.java:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

import java.sql.*;

public class Visitors extends JFrame implements ActionListener

{

JTable table;

JPanel buttonPanel;

JButton button;

private Connection con;

private Statement st;

Addvisitor av;

public Visitors()

{

Vector columnNames = new Vector();

Vector data = new Vector();

// Create table

// Add table and a Button panel to the frame

buttonPanel = new JPanel();

button = new JButton( "Add Visitor" );

button.addActionListener(this);

button.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent arg0) {

System.out.println("focus gained");

System.out.println("this is:"+Addvisitor.getStatus());

if(Addvisitor.getStatus()==1){

System.out.println("vector is:");

((DefaultTableModel)table.getModel()).addRow(Addvisitor.getRowData());

Addvisitor.setStatus(0);

Addvisitor.setRowData();

}

}

public void focusLost(FocusEvent arg0) {

}

});

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

table = new JTable(7,6);

JPanel pnl2 = new JPanel();

pnl2.add(table);

buttonPanel.add( table );

buttonPanel.add( button );

getContentPane().add(pnl2, BorderLayout.NORTH);

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

try

{

//Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

Class.forName("net.sourceforge.jtds.jdbc.Driver");

//String loc = "jdbc:odbc:visitors";

String loc="jdbc:jtds:sqlserver://192.168.1.20:1433/testDatabase";

con = DriverManager.getConnection (loc,"sa","madmax");

st=con.createStatement();

ResultSet rs=st.executeQuery("Select * from Books");

ResultSetMetaData md= rs.getMetaData();

int columns =md.getColumnCount();

String booktblheading[]={"VistorName","IN TIME","OUT TIME","REASON"};

for(int i=1; i<= booktblheading.length;i++)

{

columnNames.addElement(booktblheading[i-1]);

}

while(rs.next())

{

Vector row = new Vector(columns);

for(int i=1;i<=columns;i++)

{

row.addElement(rs.getObject(i));

}

data.addElement(row);

System.out.println("data is:"+data);

}

((DefaultTableModel)table.getModel()).setDataVector(data,columnNames);

rs.close();

st.close();

}

catch (ClassNotFoundException cnf) {

JOptionPane.showMessageDialog (null, "Driver not Loaded...");

System.exit (0);

}

catch (SQLException sqlex) {

JOptionPane.showMessageDialog (null, "Unable to Connect to Database...");

System.exit (0);

}

}

public void actionPerformed (ActionEvent ae)

{

Object obj = ae.getSource();

if (obj == button)

{

av = new Addvisitor();

}

}

public static void main(String[] args)

{

Visitors frame = new Visitors();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setSize(700,500);

frame.setVisible(true);

}

}

Addvisitor.java

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

import javax.swing.*;

/**

* Summary description for Addvisitor

*

*/

public class Addvisitor extends JDialog

{

// Variables declaration

private JLabel lblname;

private JLabel lblintime;

private JLabel lblouttime;

private JLabel lblreason;

private JTextField jTextField1;

private JTextField jTextField2;

private JTextField jTextField3;

private JTextField jTextField4;

private JButton btnsave;

private JButton btncancel;

private JPanel contentPane;

private static Vector vec;

private static int status=0;

// End of variables declaration

public static int getStatus(){

return status;

}

public static void setStatus(int i){

status=0;

}

public Addvisitor()

{

initializeComponent();

this.setVisible(true);

}

private void initializeComponent()

{

lblname = new JLabel();

lblintime = new JLabel();

lblouttime = new JLabel();

lblreason = new JLabel();

jTextField1 = new JTextField();

jTextField2 = new JTextField();

jTextField3 = new JTextField();

jTextField4 = new JTextField();

btnsave = new JButton();

btncancel = new JButton();

contentPane = (JPanel)this.getContentPane();

lblname.setText("Visitor name");

lblintime.setText("In Time");

lblouttime.setText("Out Time");

lblreason.setText("Reason");

btnsave.setText("Save");

btnsave.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btnsave_actionPerformed(e);

}

});

btncancel.setText("Cancel");

btncancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btncancel_actionPerformed(e);

}

});

contentPane.setLayout(null);

addComponent(contentPane, lblname, 28,43,74,18);

addComponent(contentPane, lblintime, 30,75,60,20);

addComponent(contentPane, lblouttime, 29,113,60,18);

addComponent(contentPane, lblreason, 30,148,60,16);

addComponent(contentPane, jTextField1, 118,42,155,22);

addComponent(contentPane, jTextField2, 118,75,73,22);

addComponent(contentPane, jTextField3, 118,111,75,22);

addComponent(contentPane, jTextField4, 118,147,160,22);

addComponent(contentPane, btnsave, 138,191,83,28);

addComponent(contentPane, btncancel, 234,190,83,28);

this.setTitle("Addvisitor - extends JDialog");

this.setLocation(new Point(10, 10));

this.setSize(new Dimension(360, 260));

this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

private void btnsave_actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("Save")){

System.out.println("\nbtnsave_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

//write your code to save to database here

//I did not write this.

//then add all values to vector as follows

vec=new Vector();

vec.addElement(jTextField1.getText());

vec.addElement(jTextField2.getText());

vec.addElement(jTextField3.getText());

vec.addElement(jTextField4.getText());

status=1;

btnsave.setText("Exit");

}

if(e.getActionCommand().equals("Exit")){

dispose();

}

}

public static Vector getRowData(){

return vec;

}

public static void setRowData(){

vec=new Vector();

}

private void btncancel_actionPerformed(ActionEvent e)

{

System.out.println("\nbtncancel_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

}

Sruthi.ma at 2007-7-9 5:29:35 > top of Java-index,Desktop,Core GUI APIs...
# 10
Thank you very muchI got exactly what i wanted.i will proceed with my work.If there is any problem. i will let you know.Thank you very much for your serviceCheersJofin
jofin123a at 2007-7-9 5:29:36 > top of Java-index,Desktop,Core GUI APIs...
# 11

Hi

i have just one more question

IS It not possible to add the record as soon as Click on the Save button.?

you have give on option like the

After entering the details I Click on Save Button. then Caption is Changes into EXIT. If i click on the Exit Button only it displays the record on the JTable. and dispose the JDialog

For me The JDialog should remain there to enter the Next record. But the Privouse record should be shown on the JTable

Could you please give an idea for this

Thank you very much

Thank you for your service

Regards

Jofin

jofin123a at 2007-7-9 5:29:36 > top of Java-index,Desktop,Core GUI APIs...
# 12

hi

i have the same problem with my system. So please Help me also.

That is when the JDialog is Opend i Enter the Details and click on save Button. The system should save the record in the Database and show in JTable.

Then it should not Show the record when i click on Exit Button which is in the JDialog. Instead it should clear the JTextField for Entering the Next record.

Please help me out with this issue.

Thank you

rowinraja at 2007-7-9 5:29:36 > top of Java-index,Desktop,Core GUI APIs...
# 13

Please do not continue a single thread for different questions.If you have any issue similar to this,then you can post in new thread with this reference and clearly stating your problem.

One solution to the problem is:We have to set focus to the parent frame after saving the record in database,so that the focus listener of button will update the table and set the focus back to child frame.

sruthima at 2007-7-9 5:29:36 > top of Java-index,Desktop,Core GUI APIs...
# 14

Hi

I Tried out the idea which you gave to Mr.rowinraj. But I am not able to come out of this problem. Please give some modified code for the following.

IS It not possible to add the record as soon as Click on the Save button.?

you have given an option like this

After entering the details I Click on Save Button. then Caption is Changes into EXIT. If i click on the Exit Button only it displays the record on the JTable. and dispose the JDialog

For me The JDialog should remain there to enter the Next record. But the Privouse record should be shown on the JTable and the JTextField should be cleared and the Focus should be in the First JTextField

Could you please give some code for this

Thank you very much

Thank you for your service

Regards

Jofin

jofin123a at 2007-7-9 5:29:36 > top of Java-index,Desktop,Core GUI APIs...
# 15

You need to pass the DefaultTableModel to the dialog. Then when you press the "Save" button in the dialog, you take all the data from the text fields and use the addRow(...) method from the DefaultTableModel to add the data to the TableModel. The table will repaint itself automatically. You then clear the text fields and reset focus on the first text field.

camickra at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...
# 16

Please check this sample and give the proper code and the idea for this

Please i tried in many ways i am not getting it.

code for Visitors.java

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

import java.sql.*;

public class Visitors extends JFrame implements ActionListener

{

JTable table;

JPanel buttonPanel;

JButton button;

private Connection con;

private Statement st;

Addvisitor av;

public Visitors()

{

Vector columnNames = new Vector();

Vector data = new Vector();

// Create table

// Add table and a Button panel to the frame

buttonPanel = new JPanel();

button = new JButton( "Add Visitor" );

button.addActionListener(this);

button.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent arg0) {

System.out.println("focus gained");

System.out.println("this is:"+Addvisitor.getStatus());

if(Addvisitor.getStatus()==1){

System.out.println("vector is:");

((DefaultTableModel)table.getModel()).addRow(Addvisitor.getRowData());

Addvisitor.setStatus(0);

Addvisitor.setRowData();

}

}

public void focusLost(FocusEvent arg0) {

}

});

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

table = new JTable(7,6);

JPanel pnl2 = new JPanel();

pnl2.add(table);

buttonPanel.add( table );

buttonPanel.add( button );

getContentPane().add(pnl2, BorderLayout.NORTH);

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

try

{

//Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

Class.forName("net.sourceforge.jtds.jdbc.Driver");

//String loc = "jdbc:odbc:visitors";

String loc="jdbc:jtds:sqlserver://192.168.1.20:1433/testDatabase";

con = DriverManager.getConnection (loc,"sa","madmax");

st=con.createStatement();

ResultSet rs=st.executeQuery("Select * from Books");

ResultSetMetaData md= rs.getMetaData();

int columns =md.getColumnCount();

String booktblheading[]={"VistorName","IN TIME","OUT TIME","REASON"};

for(int i=1; i<= booktblheading.length;i++)

{

columnNames.addElement(booktblheading[i-1]);

}

while(rs.next())

{

Vector row = new Vector(columns);

for(int i=1;i<=columns;i++)

{

row.addElement(rs.getObject(i));

}

data.addElement(row);

System.out.println("data is:"+data);

}

((DefaultTableModel)table.getModel()).setDataVector(data,columnNames);

rs.close();

st.close();

}

catch (ClassNotFoundException cnf) {

JOptionPane.showMessageDialog (null, "Driver not Loaded...");

System.exit (0);

}

catch (SQLException sqlex) {

JOptionPane.showMessageDialog (null, "Unable to Connect to Database...");

System.exit (0);

}

}

public void actionPerformed (ActionEvent ae)

{

Object obj = ae.getSource();

if (obj == button)

{

av = new Addvisitor();

}

}

public static void main(String[] args)

{

Visitors frame = new Visitors();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setSize(700,500);

frame.setVisible(true);

}

}

[u]Addvisitor.java[/u]

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

import javax.swing.*;

/**

* Summary description for Addvisitor

*

*/

public class Addvisitor extends JDialog

{

// Variables declaration

private JLabel lblname;

private JLabel lblintime;

private JLabel lblouttime;

private JLabel lblreason;

private JTextField jTextField1;

private JTextField jTextField2;

private JTextField jTextField3;

private JTextField jTextField4;

private JButton btnsave;

private JButton btncancel;

private JPanel contentPane;

private static Vector vec;

private static int status=0;

// End of variables declaration

public static int getStatus(){

return status;

}

public static void setStatus(int i){

status=0;

}

public Addvisitor()

{

initializeComponent();

this.setVisible(true);

}

private void initializeComponent()

{

lblname = new JLabel();

lblintime = new JLabel();

lblouttime = new JLabel();

lblreason = new JLabel();

jTextField1 = new JTextField();

jTextField2 = new JTextField();

jTextField3 = new JTextField();

jTextField4 = new JTextField();

btnsave = new JButton();

btncancel = new JButton();

contentPane = (JPanel)this.getContentPane();

lblname.setText("Visitor name");

lblintime.setText("In Time");

lblouttime.setText("Out Time");

lblreason.setText("Reason");

btnsave.setText("Save");

btnsave.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btnsave_actionPerformed(e);

}

});

btncancel.setText("Cancel");

btncancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btncancel_actionPerformed(e);

}

});

contentPane.setLayout(null);

addComponent(contentPane, lblname, 28,43,74,18);

addComponent(contentPane, lblintime, 30,75,60,20);

addComponent(contentPane, lblouttime, 29,113,60,18);

addComponent(contentPane, lblreason, 30,148,60,16);

addComponent(contentPane, jTextField1, 118,42,155,22);

addComponent(contentPane, jTextField2, 118,75,73,22);

addComponent(contentPane, jTextField3, 118,111,75,22);

addComponent(contentPane, jTextField4, 118,147,160,22);

addComponent(contentPane, btnsave, 138,191,83,28);

addComponent(contentPane, btncancel, 234,190,83,28);

this.setTitle("Addvisitor - extends JDialog");

this.setLocation(new Point(10, 10));

this.setSize(new Dimension(360, 260));

this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

private void btnsave_actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("Save")){

System.out.println("\nbtnsave_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

//write your code to save to database here

//I did not write this.

//then add all values to vector as follows

vec=new Vector();

vec.addElement(jTextField1.getText());

vec.addElement(jTextField2.getText());

vec.addElement(jTextField3.getText());

vec.addElement(jTextField4.getText());

status=1;

btnsave.setText("Exit");

}

if(e.getActionCommand().equals("Exit")){

dispose();

}

}

public static Vector getRowData(){

return vec;

}

public static void setRowData(){

vec=new Vector();

}

private void btncancel_actionPerformed(ActionEvent e)

{

System.out.println("\nbtncancel_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

}

Please give me some Sample code for this

Thank you very much

rowinraja at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...
# 17

Hey, This is the modified Code.We can also use JInternalFrame which has direct methods like toBack() and toFront() methods,but I have made modifications to the same Dialog.

Visitors.java

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

import java.sql.*;

public class Visitors extends JFrame implements ActionListener

{

JTable table;

JPanel buttonPanel;

JButton button;

private Connection con;

private Statement st;

Addvisitor av=null;

JDesktopPane desktop;

public Visitors()

{

Vector columnNames = new Vector();

Vector data = new Vector();

// Create table

// Add table and a Button panel to the frame

buttonPanel = new JPanel();

button = new JButton( "Add Visitor" );

button.addActionListener(this);

button.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent arg0) {

System.out.println("focus gained");

System.out.println("this is:"+Addvisitor.getStatus());

if(Addvisitor.getStatus()==1){

System.out.println("vector is:");

((DefaultTableModel)table.getModel()).addRow(Addvisitor.getRowData());

Addvisitor.setStatus(0);

Addvisitor.setRowData();

if(av!=null)

av.show();

}

}

public void focusLost(FocusEvent arg0) {

}

});

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

table = new JTable(7,6);

JPanel pnl2 = new JPanel();

pnl2.add(table);

buttonPanel.add( table );

buttonPanel.add( button );

getContentPane().add(pnl2, BorderLayout.NORTH);

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

desktop=new JDesktopPane();

getContentPane().add(desktop);

try

{

//Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

Class.forName("net.sourceforge.jtds.jdbc.Driver");

//String loc = "jdbc:odbc:visitors";

String loc="jdbc:jtds:sqlserver://192.168.1.20:1433/testDatabase";

con = DriverManager.getConnection (loc,"sa","madmax");

st=con.createStatement();

ResultSet rs=st.executeQuery("Select * from Books");

ResultSetMetaData md= rs.getMetaData();

int columns =md.getColumnCount();

String booktblheading[]={"VistorName","IN TIME","OUT TIME","REASON"};

for(int i=1; i<= booktblheading.length;i++)

{

columnNames.addElement(booktblheading[i-1]);

}

while(rs.next())

{

Vector row = new Vector(columns);

for(int i=1;i<=columns;i++)

{

row.addElement(rs.getObject(i));

}

data.addElement(row);

System.out.println("data is:"+data);

}

((DefaultTableModel)table.getModel()).setDataVector(data,columnNames);

rs.close();

st.close();

}

catch (ClassNotFoundException cnf) {

JOptionPane.showMessageDialog (null, "Driver not Loaded...");

System.exit (0);

}

catch (SQLException sqlex) {

JOptionPane.showMessageDialog (null, "Unable to Connect to Database...");

System.exit (0);

}

}

public void actionPerformed (ActionEvent ae)

{

Object obj = ae.getSource();

if (obj == button)

{

av = new Addvisitor();

//desktop.add(av);

}

}

public static void main(String[] args)

{

Visitors frame = new Visitors();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setSize(700,500);

frame.setVisible(true);

}

}

AddVisitors.java

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

import javax.swing.*;

/**

* Summary description for Addvisitor

*

*/

public class Addvisitor extends JDialog//JInternalFrame

{

// Variables declaration

private JLabel lblname;

private JLabel lblintime;

private JLabel lblouttime;

private JLabel lblreason;

private JTextField jTextField1;

private JTextField jTextField2;

private JTextField jTextField3;

private JTextField jTextField4;

private JButton btnsave;

private JButton btncancel;

private JPanel contentPane;

private static Vector vec;

private static int status=0;

// End of variables declaration

public static int getStatus(){

return status;

}

public static void setStatus(int i){

status=0;

}

public Addvisitor()

{

initializeComponent();

this.setVisible(true);

}

private void initializeComponent()

{

lblname = new JLabel();

lblintime = new JLabel();

lblouttime = new JLabel();

lblreason = new JLabel();

jTextField1 = new JTextField();

jTextField2 = new JTextField();

jTextField3 = new JTextField();

jTextField4 = new JTextField();

btnsave = new JButton();

btncancel = new JButton();

contentPane = (JPanel)this.getContentPane();

lblname.setText("Visitor name");

lblintime.setText("In Time");

lblouttime.setText("Out Time");

lblreason.setText("Reason");

btnsave.setText("Save");

btnsave.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btnsave_actionPerformed(e);

}

});

btncancel.setText("Cancel");

btncancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btncancel_actionPerformed(e);

}

});

contentPane.setLayout(null);

addComponent(contentPane, lblname, 28,43,74,18);

addComponent(contentPane, lblintime, 30,75,60,20);

addComponent(contentPane, lblouttime, 29,113,60,18);

addComponent(contentPane, lblreason, 30,148,60,16);

addComponent(contentPane, jTextField1, 118,42,155,22);

addComponent(contentPane, jTextField2, 118,75,73,22);

addComponent(contentPane, jTextField3, 118,111,75,22);

addComponent(contentPane, jTextField4, 118,147,160,22);

addComponent(contentPane, btnsave, 138,191,83,28);

addComponent(contentPane, btncancel, 234,190,83,28);

this.setTitle("Addvisitor - extends JDialog");

this.setLocation(new Point(10, 10));

this.setSize(new Dimension(360, 260));

//this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

private void btnsave_actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("Save")){

System.out.println("\nbtnsave_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

//write your code to save to database here

//I did not write this.

//then add all values to vector as follows

vec=new Vector();

vec.addElement(jTextField1.getText());

vec.addElement(jTextField2.getText());

vec.addElement(jTextField3.getText());

vec.addElement(jTextField4.getText());

status=1;

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

jTextField4.setText("");

hide();

System.out.println("sruthi is here");

}

if(e.getActionCommand().equals("Exit")){

dispose();

}

}

public static Vector getRowData(){

return vec;

}

public static void setRowData(){

vec=new Vector();

}

private void btncancel_actionPerformed(ActionEvent e)

{

System.out.println("\nbtncancel_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

}

Sruthi.ma at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...
# 18
Thank you very Much surithi, Only you are providing me a good solution.Thank you very Much. Now i can go ahead with my work without any problem.Thank you very much for your ServiceCheersjofin
jofin123a at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...
# 19

> Please i tried in many ways i am not getting it.

As I suggested above:

Then when you press the "Save" button in the dialog, you take all the data from the text fields and use the addRow(...) method from the DefaultTableModel to add the data to the TableModel

Where in your code do you do what I suggested?

camickra at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...
# 20

> Only you are providing me a good solution.

Just looked at the solution, and frankly its a terrible solution.

The use of static methods in the Addvistor class are not required.

The use of the FocusListener on your "Add Visitor" button is ugly.

Hiding the Addvisitor dialog when the "Save" button is clicked so that focus goes to the "Add Visitor" button and then reshowing the Addvisitor dialog is ugly.

As I mentioned twice before, you simply pass the TableModel to the Addvisitor dialog and the invoke the addRow(...) method directly from the "Save" button code.

camickra at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...
# 21
Hi Camrik Please give me some sample. I am not that Much cleaver to understand just by words.If you provide me Some sample surely i will understandWhy don't you help By providing some sample here.Only then i can learn your Idea alsoThank you very Much
jofin123a at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...
# 22

Hi, camickr,

Thank you for letting me know about the solution that I gave.I am new to Swing and trying to find out solution,in the process I am doing mistakes,but since i got the answer I thought it is correct.

For this problem I thought that I should get the cevtor data from second frame and then go to first frame to update and so did it like that.Now based upon the second suggestion I did and got solution.

Hi, jofin123 here is the code:

Visitor.java

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

import java.sql.*;

public class Visitors extends JFrame implements ActionListener

{

JTable table;

JPanel buttonPanel;

JButton button;

private Connection con;

private Statement st;

Addvisitor av=null;

JDesktopPane desktop;

public Visitors()

{

Vector columnNames = new Vector();

Vector data = new Vector();

// Create table

// Add table and a Button panel to the frame

buttonPanel = new JPanel();

button = new JButton( "Add Visitor" );

button.addActionListener(this);

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

table = new JTable(7,6);

JPanel pnl2 = new JPanel();

pnl2.add(table);

buttonPanel.add( table );

buttonPanel.add( button );

getContentPane().add(pnl2, BorderLayout.NORTH);

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

desktop=new JDesktopPane();

getContentPane().add(desktop);

try

{

//Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

Class.forName("net.sourceforge.jtds.jdbc.Driver");

//String loc = "jdbc:odbc:visitors";

String loc="jdbc:jtds:sqlserver://192.168.1.20:1433/testDatabase";

con = DriverManager.getConnection (loc,"sa","madmax");

st=con.createStatement();

ResultSet rs=st.executeQuery("Select * from Books");

ResultSetMetaData md= rs.getMetaData();

int columns =md.getColumnCount();

String booktblheading[]={"VistorName","IN TIME","OUT TIME","REASON"};

for(int i=1; i<= booktblheading.length;i++)

{

columnNames.addElement(booktblheading[i-1]);

}

while(rs.next())

{

Vector row = new Vector(columns);

for(int i=1;i<=columns;i++)

{

row.addElement(rs.getObject(i));

}

data.addElement(row);

System.out.println("data is:"+data);

}

((DefaultTableModel)table.getModel()).setDataVector(data,columnNames);

rs.close();

st.close();

}

catch (ClassNotFoundException cnf) {

JOptionPane.showMessageDialog (null, "Driver not Loaded...");

System.exit (0);

}

catch (SQLException sqlex) {

JOptionPane.showMessageDialog (null, "Unable to Connect to Database...");

System.exit (0);

}

}

public void actionPerformed (ActionEvent ae)

{

Object obj = ae.getSource();

if (obj == button)

{

av = new Addvisitor(table.getModel());

//desktop.add(av);

}

}

public static void main(String[] args)

{

Visitors frame = new Visitors();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setSize(700,500);

frame.setVisible(true);

}

}

AddVisitors.java

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

/**

* Summary description for Addvisitor

*

*/

public class Addvisitor extends JDialog//JInternalFrame

{

// Variables declaration

private JLabel lblname;

private JLabel lblintime;

private JLabel lblouttime;

private JLabel lblreason;

private JTextField jTextField1;

private JTextField jTextField2;

private JTextField jTextField3;

private JTextField jTextField4;

private JButton btnsave;

private JButton btncancel;

private JPanel contentPane;

private Vector vec;

DefaultTableModel tm;

// End of variables declaration

public Addvisitor(TableModel tm)

{

this.tm=(DefaultTableModel)tm;

initializeComponent();

this.setVisible(true);

}

private void initializeComponent()

{

lblname = new JLabel();

lblintime = new JLabel();

lblouttime = new JLabel();

lblreason = new JLabel();

jTextField1 = new JTextField();

jTextField2 = new JTextField();

jTextField3 = new JTextField();

jTextField4 = new JTextField();

btnsave = new JButton();

btncancel = new JButton();

contentPane = (JPanel)this.getContentPane();

lblname.setText("Visitor name");

lblintime.setText("In Time");

lblouttime.setText("Out Time");

lblreason.setText("Reason");

btnsave.setText("Save");

btnsave.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btnsave_actionPerformed(e);

}

});

btncancel.setText("Cancel");

btncancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

btncancel_actionPerformed(e);

}

});

contentPane.setLayout(null);

addComponent(contentPane, lblname, 28,43,74,18);

addComponent(contentPane, lblintime, 30,75,60,20);

addComponent(contentPane, lblouttime, 29,113,60,18);

addComponent(contentPane, lblreason, 30,148,60,16);

addComponent(contentPane, jTextField1, 118,42,155,22);

addComponent(contentPane, jTextField2, 118,75,73,22);

addComponent(contentPane, jTextField3, 118,111,75,22);

addComponent(contentPane, jTextField4, 118,147,160,22);

addComponent(contentPane, btnsave, 138,191,83,28);

addComponent(contentPane, btncancel, 234,190,83,28);

this.setTitle("Addvisitor - extends JDialog");

this.setLocation(new Point(10, 10));

this.setSize(new Dimension(360, 260));

//this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

private void btnsave_actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("Save")){

System.out.println("\nbtnsave_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

//write your code to save to database here

//I did not write this.

//then add all values to vector as follows

vec=new Vector();

vec.addElement(jTextField1.getText());

vec.addElement(jTextField2.getText());

vec.addElement(jTextField3.getText());

vec.addElement(jTextField4.getText());

tm.addRow(vec);

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

jTextField4.setText("");

//hide();

System.out.println("sruthi is here");

}

if(e.getActionCommand().equals("Exit")){

dispose();

}

}

private void btncancel_actionPerformed(ActionEvent e)

{

System.out.println("\nbtncancel_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

}

I have edited the old files,so If I have any unneccesary code,please delete those lines.

sruthima at 2007-7-21 17:13:22 > top of Java-index,Desktop,Core GUI APIs...