• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getActionCommand() this is not working in code

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I developed the GUI and tried to update the database. I am able to compile the code and run but "if(ae.getActionCommand()=="Exit")"
function is not working i.e when i click on exit button no actions is performed. Can any one help where i am going wrong.Let me tell you people that i am a beginner who's learning java

____________________________________________________________________

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Example implements ActionListener
{
Frame f=new Frame();
Label l1=new Label("Id");
JComboBox c1=new JComboBox();
TextField tf1=new TextField(20);
Label l2=new Label("Description");
TextField tf2=new TextField(20);
Label l3=new Label("Rate");
TextField tf3=new TextField(20);

Label l5=new Label("Unit of measurement");
TextField tf5=new TextField(20);

Button b1=new Button("Insert");
Button b2=new Button("Update");
Button b3=new Button("Delete");
Button b4=new Button("Clear");
Button b5=new Button("Exit");
Connection con;
Statement stmt;
ResultSet rs;
PreparedStatement stat;
Example()
{


f.setLayout(new FlowLayout());
f.add(l1);
f.add(tf1);
f.add(c1);

f.add(l2);
f.add(tf2);
f.add(l3);
f.add(tf3);


f.add(l5);
f.add(tf5);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
f.setSize(200,400);
f.show();
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:MyDataSource","","");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT id FROM MyProduct");
while(rs.next())
{
c1.addItem(Integer.toString(rs.getInt(1)));
}
con.close();

}
catch(Exception e)
{
System.out.println("error:"+e);
}
c1.addActionListener(this);
System.out.println("action is listened");
}




public void actionPerformed(ActionEvent ae)
{


if(ae.getActionCommand()=="Exit")
System.exit(0);

if(ae.getActionCommand()=="Delete")
{
try
{
System.out.println("Deleted");
con=DriverManager.getConnection("jdbc dbc:MyDataSource","","");
stmt=con.prepareStatement("DELETE from MyProduct WHERE id= ?");
String selected_id=c1.getSelectedItem().toString();
int id=Integer.parseInt(selected_id);
stat.setInt(1,id);
stat.executeUpdate();
con.close();
c1.removeActionListener(this);
con=DriverManager.getConnection("jdbc dbc:MyDataSource","","");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT id FROM MyProduct");
c1.removeAllItems();
while(rs.next())
c1.addItem(Integer.toString(rs.getInt(1)));
con.close();
c1.addActionListener(this);
tf1.setText("");
tf2.setText("");
tf3.setText("");

tf5.setText("");
//e.setText("Row Deleted");
}
catch(Exception e)
{

}

}
if(ae.getSource()==c1)
{
try
{
con=DriverManager.getConnection("jdbc dbc:MyDataSource","","");
String selected_id = c1.getSelectedItem().toString();
int id=Integer.parseInt(selected_id);
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT id,name,rate,unit FROM MyProduct WHERE id = "+ id);
rs.next();
tf1.setText(selected_id);
tf2.setText(rs.getString(2));
tf3.setText(Integer.toString(rs.getInt(3)));

tf5.setText(Integer.toString(rs.getInt(4)));
con.close();
}
catch(Exception e)
{

}
}
}
public static void main(String args[])
{

Example ea=new Example();
System.out.println(ea);
_______________________________________________________________________


Thanks in Advance

Once again i am a beginner and i know my code is not upto standards so please bear with me.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wrong forum, shifting to another four letter forum...
[ July 31, 2007: Message edited by: Burkhard Hassel ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, this is just an AWT/Swing question.

You need to add an action listener to your button. Before adding b5 to the frame, insert the line...

b5.addActionListener(this);
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also,
"if(ae.getActionCommand()=="Exit")"
probably should be
"if(ae.getActionCommand().equals("Exit"))"

and
Frame f=new Frame();
Label l1=new Label("Id");
JComboBox c1=new JComboBox();//<-----------

you may run into painting problems when mixing swing/awt components.
as the majority of the components are awt, use a Choice() instead of JComboBox

and, in 'delete', with all the sql code, this is not good
catch(Exception e)
{

}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic