Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:11:04+00:00 2026-06-08T00:11:04+00:00

hello all been using this site for ages as a lurker but come across

  • 0

hello all been using this site for ages as a lurker but come across this problem and cant find the solution Anywhere…

i have actionlistener in a inner class that just wont respond… any chance you guys could look over my beginner coding and see what you think?

sorry theres so much code here the only bit im having trouble with is the ‘AddPet’ class and its inner class ‘AddEle’ and the System.exit method. When the window ‘choose pet type dialog’ opens nither of these buttons will work… hope i have all the terminology right as im totally self taught and just starting out.

thanks for your time

eddy

package petshopwk1;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.* ;
import javax.swing.*;
import java.util.Scanner;
import javax.swing.JOptionPane ;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JList;
import java.awt.event.*;

public class CurrentPet2 extends JFrame {
private JButton setName, setDOB, setSold, getName, getDOB, getSold, addPet, changePre,           changeNext, exit, setCurrentPet, getFeel;
private JLabel cPetLabel ;

private Shop theShop = new Shop("Brighton");

private JButton exitButton, Ele, MF, Pet;
String temp = "temp" ;


Date dob = new Date();
public Pet CurrentPetObj = new Pet("", "", true, dob) ;

int cPetIndex ;


DefaultListModel model = new DefaultListModel();
public JList list = new JList(model);


public CurrentPet2 (String cTitle)
            {
super(cTitle) ; 






Pet Alburt = new Pet("Alburt", " 09 APRIL 2009", true) ;
Pet Eddy = new Pet("Eddy", " 13 JANUARY 1982", false) ;
Pet Paul = new Pet("Paul", " 21 DECEMBER 1956", true) ;
Pet Sian = new Pet("Sian", " 11 SEPTEMBER 1988", true) ;
Pet Morrise = new Pet("Morrise", " 26 MARCH 1996", false) ;
Pet Betty = new Pet("Betty", " 31 JANUARY 1962", false) ;
Elephant Ele = new Elephant ("Ele", "13 JANUARY 1982", true, 12) ;
MF NOW = new MF ("NOW", "8 JULY 2012", false, 11, "Rocking") ;


theShop.addPet(0, Alburt);
theShop.addPet(1, Eddy);
theShop.addPet(2, Paul);
theShop.addPet(3, Sian);
theShop.addPet(4, Morrise);
theShop.addPet(5, Betty); 
theShop.addPet(6, Ele);
theShop.addPet(7, NOW);


Container contentPane ;
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());



cPetLabel = new JLabel("Current Pet Mode");

setCurrentPet = new JButton("Choose current Pet");
setName = new JButton("Set Name") ;
setDOB = new JButton("Set DOB") ;
setSold = new JButton("Set Sold");
getName = new JButton("Get Name") ;
getDOB = new JButton("Get DOB") ;
getSold = new JButton("Get Sold") ;
addPet = new JButton("Add Pet");
changePre = new JButton("Change to Previous") ;
changeNext = new JButton("Change to Next");
getFeel = new JButton("FEEL?");
exit = new JButton("EXIT");

exit.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e )
{System.exit(0); }}
);



setCurrentPet.addActionListener(new ChoosePetName());
setName.addActionListener(new       setPetName()) ;
setDOB.addActionListener(new        setPetDOB());
setSold.addActionListener(new       setIsSold());
getName.addActionListener(new       getPetName()) ;
getDOB.addActionListener(new        getPetDOB());
getSold.addActionListener(new       getIsSold());
addPet.addActionListener(new        addPet());
getFeel.addActionListener(new       printFeel());

list.setListData(Shop.thePets.toArray());

list.validate();

changePre.addActionListener(new     changePetPre());
changeNext.addActionListener(new    changePetNext());


contentPane.add(setCurrentPet);
contentPane.add(addPet);
contentPane.add(setName);
contentPane.add(setDOB);
contentPane.add(setSold);
contentPane.add(getName);
contentPane.add(getDOB);
contentPane.add(getSold);
contentPane.add(getFeel) ;

contentPane.add(changePre);

contentPane.add(changeNext);
contentPane.add(exit);
contentPane.add(new JScrollPane(list));


list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
JList list = (JList) evt.getSource();
if (evt.getClickCount() ==2 )
{
int cPetIndex = list.locationToIndex (evt.getPoint());
CurrentPetObj = Shop.thePets.get(cPetIndex) ;
JOptionPane.showMessageDialog(CurrentPet2.this,               CurrentPetObj + "@ Index " + cPetIndex);
}
}
} 
                                                         );

setLayout(new FlowLayout());
add(exit);



this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setSize(550, 400);
this.setVisible(true);

}



class ChoosePetName implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

String cPetSearch  = JOptionPane.showInputDialog(null, "Please Eneter Pet to work on...") ;

 CurrentPetObj = Shop.findPet(cPetSearch) ;

 int cPetIndex = Shop.thePets.indexOf(CurrentPetObj);
 list.setSelectedIndex(cPetIndex);

 JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj + "@ Index " + cPetIndex); 

 }
 }


