Jump to content

simple event handler...

- - - - -

  • Please log in to reply
5 replies to this topic

#1
fabiniyoung

fabiniyoung

    Newbie

  • Members
  • PipPip
  • 11 posts
public class CalcLogic implements ActionListener{




@Override
public void actionPerformed(ActionEvent e) {


}

}
i have a JPanel that contains a JLabel object .
i also have a button that subscribed to this event handler (that is working as it should).
how could i set the text to the label from here everytime someone click to the button?

thanks in advance...

#2
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Isuru
 */
public class Frame implements ActionListener{

    private JFrame frame = new JFrame("Well");
    private JLabel scream = new JLabel("I'm Screaming");
    private JLabel cry = new JLabel("I'm Crying");
    private JButton screamBtn = new JButton("Stop Screaming Now!");
    private JButton cryBtn = new JButton("Stop Crying Now!");
        
    public Frame(){
        buildGUI();
        btnAction();
    }

    public void btnAction(){
        screamBtn.addActionListener(this);
        cryBtn.addActionListener(this);
    }
        
    private void buildGUI(){
        frame.add(scream);
        frame.add(cry);
        frame.add(screamBtn);
        frame.add(cryBtn);

        frame.setLayout(new FlowLayout());
        frame.setSize(200, 150);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
    }

    public static void main(String args[]){
        Frame App = new Frame();

    }

    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource() == screamBtn){
            scream.setText("I will stop sreaming!");
        }else{
            cry.setText("I'm not crying any more!");
        }
    }

}

Here, I wrote a complete program. But all you need to learn is,
public void actionPerformed(ActionEvent ae) {
        if(ae.getSource() == screamBtn){
            scream.setText("I will stop sreaming!");
        }else{
            cry.setText("I'm not crying any more!");
        }
    }

How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
Lost!

#3
fabiniyoung

fabiniyoung

    Newbie

  • Members
  • PipPip
  • 11 posts
thanks alot for your reply,
but what's wrong with this one:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;


public class CalcLogic extends JPanel implements ActionListener{

JLabel calcAnswer=new JLabel();
public CalcLogic() {
//calcAnswer.setText("hello");
add(calcAnswer);

}

@Override
public void actionPerformed(ActionEvent e) {
calcAnswer.setText("your answer");
System.out.println("what's wrong...?");

}

}


mport java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JPanel;


public class ButtonArea extends JPanel {

public ButtonArea() {
JButton add=new JButton("add");
JButton minus=new JButton("minus");
JButton multi=new JButton("times");
JButton div=new JButton("devide");
JButton power=new JButton("exp");
CalcLogic listener=new CalcLogic();
setLayout(new GridLayout(7,1));
add(add);
add(minus);
add(multi);
add(div);
add(power);
add.addActionListener(listener);
minus.addActionListener(listener);
multi.addActionListener(listener);
div.addActionListener(listener);
power.addActionListener(listener);
}

}

what's confuse me is that by testing with system.out.println(); it works. which means that the eventhandler does get informed of the action event. but why wont it set the text of the label???

ps: this is just part of the whole project, there are more classes...but these two are the ones relevant for this issue.

thanks again for the help!

#4
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
Check again this block!
@Override
public void actionPerformed(ActionEvent e) {
calcAnswer.setText("your answer");
System.out.println("what's wrong...?");

}

}

How the program knows which button got clicked. And use something like below,
public void actionPerformed(ActionEvent ae) {
        if(ae.getSource() == screamBtn){
            scream.setText("I will stop sreaming!");
        }else{
            cry.setText("I'm not crying any more!");
        }
    }
getSource() method use to tell which button clicked by user.
Lost!

#5
fabiniyoung

fabiniyoung

    Newbie

  • Members
  • PipPip
  • 11 posts
yeah i get what u mean,, but it looks like it doesn t matter what button i click as long as it is registered as a source of event on CalcLogic object. in this case, when i put ##system.out.println("test event");## in the event handler method, whatever button i click i get the same string "test event" written again and again (for every button i click...).

i decided to put everything in the same classe and behold now its working"i did not have to tell hich button is the source"

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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


public class ButtonArea extends JPanel implements ActionListener{

JLabel calcAnswer=new JLabel();

JButton add=new JButton("add");
JButton minus=new JButton("minus");
JButton multi=new JButton("times");
JButton div=new JButton("devide");
JButton power=new JButton("exp");

public ButtonArea() {

setLayout(new GridLayout(7,1));
add(add);
add(minus);
add(multi);
add(div);
add(power);
add(calcAnswer);
add.addActionListener(this);
minus.addActionListener(this);
multi.addActionListener(this);
div.addActionListener(this);
power.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent arg0) {
calcLogic();

}

private void calcLogic(){
calcAnswer.setText("your answer!!");
}

}

however my aim was to move this method "calcLogic" in its own class and have actionPerformed(ActionEvent arg0) use an object of this new classe to run one of its logic methods.....

in few words i m trying to achieve a program with high cohesion and specialised responsibility for each class.

but ofcourse this is not as easy as i want it to be. help help help lol

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You can split your classes again :)

Use .setActionCommand(String command)
JButton add=new JButton("add");
add.setActionCommand("add");

@Override
public void actionPerformed(ActionEvent e) {
  if(e.getActionCommand.equals("add")){
     calcLogic(add);
  } else if( e.getActionCommand.equals("minus")){
     calcLogic(minus);
  }
   .....
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users