Forum Stats

  • 3,827,131 Users
  • 2,260,743 Discussions
  • 7,897,181 Comments

Discussions

Retrieving the object in the Second Action Listner

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Jan 22, 2016 4:00AM in New To Java

Hi,

I have created a Stack class using vectors (VectorStack2). Now i want to implement this operation with the help of buttons.I have created a push button. Its action Listener is creating an object of VectorStack2 class which invokes the push(.... ) method of VectorStack2 class in the actionPerformed(...) method of Action1 class . Now i want to retrieve this object of VectorStack2 class in the second action listener corresponding to the Pop button so that i call invoke pop() method.

I cant understand how to do it?

Some body please guide me. I have put "????" symbols on the line after the object which i want to retrieve in the second actionlistener's actionPerformed method.

import javax.swing.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class VectorStack2{

//private String[ ] stArr = new String[5];

Vector<Integer> items;

private int tos;

  VectorStack2( ){

items =  new Vector<Integer>( );

tos=-1;

}

boolean empty(){

   if(tos == -1)

      return true;

   return false;

}

Object pop( ) {

   if(empty()) {

JOptionPane.showMessageDialog(null, "Stack Underflow");

      System.exit(1);

    }

  

    return items.remove(tos--);

}

public void push(Integer x) {

items.add(++tos,   x );

}

}

public class useVSButt{

   public static void main(String[ ] args) {

   

     int i;

     int nLength=0;

     int lLength=0;

     String nStr="";

     String lStr="";

   

  

   

     JFrame frame = new JFrame("Test");

frame.setVisible(true);

      frame.setSize(500,200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     JPanel panel = new JPanel();

     frame.add(panel);

     JButton button = new JButton("Push");

panel.add(button);

button.addActionListener (new Action1());

     JButton button2 = new JButton("Pop");

panel.add(button2);

button.addActionListener (new Action2());

   

   }//main

}//useStack

class Action1 implements ActionListener {

    VectorStack2 objVS;

        

  public void actionPerformed (ActionEvent e) {

    int i;   

    JFrame frame2 = new JFrame("Clicked");

frame2.setVisible(true);

frame2.setSize(200,200);

    //JLabel label = new JLabel("you clicked me");

    //JPanel panel = new JPanel();

//frame2.add(panel);

    //panel.add(label);

   

     int[ ] int1={100, 200, 300, 400, 500};

     VectorStack2 obj = new VectorStack2( );

     for(i=0; i<5; ++i)

     obj.push(new Integer(int1[i]));

     

     //HOW TO GET THIS OBJECT of VectorStack2 IN Action2's actionPerformed method??????????????????????????????????????

  }

class Action2 implements ActionListener {      

  public void actionPerformed (ActionEvent e) {  

    int i;

    JFrame frame3 = new JFrame("OKNO 3");

    frame3.setVisible(true);

frame3.setSize(200,200);

    //JLabel label = new JLabel("kliknales");

    //JPanel panel = new JPanel();

//frame3.add(panel);

//panel.add(label);

    // VectorStack2 obj = new VectorStack2();

    for(i=0; i<5; ++i)

JOptionPane.showMessageDialog(null, "Popped val is" + obj.pop());

  }

Best Answer

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Jan 22, 2016 2:56AM Answer ✓

    Please look carefully on this lines of your code:

         JButton button = new JButton("Push");
         panel.add(button);
         button.addActionListener (new Action1(objVStack));
    
    
         JButton button2 = new JButton("Pop");
         panel.add(button2);
         button.addActionListener (new Action2(objVStack));
    

    The lesson here is: always choose meaningfull names for your identifiers!

    bye

    TPD

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Jan 21, 2016 1:54PM

    The keyword here is "separation of concerns".

    And the creation of objects is a concern of its own.

    So instead of creating an instance of VectorStack2 in your ActionListener1 you should create it in your main method and pass it as constructor parameter to both ActionListeners.

    bye

    TPD

    User_AYF65
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jan 21, 2016 11:44PM

    Hi,

    Thanks for your response. This has solved the object retrieval problem but still my program is not working correctly. When i am pressing the "Push" button (related to Push operation in my code), its displaying "Stack Underflow" message. This message should be invoked when i press the Pop button without  Pushing any item in the stack (i.e when my stack is empty) but its not working in that way. Some body please check my code & let me know why its behaving in a wrong way.

    import javax.swing.*;

    import java.util.*;

    import java.awt.*;

    import java.awt.event.*;

    class VectorStack2{

    //private String[ ] stArr = new String[5];

    Vector<Integer> items; 

    private int tos;

      VectorStack2( ){

    items =  new Vector<Integer>( );

    tos=-1;

    }

    boolean empty(){

       if(tos == -1)

          return true;

       return false;

    }

    Object pop( ) {

       if(empty()) {

          JOptionPane.showMessageDialog(null, "Stack Underflow");

          System.exit(1);

        }

       

        return items.remove(tos--);

    }

    public void push(Integer x) {

       items.add(++tos,   x );

    }

    }

    public class useVSButt{

       useVSButt(){

        VectorStack2 obj = new VectorStack2();

       }

       public static void main(String[ ] args) {

        

         int i;

         int nLength=0;

         int lLength=0;

         String nStr="";

         String lStr="";

        

       

         VectorStack2 objVStack = new VectorStack2();

         JFrame frame = new JFrame("Test");

         frame.setVisible(true);

          frame.setSize(500,200);

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         JPanel panel = new JPanel();

         frame.add(panel);

         JButton button = new JButton("Push");

         panel.add(button);

         button.addActionListener (new Action1(objVStack));

         JButton button2 = new JButton("Pop");

         panel.add(button2);

         button.addActionListener (new Action2(objVStack));

        

       }//main

    }//useStack