class setPetName implements ActionListener
{
public void actionPerformed(ActionEvent e )
{
String nName = JOptionPane.showInputDialog(null, "Please input new Pet name...");

CurrentPetObj.setName(nName);
JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj) ;


}
}

class setPetDOB implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String nDOB = JOptionPane.showInputDialog(null, "Please input new Pet DOB... (in format 26 MARCH 1996");
CurrentPetObj.setDOB(nDOB) ;
JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj);
}
}

class setIsSold implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
boolean s = Boolean.parseBoolean(JOptionPane.showInputDialog(null, "Please input if new Pet is sold..."));
CurrentPetObj.setIsSold(s);
JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj.toString());
}
}



class getPetName implements ActionListener
{
public void actionPerformed(ActionEvent e )
{
JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj.getName());

}
}

class getPetDOB implements ActionListener
{
public void actionPerformed(ActionEvent e )
{
JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj.getDOB()) ;
}
}

class getIsSold implements ActionListener
{
public void actionPerformed(ActionEvent e )
{
JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj.getIsSold());
}
}

    -----------------------------------------------------------------------
    its just between this and the following line i have the problem. the actionListener responds fine in the earlier class but just cant get it working with the AddPet class and therefore wont call the system.exit or AddEle...

any help would be great:)

***class AddPet implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Container contentPane ;
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

JFrame f = new JFrame("Choose pet type dialog");
f.setSize(300, 100);
Container content = f.getContentPane();


Ele = new JButton("Add Elephant");
exitButton = new JButton("EXIT");


exitButton.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent L )
{System.exit(0); }}
                             );

Ele.addActionListener(new        AddEle());

content.setLayout(new FlowLayout()); 
content.add(new JButton("Add Elephant"));
content.add(new JButton("Add Pet"));
content.add(new JButton("EXIT"));

contentPane.add(Ele);
contentPane.add(exit);

f.setVisible(true);


contentPane.add(MF);
contentPane.add(Pet);
}
}


class AddEle implements ActionListener
{
public void actionPerformed(ActionEvent L )
{

String nName  = JOptionPane.showInputDialog(null, "Please Enter new Pet name...") ;
String nDate  = JOptionPane.showInputDialog(null, "Please input new pet DOB (in format 20 MARCH 2001");

boolean s     = Boolean.parseBoolean(JOptionPane.showInputDialog(null, "Please input if     sold or not? ( true / false"));

int nSize = Integer.parseInt(JOptionPane.showInputDialog(null, "Please input Elephant SIZE..."));

cPetIndex = Integer.parseInt(JOptionPane.showInputDialog(null, "Please input INDEX to place new Pet..."));

Pet CurrentPetObj = new Elephant(nName, nDate, s, nSize) ;
theShop.addPet(cPetIndex, CurrentPetObj) ;
list.setListData(Shop.thePets.toArray());
model.setElementAt(CurrentPetObj, cPetIndex);

JOptionPane.showMessageDialog(CurrentPet2.this, CurrentPetObj + " @ Index " + cPetIndex); 



}

}

}***

class changePetPre implements ActionListener
{
public void actionPerformed(ActionEvent e )
{
int cPetIndex = Shop.thePets.indexOf(CurrentPetObj);

--cPetIndex;
CurrentPetObj = Shop.thePets.get(cPetIndex);
list.setSelectedIndex(cPetIndex);
JOptionPane.showMessageDialog(CurrentPet2.this, "Current Pet is now == " + CurrentPetObj + "@ Index " + cPetIndex); 

}
}

class changePetNext implements ActionListener
{
public void actionPerformed(ActionEvent e )
{
int cPetIndex = Shop.thePets.indexOf(CurrentPetObj);
++cPetIndex;
CurrentPetObj = Shop.thePets.get(cPetIndex);
list.setSelectedIndex(cPetIndex);

JOptionPane.showMessageDialog(CurrentPet2.this, "Current Pet is now == " + CurrentPetObj   + "@ Index " + cPetIndex); 

}
}

class printFeel implements ActionListener
{
public void actionPerformed(ActionEvent e )
{
JOptionPane.showMessageDialog(CurrentPet2.this, "Current feel is now == " + MF.getFeel()); 
}
}


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:11:05+00:00Added an answer on June 8, 2026 at 12:11 am

    You are adding the action listeners to buttons you don’t add to the frame (the ones you do add are new instances, and don’t have the action listeners)

    Try:

    content.add(Ele); 
    content.add(exitButton); 
    

    in AddPet

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello all this seems to be my problem I have a table in mysql
Problem Hello all! I have this code which takes my jpg image loops through
I have been using these files all day... And then suddenly this is now
Have been struggling all day trying to make this simple example work using socket.io.
Hell All, So I have been facing this behavior for quite some time and
I've been reading through quite a few articles on the 'this' keyword when using
I was reading the ctypes tutorial, and I came across this: s = Hello,
Hello all i got a mail code like this body = Dear & cName
Hello I've been trying to display progress using the annular determinate for the last
I have been looking all over the Internet for an answer to this question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.