List244
asked on
Button Listener
import java.awt.*;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.*;
public class Chapter636 extends JApplet
{
private static final long serialVersionUID = 1L;
public void init()
{
Container Form = getContentPane();
Form.setLayout(new BorderLayout());
JButton Calculate = new JButton("Calculate");
Form.add(Calculate,BorderL ayout.SOUT H);
JPanel Panel = new JPanel();
final JTextField Base = new JTextField("Base",5);
final JTextField Exp = new JTextField("Exp",5);
Panel.add(Base,BorderLayou t.NORTH);
Panel.add(Exp,BorderLayout .SOUTH);
Form.add(Panel,BorderLayou t.NORTH);
Calculate.addActionListene r(
new ActionListener()
{
public void actionPerformed(ActionEven t e)
{
if (IsNum(Base.getText()) && IsNum(Exp.getText()))
{
int B,E;
B = Integer.parseInt(Base.getT ext());
E = Integer.parseInt(Exp.getTe xt());
JOptionPane.showMessageDia log(null,B +"^" + E
+"=" + Exp(B,E));
}
else
JOptionPane.showMessageDia log(null,
"Numeric input only! No negatives, no decimals.");
}
});
}
private boolean IsNum(String Str)
{
for(int i=0;i<Str.length();i++)
{
if ((int)Str.charAt(i) <48 || (int)Str.charAt(i) > 57)
return false;
}
return true;
}
private int Exp(int NBase,int NExp)
{
if(NExp <1)
return 1;
if (NExp >1)
{
NBase = NBase * Exp(NBase,NExp-1);
}
return NBase;
}
}
In the above completed application, how would I move the action listener to be its own class rather than disturbing the flow
of code in method init?
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.*;
public class Chapter636 extends JApplet
{
private static final long serialVersionUID = 1L;
public void init()
{
Container Form = getContentPane();
Form.setLayout(new BorderLayout());
JButton Calculate = new JButton("Calculate");
Form.add(Calculate,BorderL
JPanel Panel = new JPanel();
final JTextField Base = new JTextField("Base",5);
final JTextField Exp = new JTextField("Exp",5);
Panel.add(Base,BorderLayou
Panel.add(Exp,BorderLayout
Form.add(Panel,BorderLayou
Calculate.addActionListene
new ActionListener()
{
public void actionPerformed(ActionEven
{
if (IsNum(Base.getText()) && IsNum(Exp.getText()))
{
int B,E;
B = Integer.parseInt(Base.getT
E = Integer.parseInt(Exp.getTe
JOptionPane.showMessageDia
+"=" + Exp(B,E));
}
else
JOptionPane.showMessageDia
"Numeric input only! No negatives, no decimals.");
}
});
}
private boolean IsNum(String Str)
{
for(int i=0;i<Str.length();i++)
{
if ((int)Str.charAt(i) <48 || (int)Str.charAt(i) > 57)
return false;
}
return true;
}
private int Exp(int NBase,int NExp)
{
if(NExp <1)
return 1;
if (NExp >1)
{
NBase = NBase * Exp(NBase,NExp-1);
}
return NBase;
}
}
In the above completed application, how would I move the action listener to be its own class rather than disturbing the flow
of code in method init?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have been unable to accomplish either of these methods, can you show a more specific example?
Maybe an implementation to my code, or even a smaller example which implements a button with a
detached listener?
Maybe an implementation to my code, or even a smaller example which implements a button with a
detached listener?
>>I have been unable to accomplish either of these methods, can you show a more specific example?
What was the problem? There's really nothing more to it than the code in my last comment
What was the problem? There's really nothing more to it than the code in my last comment
ASKER
Well, take a look at this:
import java.awt.*;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.*;
public class Chapter636 extends JApplet
{
//Not sure on the below line, Eclipse put it there..
private static final long serialVersionUID = 1L;
public void init()
{
Container Form = getContentPane();
Form.setLayout(new BorderLayout());
JButton Calculate = new JButton("Calculate");
Form.add(Calculate,BorderL ayout.SOUT H);
JPanel Panel = new JPanel();
final JTextField Base = new JTextField("Base",5);
final JTextField Exp = new JTextField("Exp",5);
Panel.add(Base,BorderLayou t.NORTH);
Panel.add(Exp,BorderLayout .SOUTH);
Form.add(Panel,BorderLayou t.NORTH);
final JLabel Label = new JLabel("Result:");
Form.add(Label,BorderLayou t.CENTER);
Calculate.addActionListene r(this); //The method addActionListener(ActionLi stener) in the type AbstractButton is not
//is not applicapable for the arguments (Chapter636)
}
public void actionPerformed(ActionEven t e)
{
if (IsNum(Base.getText()) && IsNum(Exp.getText()))
{
int B,E;
B = Integer.parseInt(Base.getT ext());
E = Integer.parseInt(Exp.getTe xt());
Label.setText("Result: " + B + "^" + E +
" = " + Exp(B,E));
//JOptionPane.showMessageD ialog(null ,B +"^" + E
//+"=" + ;
}
else
JOptionPane.showMessageDia log(null,
"Numeric input only! No negatives, no decimals.");
}
private boolean IsNum(String Str)
{
for(int i=0;i<Str.length();i++)
{
if ((int)Str.charAt(i) <48 || (int)Str.charAt(i) > 57)
return false;
}
return true;
}
private int Exp(int NBase,int NExp)
{
if(NExp <1)
return 1;
if (NExp >1)
{
NBase = NBase * Exp(NBase,NExp-1);
}
return NBase;
}
}
There is an error commented above:
//The method addActionListener(ActionLi stener) in the type AbstractButton is not
//is not applicapable for the arguments (Chapter636)
And, all items of the form become inaccessible to the listener, would that be fixed by a more global-scope, or is there a better way?
import java.awt.*;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.*;
public class Chapter636 extends JApplet
{
//Not sure on the below line, Eclipse put it there..
private static final long serialVersionUID = 1L;
public void init()
{
Container Form = getContentPane();
Form.setLayout(new BorderLayout());
JButton Calculate = new JButton("Calculate");
Form.add(Calculate,BorderL
JPanel Panel = new JPanel();
final JTextField Base = new JTextField("Base",5);
final JTextField Exp = new JTextField("Exp",5);
Panel.add(Base,BorderLayou
Panel.add(Exp,BorderLayout
Form.add(Panel,BorderLayou
final JLabel Label = new JLabel("Result:");
Form.add(Label,BorderLayou
Calculate.addActionListene
//is not applicapable for the arguments (Chapter636)
}
public void actionPerformed(ActionEven
{
if (IsNum(Base.getText()) && IsNum(Exp.getText()))
{
int B,E;
B = Integer.parseInt(Base.getT
E = Integer.parseInt(Exp.getTe
Label.setText("Result: " + B + "^" + E +
" = " + Exp(B,E));
//JOptionPane.showMessageD
//+"=" + ;
}
else
JOptionPane.showMessageDia
"Numeric input only! No negatives, no decimals.");
}
private boolean IsNum(String Str)
{
for(int i=0;i<Str.length();i++)
{
if ((int)Str.charAt(i) <48 || (int)Str.charAt(i) > 57)
return false;
}
return true;
}
private int Exp(int NBase,int NExp)
{
if(NExp <1)
return 1;
if (NExp >1)
{
NBase = NBase * Exp(NBase,NExp-1);
}
return NBase;
}
}
There is an error commented above:
//The method addActionListener(ActionLi
//is not applicapable for the arguments (Chapter636)
And, all items of the form become inaccessible to the listener, would that be fixed by a more global-scope, or is there a better way?
Replace
>>public class Chapter636 extends JApplet
with
public class Chapter636 extends JApplet implements ActionListener
>>public class Chapter636 extends JApplet
with
public class Chapter636 extends JApplet implements ActionListener
ASKER
Okay, that works.
If I were to add another button, would there be a way to differentiate which was clicked?
If I were to add another button, would there be a way to differentiate which was clicked?
Yes
public void actionPerformed(ActionEven t e) {
Object initiator = e.getSource();
if (initiator == buttonA) {
//
}
else if (initiator == buttonB) {
//
}
}
public void actionPerformed(ActionEven
Object initiator = e.getSource();
if (initiator == buttonA) {
//
}
else if (initiator == buttonB) {
//
}
}
ASKER
That works, thanks.
:-)
1. Use a private named class that implements ActionListener
2. Make you applet implement ActionListener
The second is simpler