I would appreciate any help (I am very much a beginner in Java) what is this error message and also do I use init() or main() for applet?
Thank you so much

//JavaAirlinesApplet.java       ERROR MESSAGE:
/*JavaAirlinesApplet.java:8: JavaAirlinesApplet is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
    public class JavaAirlinesApplet extends JApplet
           ^
    1 error*/

Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.text.NumberFormat;

public class JavaAirlinesApplet extends JApplet
  implements ActionListener
  {
  JTextField jtfMessage = new JTextField(20);  //created message,label,2radio, button
  JLabel jlblPassenger = new JLabel("Enter passenger name");

  public JRadioButton jrbCessna, jrb747;
  public JRadioButton jrbfirst, jrbsecond;
  public JRadioButton jrbVeggie, jrbFish, jrbBeef;

  JButton jbtPrintTicket = new JButton("Print Ticket");

   public void init()// static void main(String[] args)
     {
    JFrame JavaAirlines = new JFrame("JavaAirlines");
    JavaAirlines.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JavaAirlines.setTitle("JavaAirlines");
    JavaAirlines.setSize(500, 200);
    JavaAirlines.setVisible(true);
  }
  public void JavaAirlines()
   {                     //created message,label, 2 radio, button
    JPanel jpTextField = new JPanel();
    jpTextField.setLayout(new BorderLayout(1,2));
    jpTextField.add(
      new JLabel("Enter passenger name"), BorderLayout.WEST);
    jpTextField.add(jtfMessage, BorderLayout.CENTER);
    getContentPane().add(jpTextField, BorderLayout.NORTH);
    jtfMessage.setHorizontalAlignment(JTextField.RIGHT);

     JPanel jpRadioButtons = new JPanel();
     jpRadioButtons.setLayout(new GridLayout(2, 2));
     jpRadioButtons.add(jrbCessna = new JRadioButton("Cessna"));
     jpRadioButtons.add(jrb747 = new JRadioButton("747"));
     jpRadioButtons.add(jrbfirst = new JRadioButton("747 First Class"));
     jpRadioButtons.add(jrbsecond = new JRadioButton("747 Second Class"));

//JPanel jpRadioButtons = new JPanel();
      jpRadioButtons.setLayout(new GridLayout(3, 1));
      jpRadioButtons.add(jrbVeggie = new JRadioButton("747 Veggie"));
      jpRadioButtons.add(jrbFish = new JRadioButton("747 Fish"));
      jpRadioButtons.add(jrbBeef = new JRadioButton("747 Beef"));

    getContentPane().add(jbtPrintTicket, BorderLayout.SOUTH);

// Create a radio button group to group buttons
    ButtonGroup group1 = new ButtonGroup();  
     group1.add(jrbCessna);
              group1.add(jrb747);
              group1.add(jrbfirst);
     group1.add(jrbsecond);

    ButtonGroup group2 = new ButtonGroup();  
    group2.add(jrbVeggie);
    group2.add(jrbFish);
    group2.add(jrbBeef);

    // Register listeners            //created message,2 radio, button
     jtfMessage.addActionListener(this); 
 //jlblPassenger.addActionListener(this); //this line causes error

     jrbCessna.addActionListener(this);
     jrb747.addActionListener(this);
     jrbfirst.addActionListener(this);
            jrbsecond.addActionListener(this);

             jrbVeggie.addActionListener(this);
     jrbFish.addActionListener(this);
     jrbBeef.addActionListener(this);

     jbtPrintTicket.addActionListener(this);

   ButtonListener btListener = new ButtonListener();
   JPanel pa = new JPanel();
 }
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Print Ticket");  
        }
   }      
}

Edited 3 Years Ago by pyTony: fixed formating

Hi everyone,

This is happening because of the declaration of the class

public class JavaAirlinesApplet extends JApplet
implements ActionListener

You cannot implement ActionListener if you do not have this function in your class

public void actionPerformed(ActionEvent e)
{
}

You have to insert the above function in your applet class or not implement it at all
and have a alternative system for receiving listeners for your applet

I hoped this has helped you

Yours Sincerely

Richard West

THanks for your help but no matter where I move braces or that function I get errors. Can you tell me exactly where to put it and what is wrong with my braces?

I really appreciate it Kittie

THanks for your help but no matter where I move braces or that function I get errors. Can you tell me exactly where to put it and what is wrong with my braces?

I really appreciate it Kittie

This is an simple example on implementing the ActionListener interface.

class MyClass implements ActionListener
{
	
	public void actionPerformed(ActionEvent e)
	{
		JOptionPane.showMessageDialog(null, "ACTION!");
	}
}

By looking at your code, this method actionPerformed will be called whenever the controls fire an action. I'm not sure if this is what you want. What are you trying to do anyways?

Also, you have a method called public void JavaAirlines(). This is not the constructor and I don't see it being called anywhere in your code.

