Member Avatar for mehnihma

Can you help me with this problem

I have a class and I need to create separate class justo fro reste button, but I am not sure how to do this?

Here is what I have done

import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
	

   public class MyEventC extends JFrame implements ActionListener
   {
   
      JTextField text;
		
      public MyEventC()
      {
         setLayout(new BorderLayout(5,10));
      
         JPanel topSide = new JPanel();
         topSide.setLayout(new FlowLayout(FlowLayout.LEFT, 25,3));
         JLabel one = new JLabel("Current value");
         text = new JTextField("0", 10);
         topSide.add(one);
         topSide.add(text);
         text.addActionListener(this);
      
         add(topSide, "North");
      
         JPanel southSide = new JPanel();
         southSide.setLayout(new FlowLayout(FlowLayout.CENTER, 20,3));
         JButton btn1 = new JButton("+");
         JButton btn2 = new JButton("-");
         JButton btn3 = new JButton("Reset");
         JButton btn4 = new JButton("Quit");
      
         southSide.add(btn1);
         southSide.add(btn2);
         southSide.add(btn3);
        southSide.add(btn4);
         add(southSide, BorderLayout.SOUTH);
      	
      	//text.addActionListener();
      	
         btn1.addActionListener(this);
         btn2.addActionListener(this);
         btn3.addActionListener(this);
         btn4.addActionListener(this);
      	
      	
      	
      
      }
   	
      public void actionPerformed(ActionEvent e) 
      
      {
         if (e.getActionCommand() == "+")
         {
            int num1 = Integer.parseInt(text.getText());
         
            num1++;
         	
            String result = num1 + "";
            text.setText(result);	
            System.out.println(num1+"");		
         }
         
         else if (e.getActionCommand() == "-")
         {
            int num1 = Integer.parseInt(text.getText());
         
            num1--;
         	
            String result = num1 + "";
            text.setText(result);	
            System.out.println(num1+"");	
         }
			
         			
			
         else if(e.getActionCommand() == "Quit")
         {
           
            System.exit(0);
         }
      
      
      }
   
      public static void main(String[]args)
      {
         MyEventC event = new MyEventC();
			ResetListener reset = new ResetListener();
         event.setTitle("Part 4 Using separte class for Reset Button");
         event.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         event.setSize(400,150);
         event.setVisible(true);
      
      }
   
   }
import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;


   public class ResetListener extends MyEventC implements ActionListener
   
   {
   
   
   
   
      public void actionPerformed(ActionEvent e) 
      
      {
         if (e.getActionCommand() == "Reset")
         {
            text.setText("0");	
            System.out.println("test1");	
         }
      
      }
   
   
   }

" I need to create separate class justo fro reste button"
>> That is very loose description. Can yo be more specific? If I take your request literal then I would expected to see ResetButton class extending JButton(setting all parameters like title, size, colour) with ActionListener specifically only for this button

Member Avatar for mehnihma

in description I just have this:

Modify your solution from Part 1 to implement the action listener for the Reset button in a separate class name ResetListener

in description I just have this:

Modify your solution from Part 1 to implement the action listener for the Reset button in a separate class name ResetListener

what are the problems you are having this code looks fine to me, just you would need to add the action listener to the appropriate button now?

Member Avatar for mehnihma

I dont know what I need to do to make it work in separate class

in description I just have this:

Modify your solution from Part 1 to implement the action listener for the Reset button in a separate class name ResetListener

That is what you failed to add to your post, otherwise I wouldn't have asked.

I dont know what I need to do to make it work in separate class

check here:

public class SomeActionListener implements ActionListener{

private JTextField textField1;
private JComboBox combo1;
private JTextField textField2;
//...

public SomeActionListener(JTextField textField1, JComboBox combo1, 
                                          JTextField textField2){
    this.textField1=textField1;
    this.combo1=combo1;
    this.textField2=textField2;
    //...
}

public void actionPerformed(ActionEvent e) {
//cmd
}

}

the action listener can then be implemented/added by:

ActionListener actionListener = 
                               new SomeActionListener(textField1, combo1, textField2);
someButton.addActionListener(actionListner);
Member Avatar for mehnihma

is this ok?

