pls refer to the code below:-
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class PhoneBook1 extends Frame {
//this class creates 2 textfields for name and ph no entering n adds panel
String name; String myname;
ResultSet rs;TestEventPanel pt;Statement stmt;PreparedStatement ps;
Label la;Label lc; Connection con ;LbPanel lb;
GridBagLayout gridbag;GridBagConstraints constraints;
PhoneBook1(){
setLayout(new FlowLayout());
pt=new TestEventPanel();
add(pt);
frameLocation();
// redSlider=new Scrollbar(Scrollbar.VERTICAL,0,0,100,1,10);
//add(redSlider);
addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// System.exit(0);
dispose();
}
}
);
constraints = new GridBagConstraints();
constraints.gridx = 0; //constraints.gridy = 4+i;RELATIVE ;
// i++;
gridbag = new GridBagLayout();
pt.save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="SAVE"){
myname=pt.tn.getText();
createTable(myname);
String nam="name ";
myname=nam.concat(myname);
System.out.println(myname);
// la=new Label(myname);
// add(la);
validate();
}
}
}
);
pt.view.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="view"){
// myname=pt.tn.getText();
viewPhoneBook();
// System.out.println(myname);
// lc=new Label(myname);
// add(lc);
validate();
}
}
}
);
} //end constructor
public void createTable(String x){
name=x;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc
dbc:acodbc","","");
stmt=con.createStatement();
storePhone(name);
// viewPhoneBook();
stmt.close();
con.close();
System.out.println("table PHONEDIARY created");
}
catch(Exception e)
{
e.printStackTrace();
}
} //end createTable()
// name=tn.paramString() ;
public void frameLocation(){
Dimension d = getToolkit().getScreenSize();
setLocation(d.width/4,d.height/3);
setTitle("PhoneBook");
setSize(400,400);
setVisible(true);
}//end framelocation
public void storePhone(String x){
myname=x;
try{
String sql="INSERT INTO PHONEBOOK(NAME)VALUES(?)";
ps=con.prepareStatement(sql);
ps.setString(1,myname);
ps.executeUpdate();
// stmt.executeUpdate("INSERT INTO PHONEBOOK VALUES(" + myname + ",236821)");
}
catch(Exception e){}
}
public void viewPhoneBook(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc
dbc:acodbc","","");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT* from PHONEBOOK");
while(rs.next())
{
Label ld=new Label(rs.getString(1)+" "+rs.getInt(2));
constraints = new GridBagConstraints();
constraints.gridx = 0; //constraints.gridy = 4+i;RELATIVE ;
// i++;
gridbag = new GridBagLayout();
//setLayout(gridbag);*/
// Panel p1=new Panel();
// add(p1);
// p1.add(ld);
add(ld);
gridbag.setConstraints(ld, constraints);
}
}catch(Exception e){}
}//end method viewphonebook
public static void main(String st[]){
PhoneBook1 p=new PhoneBook1();
}
}
and the code for a panel is:-
import java.awt.*;
import java.awt.event.*;
public class TestEventPanel extends Panel {
Button view,b2; Button save;TextField tn;
public TestEventPanel(){
super();
setLayout(new FlowLayout());
setBackground(new Color(0).pink);
Label ln=new Label("name");
tn=new TextField("name");
Label lp=new Label("PhoneNo");
TextField tp=new TextField(15);
save=new Button("SAVE");
add(ln);
add(tn);
add(lp);
add(tp);
add(save);
view = new Button("view");
add(view);
b2 = new Button("delete");
add(b2);
}
public static void main(String st[]){
TestEventPanel p=new TestEventPanel();
}
}
In this i create 2 textfields and two buttons.When i press the button it fetches some text stored in the MS Acess and displays it on the frame
When i run the panel(TestEventPanel) appears with components on it as flowlayout.But the data(text) from the database is also presented as label in flowlayout manner.However i had given the gridbag layout for its laying.
i want the panel with textfield and button to be presented as flowlayout
and rest as gridbag in a vertical line.When i comment setLayout(new FlowLayout()); in the constructor PhoneBook1() Im not able to view the data from database why??
thanks in advance-rajesh