ActionListener is an Abstract class meaning that actionPerformed is intended
to be overridden. The first step is to override actionPerformed in your own
classes which should get rid of a couple of errors.
The second observation is that you should probably consider changing your
design. Making an encode action listener class implies that you are
extending the ActionListener abstraction. What you probably want to do is
1. create an EnCode/DecodeButton class which implements ActionListener.
2. override actionPerformed with a test like:
if (evt.getSource() instanceof Button) {
Button b =(Button)evt.getSource;
if (b.getLabel() == "Encode") {
abc;
xyz;
}
if (b.getLabel() == "Decode") {
abc;
xyz;
}
3. implement your new EncodeDecodeButton class in your Codec1 application.
If you really want to use an extra level of abstraction, as your code
currently suggests, then you will need to override the code in Codec1 as
follows:
// add Listeners
encodeBtn.addActionListener(new EncBtnAL() {
void actionPerformed() { abc; xyz; }
});
decodeBtn.addActionListener(new DecBtnAL() {
void actionPerformed() { abc; xyz; }
});
exitBtn.addActionListener(new ExiBtnAL() {
void actionPerformed() { abc; xyz; }
});
By further abstracting the Action Listener in this way, you are attempting
to use an inner class like a subroutine library which won't help you in the
long run.
I suggest using the first method. It will allow you to change your encoding
algorithm without having to affect the main Codec1 class.
Charles
>
> -------------------------------------------------
> // Codec1.java
>
> import java.awt.*;
> import java.awt.event.*;
> import java.io.*;
>
> public class Codec1 extends Frame {
>
> /** initialize fields */
> Label inFileLabel = new Label("Input file name");
> TextField inFileName = new TextField(64);
> Label outFileLabel = new Label("Output file name");
> TextField outFileName = new TextField(64);
> Button encodeBtn = new Button("Encode");
> Button decodeBtn = new Button("Decode");
> Button exitBtn = new Button("Exit");
> TextField status = new TextField(64);
> FileInputStream fis;
> BufferedInputStream bis;
> DataInputStream dis;
> FileOutputStream fos;
> BufferedOutputStream bos;
> DataOutputStream dos;
>
> /** main - just creates a new Codec1 object */
> public static void main(String[] args) {
> new Codec1();
> }
>
> /* no-arg constructor */
> public Codec1() {
>
> super("Codec1");
>
> // setup GUI
> setLayout(new FlowLayout());
> add(inFileLabel);
> add(inFileName);
> add(outFileLabel);
> add(outFileName);
> add(encodeBtn);
> add(decodeBtn);
> add(exitBtn);
> add(status);
>
> // add listeners
> encodeBtn.addActionListener(new EncBtnAL());
> decodeBtn.addActionListener(new DecBtnAL());
> exitBtn.addActionListener(new ExiBtnAL());
>
> enableButtons();
> this.setSize(400, 500);
> this.setVisible(true);
> this.show();
>
> }
>
> /** listener for Encode Button */
> public class EncBtnAL implements ActionListener {
> public void ActionPerformed(ActionEvent ae) {
> disableButtons();
> status.setText("Encoding...please wait");
> encode( inFileName.getText().trim(),
> outFileName.getText().trim() );
> enableButtons();
> }
> }
>
> /** listener for Decode Button */
> public class DecBtnAL implements ActionListener {
> public void ActionPerformed(ActionEvent ae) {
> //disableButtons();
> status.setText("Not implemented yet");
> // decode(inFileName.getText(),
> //outFileName.getText());
> //enableButtons();
> }
> }
>
> /** listener for Encode Button */
> public class ExiBtnAL implements ActionListener {
> public void actionPerformed(ActionEvent ae) {
> System.out.println("Exiting...");
> sleep(1);
> System.exit(0);
> }
> }
>
> public void encode(String inFileName, String outFileName) {
> try {
> fis = new FileInputStream(inFileName);
> bis = new BufferedInputStream(fis);
> dis = new DataInputStream(bis);
> fos = new FileOutputStream(outFileName);
> bos = new BufferedOutputStream(fos);
> dos = new DataOutputStream(bos);
> char c;
> while ((c = dis.readChar()) != -1) {
> //c = c ^ c;
> dos.writeChar((int) c);
> }
> status.setText("Encoding complete");
> sleep(1);
> }
> catch(IOException ioe) {
> System.out.println("IO error: " + ioe);
> status.setText("Encode failed");
> sleep(1);
> }
> finally {
> try {
> dis.close(); bis.close(); fis.close();
> dos.close(); bos.close(); fos.close();
> }
> catch(IOException ioe) {
> System.out.println("IO error: " + ioe);
> status.setText("Close(s) failed");
> sleep(1);
> }
> }
> }
>
> public void disableButtons() {
> encodeBtn.setEnabled(false);
> decodeBtn.setEnabled(false);
> exitBtn.setEnabled(false);
> }
>
> public void enableButtons() {
> encodeBtn.setEnabled(true);
> decodeBtn.setEnabled(true);
> exitBtn.setEnabled(true);
> }
>
> public void sleep(int seconds) {
> try {
> Thread.sleep(seconds * 1000);
> }
> catch(InterruptedException ie)
> {
> }
> }
> }
> -------------------------------------------------
>
>
> The errors :
>
> C:> javac Codec1.java 2>e
> C:> type e
>
> Codec1.java:47: inner class Codec1. EncBtnAL is an abstract class. It
> can't
> be instantiated.
> encodeBtn.addActionListener(new EncBtnAL());
> ^
> Codec1.java:48: inner class Codec1. DecBtnAL is an abstract class. It
> can't
> be instantiated.
> decodeBtn.addActionListener(new DecBtnAL());
> ^
> Codec1.java:59: inner class Codec1. EncBtnAL must be declared abstract. It
> does not define void actionPerformed(java.awt.event.ActionEvent) from
> interface java.awt.event.ActionListener.
> public class EncBtnAL implements ActionListener {
> ^
> Codec1.java:70: inner class Codec1. DecBtnAL must be declared abstract. It
> does not define void actionPerformed(java.awt.event.ActionEvent) from
> interface java.awt.event.ActionListener.
> public class DecBtnAL implements ActionListener {
> ^
> 4 errors
>
> __________________________________________________________________________
> _
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html