import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
	

   public class MyEventC extends JFrame implements ActionListener
   {
   
      JTextField text;
   	
      public MyEventC()
      {
         setLayout(new BorderLayout(5,10));
      
         JPanel topSide = new JPanel();
         topSide.setLayout(new FlowLayout(FlowLayout.LEFT, 25,3));
         JLabel one = new JLabel("Current value");
         text = new JTextField("0", 10);
         topSide.add(one);
         topSide.add(text);
         text.addActionListener(this);
      
         add(topSide, "North");
      
         JPanel southSide = new JPanel();
         southSide.setLayout(new FlowLayout(FlowLayout.CENTER, 20,3));
         JButton btn1 = new JButton("+");
         JButton btn2 = new JButton("-");
         JButton btn3 = new JButton("Reset");
         JButton btn4 = new JButton("Quit");
      
         southSide.add(btn1);
         southSide.add(btn2);
         southSide.add(btn3);
         southSide.add(btn4);
         add(southSide, BorderLayout.SOUTH);
      	
      	//text.addActionListener();
      	
         btn1.addActionListener(this);
         btn2.addActionListener(this);
         btn3.addActionListener(this);
         btn4.addActionListener(this);
      	
      	
      	
      
      }
   	
      public void actionPerformed(ActionEvent e) 
      
      {
         if (e.getActionCommand() == "+")
         {
            int num1 = Integer.parseInt(text.getText());
         
            num1++;
         	
            String result = num1 + "";
            text.setText(result);	
            System.out.println(num1+"");		
         }
         
         else if (e.getActionCommand() == "-")
         {
            int num1 = Integer.parseInt(text.getText());
         
            num1--;
         	
            String result = num1 + "";
            text.setText(result);	
            System.out.println(num1+"");	
         }
			
			
         else if (e.getActionCommand() == "Reset")
         {
            ResetListener reset = new ResetListener();
				text.setText(reset.SetValue());
         }
         
         			
         
         else if(e.getActionCommand() == "Quit")
         {
           
            System.exit(0);
         }
      
      
      }
   
      public static void main(String[]args)
      {
         MyEventC event = new MyEventC();
         
         event.setTitle("Part 4 Using separte class for Reset Button");
         event.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         event.setSize(400,150);
         event.setVisible(true);
      
      }
   
   }
import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;


   public class ResetListener extends MyEventC
   
   {
   
      String reset;
   
   
      public ResetListener()
      
      {
           
      		
         this.reset = "0";
            
      }
   		
   		
      public String SetValue()
      	
      	
{

         return reset;

}
      
   }

is this ok?

import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
	

   public class MyEventC extends JFrame implements ActionListener
   {
   
      JTextField text;
   	
      public MyEventC()
      {
         setLayout(new BorderLayout(5,10));
      
         JPanel topSide = new JPanel();
         topSide.setLayout(new FlowLayout(FlowLayout.LEFT, 25,3));
         JLabel one = new JLabel("Current value");
         text = new JTextField("0", 10);
         topSide.add(one);
         topSide.add(text);
         text.addActionListener(this);
      
         add(topSide, "North");
      
         JPanel southSide = new JPanel();
         southSide.setLayout(new FlowLayout(FlowLayout.CENTER, 20,3));
         JButton btn1 = new JButton("+");
         JButton btn2 = new JButton("-");
         JButton btn3 = new JButton("Reset");
         JButton btn4 = new JButton("Quit");
      
         southSide.add(btn1);
         southSide.add(btn2);
         southSide.add(btn3);
         southSide.add(btn4);
         add(southSide, BorderLayout.SOUTH);
      	
      	//text.addActionListener();
      	
         btn1.addActionListener(this);
         btn2.addActionListener(this);
         btn3.addActionListener(this);
         btn4.addActionListener(this);
      	
      	
      	
      
      }
   	
      public void actionPerformed(ActionEvent e) 
      
      {
         if (e.getActionCommand() == "+")
         {
            int num1 = Integer.parseInt(text.getText());
         
            num1++;
         	
            String result = num1 + "";
            text.setText(result);	
            System.out.println(num1+"");		
         }
         
         else if (e.getActionCommand() == "-")
         {
            int num1 = Integer.parseInt(text.getText());
         
            num1--;
         	
            String result = num1 + "";
            text.setText(result);	
            System.out.println(num1+"");	
         }
			
			
         else if (e.getActionCommand() == "Reset")
         {
            ResetListener reset = new ResetListener();
				text.setText(reset.SetValue());
         }
         
         			
         
         else if(e.getActionCommand() == "Quit")
         {
           
            System.exit(0);
         }
      
      
      }
   
      public static void main(String[]args)
      {
         MyEventC event = new MyEventC();
         
         event.setTitle("Part 4 Using separte class for Reset Button");
         event.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         event.setSize(400,150);
         event.setVisible(true);
      
      }
   
   }
