A simple problem

J

John Rainkin

I have a class called PigLatin that is provided. I have two text
areas. One you type in and the other will display the result, when the
button translate is pressed. How do I call in the method to the
PigLatin class? This is the code I have so far. You can email me at
(e-mail address removed). Thanks in advance.

//Applet to convert English to Pig Latin
//By Chris Lawson

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

public class SampleTextArea1 extends Applet
{
TextField status;
TextArea input, output;
Button translation;
Label label1, label2;

public void init() {

// Counts how many lines are in the text area
label1 = new Label("A English to Pig Latin conversion program
by Chris Lawson");
add(label1);
status = new TextField("Text area contains 1 newlines",40);
add(status);
input = new TextArea( "Type English here",8,40);
add(input);
label2 = new Label("The Pig Latin translation will appear
below");
add(label2);
output = new TextArea( "Translation appears here",8,40);
add(output);
status.setEditable(false);
output.setEditable(false);
translation = new Button ("Translate");
add(translation);

/**translation add action listiner
This is wear I am suppose to add action to the button
The Pig Latin class file should be used hear when the button
is pressed
When button is pressed is should convert it to Pig Latin
Converts to Pig Latin by using the PigLatin.class file
The code should look something like this

translation.addActionListener( new
ActionListener ()
{
public void actionPerformed ( ActionEvent ae )
{
String s = input.getText();
//this is were it needs to call to the PigLatin. class
input.setText();


}


return result;
);
}


*/



}
// An old focus method that displays the status of the Text Area
public boolean gotFocus( Event evt, Object what ) {
if (evt.target == input) {
status.setText("Type English below");
}
return super.gotFocus( evt, what );
}

public boolean lostFocus( Event evt, Object what ) {

// Have super handle character entry:
boolean result = super.lostFocus( evt, what );

if (evt.target == input) {
// Get string in input textfield:
String s = input.getText();

// Count newlines:
int len = s.length();
int newlines = 0;
for (int i = len; i --> 0; ) {
if (s.charAt(i) == '\n') ++newlines;
}

// Report linecount in output textfield:
status.setText("Text area contains " + newlines + " newlines");
}

return result;
}
}
 
C

Christian Bongiorno

I hate Annonymous Inner classes and avoid them whenever I can.

instead of using an inner class, in your class signature, put
"implements ActionListener" as such

public class SampleTextArea1 extends Applet implements ActionListener

Then, add a method called "public void actionPerformed(ActionEvent evt)"
You will become the ActionListener .. so, do
translation.addActionListner(this);

In your actionPerformed method you can sort through which button was
pressed with either evt.getSource() == translation (then again, you
only have 1 button, you could skip that all together).

In essence, while you could use an anonymous inner class to do the work,
they add unclean complexity to otherwise simple code.

However, the reason YOUR code isn't working is because you have the
syntax wrong. Working sample with and anonymous inner class:

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


public class Test {

public static void main(String[] args) {
new Test().test();
}

public Test() {

}
public void test() {
Frame f = new Frame();

Button b = new Button("Whatever");
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
outerFunction();
System.out.flush();
}

});
f.add(b);
f.setSize(100,100);
f.setVisible(true);
}

public void outerFunction() {
System.out.println("It worked");
}
}

Christian

<! just incase This becomes googleable!>
http://www.bongiorno.org/christian/resume.PDF
 
M

Michael Rauscher

Hi Christian,

Christian said:
I hate Annonymous Inner classes and avoid them whenever I can.

instead of using an inner class, in your class signature, put
"implements ActionListener" as such

public class SampleTextArea1 extends Applet implements ActionListener

Then, add a method called "public void actionPerformed(ActionEvent evt)"
You will become the ActionListener .. so, do
translation.addActionListner(this);

In your actionPerformed method you can sort through which button was
pressed with either evt.getSource() == translation (then again, you
only have 1 button, you could skip that all together).

In essence, while you could use an anonymous inner class to do the work,
they add unclean complexity to otherwise simple code.

Please, don't recommend this way. In this case, using if statements
means nothing else but implementing a bad design. It's not just a
performance question it's against OO principles.

You cannot simply add new functionality to your system without having to
modify present code.

The cleanest way is to declare separate classes; one for each action.

Bye
Michael
 
J

John Rainkin

How do I call to the class PigLatin?




Michael Rauscher said:
Hi Christian,



Please, don't recommend this way. In this case, using if statements
means nothing else but implementing a bad design. It's not just a
performance question it's against OO principles.

You cannot simply add new functionality to your system without having to
modify present code.

The cleanest way is to declare separate classes; one for each action.

Bye
Michael
 
J

John Rainkin

This is what I have now.

//Applet to convert English to Pig Latin
//By Chris Lawson

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

