0

Found solution

decided it was easier to simply make a method outside of the actionListener called chairPrice which can be incremented by a method called getItemPrice(). This has been used to calculate the total price of items and works 100%

6
  • 2
    new Chair()... you are welcome. Next time try to post a more specific question Commented Apr 29, 2012 at 16:26
  • @S92 What exactly is giving you problems about creating a new Chair? Is it actually creating the object, adding it to a list, or what? Commented Apr 29, 2012 at 16:33
  • @SJuan76 - i already know about using new Chair() thats not what I wanted I have updated my initial q Commented Apr 29, 2012 at 16:58
  • @Andres F. - I want to create multiple instances of the Chair glass to calculate prices of the chairs and work out overall price when using the GUI - I have included a pic of what the GUI looks like hopefully this will help illustrate my problem Commented Apr 29, 2012 at 17:00
  • Alternative layouts suggested here. Is this homework? Commented Apr 29, 2012 at 18:09

1 Answer 1

1

You need to use the Object.equals() method.

@Override
public void actionPerformed(ActionEvent buttonClick)
{
   if(buttonClick.getSource().equals(guiButtons[0])) //if user clicks on 'add chair'
   {
     Chair chair = new Chair();
   }
}

Edit in response to the OP's comment

I'm not exactly sure what you're wanting. myChair isn't the name of your chair. It's the name of the variable. It has no effect on Chair at all. If you want to make a new Chair object and have it available for the whole class, you're going to need to either add a new field variable or make a list of Chair.

public class GuiClass extends JPanel implements ActionListener
{
  List<Chair> chairs = new ArrayList<Chair>(Arrays.asList(new Chair()));
  Desk myDesk = new Desk();
  Table myTable = new Table();

  @Override
  public void actionPerformed(ActionEvent buttonClick)
  {
     if(buttonClick.getSource().equals(guiButtons[0])) //if user clicks on 'add chair'
     {
       chairs.add(new Chair());
     }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

If u look at the code I provided above im already using the following code in GuiClass: Chair myChair = new Chair(); //Object of chair But I want it to create a new object with a new name everytime buttonClick is guiButtons[0]
Hi thanks for your help up to now, im having a few problems trying to implement your solution can you please check my revised initial post thanks.
I think a better way to phrase what I want to know is how to be able to call a method from an object stored inside an ArrayList because what im currently doing is causing errors which say it can't find the methods such as setIdNum(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.