Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using actionPerformed method

  1. #1
    Junior Member Boitumelo's Avatar
    Join Date
    Oct 2013
    Location
    Francistown
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using actionPerformed method

    am trying to make my buttons but i dont know how to use actionPerformed methodconfused:

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.DriverManager;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class Appointment_Form extends JFrame {

    //private static final String = null;
    private String pet;
    private String c_id;
    private String check_in;
    private String check_out;
    private String diagnosis;
    protected Object Appointment_Form;

    public Appointment_Form(){
    Appointment_Form ();

    }

    public void Appointment_Form (){
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(7, 1));
    p1.add(new JLabel("Pet: "));
    p1.add(new JLabel("C_ID: "));
    p1.add(new JLabel("Check_in: "));
    p1.add(new JLabel("Check_out: "));
    p1.add(new JLabel("Diagnosis: "));

    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(6, 1));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));

    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(1, 3));

    p3.add(new JLabel(""));
    JButton add_btn;
    p3.add(add_btn = new JButton("Calendar"));

    p3.add(add_btn = new JButton("Cancel"));
    JButton list_btn;
    p3.add( list_btn = new JButton("Next"));


    JPanel p4 = new JPanel(new BorderLayout());

    p2.setLayout(new GridLayout(7, 2));

    p4.add(p1, BorderLayout.WEST);
    p4.add(p2, BorderLayout.CENTER);
    p4.add(p3, BorderLayout.SOUTH);
    /* p4.add(new JButton("Calendar"),BorderLayout.WEST);
    p4.add(new JButton("Cancel"),BorderLayout.CENTER);
    p4.add(new JButton("Next"),BorderLayout.EAST);*/

    add(p4, BorderLayout.WEST);

    add_btn.addActionListener(
    new ActionListener(){

    public void actionPerformed(ActionEvent e)
    /*{
    if(e.getSource()== Appointment_Form)
    {
    try{
    System.out.println("Loading the driver...");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Establishing a connection...");
    Connection conne = DriverManager.getConnection("jdbcdbcetSystem_DSN");
    System.out.println("a success!");

    //Get statement from the connection
    Statement state = (Statement) conne.createStatement();

    //Execute statement
    String Query ="select "
    }
    }
    }


    );*/

    list_btn.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){

    }
    }

    );

    }


    public static void main(String[]args){

    new Calendar1();

    Appointment_Form frame = new Appointment_Form();

    frame.setTitle("Appointment Form");
    frame.setSize(360, 370);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);




    }

    public String getPet();{

    return getPet();
    }

    public void setPet(String pet);{

    this.pet = pet;
    }

    public String getC_id();{
    return c_id;
    }

    public void setC_id(String c_id);{
    this.c_id = c_id;
    }

    public String getCheck_in();{

    return getCheck_in();
    }

    public void setCheck_in(String check_in);{
    this.check_in = check_in;
    }

    public String getCheck_out();{

    return getCheck_out();
    }

    public void setCheck_out(String check_out );{

    this.check_out = check_out;
    }

    public String getDiagnosis();{

    return getDiagnosis();
    }

    public void setDiagnosis(String diagnosis);{

    this.diagnosis = diagnosis;
    }




    }

    --- Update ---

    meant to say "was trying to make my buttons work by i have a problem using the actionPerformed method"


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using actionPerformed method

    Please read the Announcement topic at the top of the sub-forum to learn how to post your code tags along with other useful stuff for newcomers.

    What problems are you having? Are you getting errors? If so, please post them. Otherwise, describe what is or is not happening that you would like to change. Describe the steps necessary to duplicate the failure with the code you've posted.

    --- Update ---

    So you have a char y that needs to be "turned into" an int num. Or, you could back up a step and parse each character (as a String object) from the String intX directly using substring() method. I would try that to eliminate the char step entirely.

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Using actionPerformed method

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.DriverManager;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Appointment_Form extends JFrame {
     
    //private static final String = null;
    private String pet;
    private String c_id;
    private String check_in;
    private String check_out;
    private String diagnosis;
    protected Object Appointment_Form;
     
    public Appointment_Form(){
    Appointment_Form ();
     
    }
     
    public void Appointment_Form (){
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(7, 1));
    p1.add(new JLabel("Pet: "));
    p1.add(new JLabel("C_ID: "));
    p1.add(new JLabel("Check_in: "));
    p1.add(new JLabel("Check_out: "));
    p1.add(new JLabel("Diagnosis: "));
     
    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(6, 1));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));
    p2.add(new JTextField(15));
     
    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(1, 3));
     
    p3.add(new JLabel(""));
    JButton add_btn;
    p3.add(add_btn = new JButton("Calendar"));
     
    p3.add(add_btn = new JButton("Cancel"));
    JButton list_btn;
    p3.add( list_btn = new JButton("Next"));
     
     
    JPanel p4 = new JPanel(new BorderLayout());
     
    p2.setLayout(new GridLayout(7, 2));
     
    p4.add(p1, BorderLayout.WEST);
    p4.add(p2, BorderLayout.CENTER);
    p4.add(p3, BorderLayout.SOUTH);
    /* p4.add(new JButton("Calendar"),BorderLayout.WEST);
    p4.add(new JButton("Cancel"),BorderLayout.CENTER);
    p4.add(new JButton("Next"),BorderLayout.EAST);*/
     
    add(p4, BorderLayout.WEST);
     
    add_btn.addActionListener(
    new ActionListener(){
     
    public void actionPerformed(ActionEvent e)
    /*{
    if(e.getSource()== Appointment_Form)
    {
    try{
    System.out.println("Loading the driver...");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Establishing a connection...");
    Connection conne = DriverManager.getConnection("jdbcdbcetSystem_DSN");
    System.out.println("a success!");
     
    //Get statement from the connection
    Statement state = (Statement) conne.createStatement();
     
    //Execute statement
    String Query ="select "
    }
    }
    }
     
     
    );*/
     
    list_btn.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
     
    }
    }
     
    );
     
    }
     
     
    public static void main(String[]args){
     
    new Calendar1();
     
    Appointment_Form frame = new Appointment_Form();
     
    frame.setTitle("Appointment Form");
    frame.setSize(360, 370);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
     
     
     
     
    }
     
    public String getPet();{
     
    return getPet();
    }
     
    public void setPet(String pet);{
     
    this.pet = pet;
    }
     
    public String getC_id();{
    return c_id;
    }
     
    public void setC_id(String c_id);{
    this.c_id = c_id;
    }
     
    public String getCheck_in();{
     
    return getCheck_in();
    }
     
    public void setCheck_in(String check_in);{
    this.check_in = check_in;
    }
     
    public String getCheck_out();{
     
    return getCheck_out();
    }
     
    public void setCheck_out(String check_out );{
     
    this.check_out = check_out;
    }
     
    public String getDiagnosis();{
     
    return getDiagnosis();
    }
     
    public void setDiagnosis(String diagnosis);{
     
    this.diagnosis = diagnosis;
    }
     
     
     
     
    }

    It is a bad idea to have an object name that is the same as either a standard java class name or the name of a java class that you use. The fact that you also have a method called Appointment_Form doesn't help either.

    The getSource() method will return the Object upon which the event initially occurred. (i.e. the button)


    However, you will have to make your JButton final to refer to it in the ActionListener or you will have to make the JButton a class variable rather than a variable local to the constructor.

    (Also, I might add, that the getSource() method is more useful when you have the same ActionListener is added to several buttons. Then, you'd use it to figure out which one fired the ActionListener so you'd know how to respond. With your anonymous ActionListener, unless you've added the button's actionListener to another Button as well, the only source that can fire it is the button that you added it to.)

Similar Threads

  1. Actionlistener and actionperformed question
    By ms_ceng in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2011, 11:59 AM
  2. actionPerformed Question.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2011, 11:18 AM
  3. actionPerformed not returning a value.
    By gher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 10:15 AM
  4. actionPerformed method
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 4th, 2010, 02:29 AM
  5. actionPerformed
    By Kumarrrr in forum Java Theory & Questions
    Replies: 5
    Last Post: November 23rd, 2010, 09:08 AM