import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;


   public class ResetListener extends MyEventC
   
   {
   
      String reset;
   
   
      public ResetListener()
      
      {
           
      		
         this.reset = "0";
            
      }
   		
   		
      public String SetValue()
      	
      	
{

         return reset;

}
      
   }

from what i understand your button has to work on a seperate class action listener:

//MyEventC.java

       btn3.addActionListener(new ResetListener());//this would add the action listener

and here is the separate class that holds the action listener however it was giving me trouble when it was extended from MyEventC so i didnt extend it:

//ResetListener.java
//package myeventc;

public class ResetListener implements ActionListener {

    public ResetListener() {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Reset".equals(e.getActionCommand())) {
            new MyEventC().text.setText("0");
            System.out.println("test1");
        }

    }
}

but the code you have is still fine as you'll need to return the text to set etc... just add an action listener in that class and in the other add the action listner class to the button

Member Avatar for mehnihma

I have tried this but it just prints out test1 and no action?

my mistake try here you would then ofcourse add the listner like so:

btn3.addActionListener(new ResetListener(text));//changed this

and here is the other ResetListener Class

import java.awt.event.*;
import javax.swing.JTextField;

public class ResetListener implements ActionListener {
private JTextField jff;
    public ResetListener(JTextField jf) {
        this.jff=jf;
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Reset".equals(e.getActionCommand())) {
            jff.setText("0");
            System.out.println("test1");
        }

    }
}
Member Avatar for mehnihma

thanks it works now

Passing out object from form is not good idea. You should not allowed objects that are not associated directly interfere. Better (not perfect solution) would be this

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MyEventC extends JFrame implements ActionListener {

    private JTextField text;

    public MyEventC() {
        setLayout(new BorderLayout(5, 10));

        JPanel topSide = new JPanel();
        topSide.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 3));
        JLabel one = new JLabel("Current value");
        text = new JTextField("0", 10);
        topSide.add(one);
        topSide.add(text);
        text.addActionListener(this);

        add(topSide, "North");

        JPanel southSide = new JPanel();
        southSide.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 3));
        JButton btn1 = new JButton("+");
        JButton btn2 = new JButton("-");
        JButton resetButton = new JButton("Reset");
        JButton btn4 = new JButton("Quit");

        southSide.add(btn1);
        southSide.add(btn2);
        southSide.add(resetButton);
        southSide.add(btn4);
        add(southSide, BorderLayout.SOUTH);

        //text.addActionListener();

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        resetButton.addActionListener(new ResetListener(this));
        btn4.addActionListener(this);


    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "+") {
            int num1 = Integer.parseInt(text.getText());
            num1++;
            String result = num1 + "";
            text.setText(result);
            System.out.println(num1 + "");
        } else if (e.getActionCommand() == "-") {
            int num1 = Integer.parseInt(text.getText());
            num1--;
            String result = num1 + "";
            text.setText(result);
            System.out.println(num1 + "");
        }


        else if (e.getActionCommand() == "Quit") {

            System.exit(0);
        }
    }

    public static void main(String[] args) {
        MyEventC event = new MyEventC();

        event.setTitle("Part 4 Using separte class for Reset Button");
        event.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        event.setSize(400, 150);
        event.setVisible(true);

    }

    public void resetForm() {
        text.setText("0");
    }
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ResetListener implements ActionListener {
    private MyEventC myEventC;
    public ResetListener(MyEventC myEventC) {
        this.myEventC = myEventC;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Reset".equals(e.getActionCommand())) {
            myEventC.resetForm();
        }

    }
}

PS: It is about time that you start giving objects proper names otherwise you get confused with btn1 to btn25. Don't you think? resetButton or something along the line is more sensible and it does tell you exactly what is an object responsible for.

commented: That is neater +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.