    class Action1 implements ActionListener {

      VectorStack2 objVStack;

      Action1(VectorStack2 objVStackT ) {

         objVStack = objVStackT;

      }

            

      public void actionPerformed (ActionEvent e) {

        int i;    

        JFrame frame2 = new JFrame("Clicked");

        frame2.setVisible(true);

        frame2.setSize(200,200);

        //JLabel label = new JLabel("you clicked me");

        //JPanel panel = new JPanel();

        //frame2.add(panel);

        //panel.add(label); 

        

         int[ ] int1={100, 200, 300, 400, 500};

         for(i=0; i<5; ++i)

         objVStack.push(new Integer(int1[i]));

         

      }

    }  

    class Action2 implements ActionListener {

       VectorStack2 objVStack;

       Action2(VectorStack2 objVStackT ) {

         objVStack = objVStackT;

      }      

      public void actionPerformed (ActionEvent e) {   

        int i;

        JFrame frame3 = new JFrame("OKNO 3");

        frame3.setVisible(true);

        frame3.setSize(200,200);

        //JLabel label = new JLabel("kliknales");

        //JPanel panel = new JPanel();

        //frame3.add(panel);

        //panel.add(label);

        // VectorStack2 obj = new VectorStack2();

        for(i=0; i<5; ++i)

         JOptionPane.showMessageDialog(null, "Popped val is" + objVStack.pop());

      }

    }  

           

    Push button when pressed shows "Stack Underflow" message which is wrong. When we press the push button it should perform the following task only:

    int[ ] int1={100, 200, 300, 400, 500};

         for(i=0; i<5; ++i)

         objVStack.push(new Integer(int1[i]));

    Somebody please guide me.

    Zulfi.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Jan 22, 2016 2:56AM Answer ✓

    Please look carefully on this lines of your code:

         JButton button = new JButton("Push");
         panel.add(button);
         button.addActionListener (new Action1(objVStack));
    
    
         JButton button2 = new JButton("Pop");
         panel.add(button2);
         button.addActionListener (new Action2(objVStack));
    

    The lesson here is: always choose meaningfull names for your identifiers!

    bye

    TPD

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jan 22, 2016 3:45AM

    Hi,

    Thanks for your response. This problem is solved now. The running code is:

    import javax.swing.*;

    import java.util.*;

    import java.awt.*;

    import java.awt.event.*;

    class VectorStack2{

    //private String[ ] stArr = new String[5];

    Vector<Integer> items; 

    private int tos;

      VectorStack2( ){

    items =  new Vector<Integer>( );

    tos=-1;

    }

    boolean empty(){

       if(tos == -1)

          return true;

       return false;

    }

    Object pop( ) {

       if(empty()) {

          JOptionPane.showMessageDialog(null, "Stack Underflow");

          System.exit(1);

        }

       

        return items.remove(tos--);

    }

    public void push(Integer x) {

       items.add(++tos,   x );

    }

    }

    public class useVSButt{

       useVSButt(){

        VectorStack2 obj = new VectorStack2();

       }

       public static void main(String[ ] args) {

        

         int i;

         int nLength=0;

         int lLength=0;

         String nStr="";

         String lStr="";

        

       

         VectorStack2 objVStack = new VectorStack2();

         JFrame frame = new JFrame("Test");

         frame.setVisible(true);

          frame.setSize(500,200);

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         JPanel panel = new JPanel();

         frame.add(panel);

         JButton button = new JButton("Push");

         panel.add(button);

         button.addActionListener (new Action1(objVStack));

         JButton button2 = new JButton("Pop");

         panel.add(button2);

         button2.addActionListener (new Action2(objVStack));

        

       }//main

    }//useStack

    class Action1 implements ActionListener {

      VectorStack2 objVStack;

      Action1(VectorStack2 objVStackT ) {

         objVStack = objVStackT;

      }

            

      public void actionPerformed (ActionEvent e) {

        int i;    

        //JFrame frame2 = new JFrame("Clicked");

        //frame2.setVisible(true);

        //frame2.setSize(200,200);

        //JLabel label = new JLabel("you clicked me");

        //JPanel panel = new JPanel();

        //frame2.add(panel);

        //panel.add(label); 

        

         int[ ] int1={100, 200, 300, 400, 500};

         for(i=0; i<5; ++i)

         objVStack.push(new Integer(int1[i]));

         

      }

    }  

    class Action2 implements ActionListener {

       VectorStack2 objVStack;

       Action2(VectorStack2 objVStackT ) {

         objVStack = objVStackT;

      }      

      public void actionPerformed (ActionEvent e) {   

        int i;

        //JFrame frame3 = new JFrame("OKNO 3");

        //frame3.setVisible(true);

        //frame3.setSize(200,200);

        //JLabel label = new JLabel("kliknales");

        //JPanel panel = new JPanel();

        //frame3.add(panel);

        //panel.add(label);

        // VectorStack2 obj = new VectorStack2();

        for(i=0; i<5; ++i)

         JOptionPane.showMessageDialog(null, "Popped val is" + objVStack.pop());

      }

    }  

           

    Zulfi.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Jan 22, 2016 4:00AM

    Thanks for posting the working solution.

    But even if (or better especially when) your coding is just training: get used to meaningfull naming of your identifiers. This will save you from lots of trouble the kind you just had...

    bye

    TPD

This discussion has been closed.