For more help, www.NeedProgrammingHelp.com

Hi everyone,

Try this kitty

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.text.NumberFormat;

public class JavaAirlinesApplet extends JApplet
implements ActionListener
{

public void actionPerformed(ActionEvent e)
{

}

JTextField jtfMessage = new JTextField(20); //created message,label,2radio, button
JLabel jlblPassenger = new JLabel("Enter passenger name");

public JRadioButton jrbCessna, jrb747;
public JRadioButton jrbfirst, jrbsecond;
public JRadioButton jrbVeggie, jrbFish, jrbBeef;

JButton jbtPrintTicket = new JButton("Print Ticket");

public void init()// static void main(String[] args)
{
JFrame JavaAirlines = new JFrame("JavaAirlines");
JavaAirlines.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JavaAirlines.setTitle("JavaAirlines");
JavaAirlines.setSize(500, 200);
JavaAirlines.setVisible(true);

}
public void JavaAirlines()
{ //created message,label, 2 radio, button
JPanel jpTextField = new JPanel();
jpTextField.setLayout(new BorderLayout(1,2));
jpTextField.add(
new JLabel("Enter passenger name"), BorderLayout.WEST);
jpTextField.add(jtfMessage, BorderLayout.CENTER);
getContentPane().add(jpTextField, BorderLayout.NORTH);
jtfMessage.setHorizontalAlignment(JTextField.RIGHT);

JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(2, 2));
jpRadioButtons.add(jrbCessna = new JRadioButton("Cessna"));
jpRadioButtons.add(jrb747 = new JRadioButton("747"));
jpRadioButtons.add(jrbfirst = new JRadioButton("747 First Class"));
jpRadioButtons.add(jrbsecond = new JRadioButton("747 Second Class"));

//JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(3, 1));
jpRadioButtons.add(jrbVeggie = new JRadioButton("747 Veggie"));
jpRadioButtons.add(jrbFish = new JRadioButton("747 Fish"));
jpRadioButtons.add(jrbBeef = new JRadioButton("747 Beef"));

getContentPane().add(jbtPrintTicket, BorderLayout.SOUTH);

// Create a radio button group to group buttons
ButtonGroup group1 = new ButtonGroup(); 
group1.add(jrbCessna);
group1.add(jrb747);
group1.add(jrbfirst);
group1.add(jrbsecond);

ButtonGroup group2 = new ButtonGroup(); 
group2.add(jrbVeggie);
group2.add(jrbFish);
group2.add(jrbBeef);

// Register listeners //created message,2 radio, button
jtfMessage.addActionListener(this); 
//jlblPassenger.addActionListener(this); //this line causes error

jrbCessna.addActionListener(this);
jrb747.addActionListener(this);
jrbfirst.addActionListener(this);
jrbsecond.addActionListener(this);

jrbVeggie.addActionListener(this);
jrbFish.addActionListener(this);
jrbBeef.addActionListener(this);

jbtPrintTicket.addActionListener(this);

ButtonListener btListener = new ButtonListener();
JPanel pa = new JPanel();
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Print Ticket"); 
}
} 
}

I compiled it and it compiles without errors but you really need to clean up your'
code and try to go to Sun's site and learn about the different listeners they have. The tutorials over there are very good

Yours Sincerely

Richard West

Hi Thanks for all your help but now that I got it to compile, the applet does not initialize.

The program is supposed to be an Applet GUI with labels, radio buttons, etc for flight and food choices for passengers.

Thanks again, Kittie