public class SampleTextArea1 extends Applet
{
TextField status;
TextArea input, output;
Button translation;
Label label1, label2;

public void init() {

// Counts how many lines are in the text area
label1 = new Label("A English to Pig Latin conversion program
by Chris Lawson");
add(label1);
status = new TextField("Text area contains 1 newlines",40);
add(status);
input = new TextArea( "Type English here",8,40);
add(input);
label2 = new Label("The Pig Latin translation will appear
below");
add(label2);
output = new TextArea( "Translation appears here",8,40);
add(output);
status.setEditable(false);
output.setEditable(false);
translation = new Button ("Translate");
add(translation);

/**translation add action listiner
This is wear I am suppose to add action to the button
The Pig Latin class file should be used hear when the button
is pressed
When button is pressed is should convert it to Pig Latin
Converts to Pig Latin by using the PigLatin.class file
The code should look something like this */

translation.addActionListener( new
ActionListener ()
{
public void actionPerformed ( ActionEvent ae )
{
String s = input.getText();
// This is wear the text need to be converted.
String result = PigLatin.encrypt( "text be be converted"
);
//this is were it needs to call to the PigLatin. class
input.setText();


}


return result;
);
}






}
// An old focus method that displays the status of the Text Area
public boolean gotFocus( Event evt, Object what ) {
if (evt.target == input) {
status.setText("Type English below");
}
return super.gotFocus( evt, what );
}

public boolean lostFocus( Event evt, Object what ) {

// Have super handle character entry:
boolean result = super.lostFocus( evt, what );

if (evt.target == input) {
// Get string in input textfield:
String s = input.getText();

// Count newlines:
int len = s.length();
int newlines = 0;
for (int i = len; i --> 0; ) {
if (s.charAt(i) == '\n') ++newlines;
}

// Report linecount in output textfield:
status.setText("Text area contains " + newlines + " newlines");
}

return result;
}
}
 
C

Christian Bongiorno

Michael,

You are absolutely right for large scale systems. This guy is just
getting started with ActionEvents and there is no reason to lead him
into a complicated discussion on how to design for such a system. In
truth, such a system could make use of a Command pattern and an
Interface. Further discussion beyond this would be offtopic to the post,
but feel free to debate it with me via email.

By the way, I hope that isn't truly your email address. Spammers WILL
harvest from this newsgroup

Christian
 
C

Christian Bongiorno

Michael,

You are absolutely right for large scale systems. This guy is just
getting started with ActionEvents and there is no reason to lead him
into a complicated discussion on how to design for such a system. In
truth, such a system could make use of a Command pattern and an
Interface. Further discussion beyond this would be offtopic to the post,
but feel free to debate it with me via email. reply to
<myfirstname>@<mylastname>.org

By the way, I hope that isn't truly your email address. Spammers WILL
harvest from this newsgroup

Christian
 
C

Christian Bongiorno

John,

In my previous post I showed you. You have to do NOTHING special.
You just directly call it. In your example you are making a static
reference to the method encrypt() in class PigLatin so make sure the
method has that Qualifier.

public static String encrypt(String input)

My Mistake before -- I wasn't paying attention but I thought your outter
class was the pig latin class. If you are still having problems then you
must also post the offending error

Christian
 
M

Michael Rauscher

Christian said:
Michael,

You are absolutely right for large scale systems. This guy is just
getting started with ActionEvents and there is no reason to lead him
into a complicated discussion on how to design for such a system. In
truth, such a system could make use of a Command pattern and an
Interface. Further discussion beyond this would be offtopic to the post,
but feel free to debate it with me via email.
Right.


By the way, I hope that isn't truly your email address. Spammers WILL
harvest from this newsgroup

It's a real address (used for spammers :) )

Michael
 
C

Chris Smith

Christian said:
You are absolutely right for large scale systems. This guy is just
getting started with ActionEvents and there is no reason to lead him
into a complicated discussion on how to design for such a system. In
truth, such a system could make use of a Command pattern and an
Interface. Further discussion beyond this would be offtopic to the post,
but feel free to debate it with me via email.

I disagree entirely. Further discussion is exactly appropriate to this
post, and likely to do more good than private email.

Comparing the two approaches (anonymous inner classes, versus having the
component class implement a listener interface), anonymous inner classes
have plenty of advantages:

1. You can have more than one of them, meaning that you aren't
artificially combining the code path for different actions and then
trying to sort them out again. Aside from your not having a good OO
means of doing the sorting, you are increasing the dependence of
different sections of code on the details of creating the GUI
components; your GUI components either need to become fields where they
didn't before, or you have to introduce a shared token such as a command
string. What you get is broadening of concerns that should be more
local, and that adds complexity to code.

2. By implementing a listener interface from a component class, you are
forced to add its methods to the public interface of the component
class. That's just wrong. Not so bad if you never intend to have other
code interact with the component, but if the component is exposed then
it's horrible. You've just projected your implementation details onto a
big screen to be seen by the whole world, and you'll eventually end up
breaking something when you change that implementation.

Compared to simply learning a language feature, the disadvantages are
enormous. Unfortunately, people frequently employ the technique of
having a component class implement a listener interface because they see
other people do it; and here in this thread, have it recommended to
them. So they get code that works but is a mess to deal with and risks
duplication, fragile changes, and lots of later maintenance. That's
bad, and to be avoided. Recommending bad design is not okay just
because you're talking to someone who doesn't know any better.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

John said:
I have a class called PigLatin that is provided. I have two text
areas. One you type in and the other will display the result, when the
button translate is pressed. How do I call in the method to the
PigLatin class?

John,

Since you're writing an inner class, you can simply call methods and
access fields of the outer class without any trouble. Just call them as
if they are in the current class, and it will work.

One word of caution, though; the 'this' keyword does refer specifically
to the inner class, so 'this.someMethod()' will only work for methods
defined in the inner class. But 'someMethod()', without the 'this.'
qualifier, will resolve to the closest implementation of someMethod,
possibly in the outer class.

So, assuming the outer class method you want is called translateText,
you'd say:
translation.addActionListener( new ActionListener () {
public void actionPerformed ( ActionEvent ae )
{
String s = input.getText();
input.setText(translateText(s));
}

return result;
);

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top