//JavaAirlinesApplet3.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.text.NumberFormat;

 public class JavaAirlinesApplet3 extends JApplet implements ActionListener
  {
  public void actionPerformed(ActionEvent e)
   {
  JOptionPane.showMessageDialog(null, "Your flight is booked"); 
   }

   JTextField jtfMessage = new JTextField(20); 
    JLabel jlblPassenger = new JLabel("Enter passenger name");
    public JRadioButton jrbCessna, jrb747;
   public JRadioButton jrbfirst, jrbsecond;
    public JRadioButton jrbVeggie, jrbFish, jrbBeef;
    JButton jbtPrintTicket = new JButton("Print Ticket");

    public void init()
     { 
     JFrame JavaAirlines = new JFrame("JavaAirlines");
     JavaAirlines.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     JavaAirlines.setTitle("JavaAirlines");
     JavaAirlines.setSize(500, 200); JavaAirlines.setVisible(true);
   }
     public void JavaAirlines3() 
     { 
      JPanel jpTextField = new JPanel(); 
     jpTextField.setLayout(new BorderLayout(1,2));
      jpTextField.add( new JLabel("Enter passenger name"), BorderLayout.WEST);
      jpTextField.add(jtfMessage, BorderLayout.CENTER);
      getContentPane().add(jpTextField, BorderLayout.NORTH);
      jtfMessage.setHorizontalAlignment(JTextField.RIGHT);
      JPanel jpRadioButtons = new JPanel();
      jpRadioButtons.setLayout(new GridLayout(2, 2));
      jpRadioButtons.add(jrbCessna = new JRadioButton("Cessna"));
      jpRadioButtons.add(jrb747 = new JRadioButton("747"));
      jpRadioButtons.add(jrbfirst = new JRadioButton("747 First Class"));
      jpRadioButtons.add(jrbsecond = new JRadioButton("747 Second Class"));
       //JPanel jpRadioButtons = new JPanel();
      jpRadioButtons.setLayout(new GridLayout(3, 1));
      jpRadioButtons.add(jrbVeggie = new JRadioButton("747 Veggie"));
      jpRadioButtons.add(jrbFish = new JRadioButton("747 Fish"));
      jpRadioButtons.add(jrbBeef = new JRadioButton("747 Beef"));
      getContentPane().add(jbtPrintTicket, BorderLayout.SOUTH); 

     ButtonGroup group1 = new ButtonGroup();
      group1.add(jrbCessna); group1.add(jrb747);
      group1.add(jrbfirst); group1.add(jrbsecond);

     ButtonGroup group2 = new ButtonGroup();
      group2.add(jrbVeggie); group2.add(jrbFish);
      group2.add(jrbBeef);

      // Register listeners 
       jtfMessage.addActionListener(this);
         //jlblPassenger.addActionListener(this); //this line causes error
        jrbCessna.addActionListener(this);
        jrb747.addActionListener(this);
        jrbfirst.addActionListener(this);
        jrbsecond.addActionListener(this);
        jrbVeggie.addActionListener(this);
        jrbFish.addActionListener(this);
        jrbBeef.addActionListener(this);
        jbtPrintTicket.addActionListener(this);
        ButtonListener btListener = new ButtonListener();
        JPanel pa = new JPanel();
           }
         class ButtonListener implements ActionListener
          {
           public void actionPerformed(ActionEvent e)
            {
             System.out.println("Print Ticket");
     } } }

Edited 3 Years Ago by pyTony: fixed formating

I believe, the method public void JavaAirlines3() is not being called. This method is the one that puts all the radio buttons, etc. into your frame.

Call this method from your init() method.

For more help, www.NeedProgrammingHelp.com

Thanks NPH
I put JavaAirlines3(); at the end of the init() but it still says Applet not initialized.
I really appreciate your help, Kittie

How are you running the applet? Are you using the applet viewer or using IE? Try to use the applet viewer and it should give some error messages.

To use the applet viewer, go into the command prompt and go to the directory of your .html file and write

appletviewer nameOfHtmlFile.html

replace nameOfHtmlFile with the correct name. Post any errors here.

For more help, www.NeedProgrammingHelp.com

Thanks NPH you have been a great help
I am using the applet viewer and this is the error message:
java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM)
Kittie

change the line

JavaAirlines.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

to

JavaAirlines.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Why are you showing a blank JFrame with the applet?

For more help, www.NeedProgrammingHelp.com

Hi, I tried chaning that close and got this error message:
java.lang.IllegalArgumentException: adding container's parent to itself

What do you mean by blank Jframe?

Thanks again, kittie

Try to comment the line that causes that error. You are adding the wrong thing to the applet or the jframe

The blank jframe is created here

public void init()
{ 
JFrame JavaAirlines = new JFrame("JavaAirlines");
JavaAirlines.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JavaAirlines.setTitle("JavaAirlines");
JavaAirlines.setSize(500, 200); JavaAirlines.setVisible(true);
}

When I ran this, a blank jframe popped up (this one) and the main applet window with all the controls showed in the back.

For more help, www.NeedProgrammingHelp.com

Hi,
I'm sorry I am really new at this (6 weeks) and I just don't understand what you are saying. I don't know what to comment out. I thought I had to set up this frame for all the buttons and text boxes? How can I just take it out - don't I need that init() line - where would that go?

Thanks again so much, Kittie

When you ran the program you got an error message. It also gave you a line number on where the error occurred. This line should be commented (put // in front of the line). Run the program. Notice the result. This error means you are adding the wrong thing to applet. Analyze that line and realize what is wrong with it.

The radio buttons and textboxes are all being added to the Applet. If this is just what you need then you don't need to create the JFrame below

JFrame JavaAirlines = new JFrame("JavaAirlines");
JavaAirlines.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JavaAirlines.setTitle("JavaAirlines");
JavaAirlines.setSize(500, 200); JavaAirlines.setVisible(true);

You should re-post your code if you have made changes to it so we can help you out better.

For more help, www.NeedProgrammingHelp.com

NPH,

I can't thank you enough - that was the problem - as soon as I removed the JFrame it worked and I just had to play around with the format.

Thanks again and have a great night,
Kittie

This article has been dead for over six months. Start a new discussion instead.