Results 1 to 13 of 13
  1. #1
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default Determining ActionListener

    Hi,

    I have a class containing two inner classes each with an actionlistener, for two separate buttons. One draws cars, the other draws trucks.
    Java Code:
    /** Creates a control panel. */
        public void createControlPanel() {
            JPanel buttonPanel = new JPanel();
            JButton addCarButton = new JButton("Add Car");
            JButton addTruckButton = new JButton("Add Truck");
    
            class CarMoveListener implements ActionListener {
                public void actionPerformed(ActionEvent event) {
                    panel.add();
                }
            }
    
            class TruckMoveListener implements ActionListener {
    			public void actionPerformed(ActionEvent event) {
    				panel.add();
    			}
    		}
    
            addCarButton.addActionListener(new CarMoveListener());
            addTruckButton.addActionListener(new TruckMoveListener());
            buttonPanel.add(addCarButton);
            buttonPanel.add(addTruckButton);
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        }
    
        private BumperCarPanel panel;


    I have a separate class that implements the drawing of these two objects (each of the objects are contained within an array for each object type)

    Java Code:
    **
         *      Draws the graphics context.
         *      @param g the graphics context
         */
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            
            for (int i = 0; i < bumpercars.size(); i++) {
                BumperCar c = (BumperCar)bumpercars.get(i);
                c.draw(g2);
            }
            for (int i = 0; i < bumpertrucks.size(); i++) {
    			BumperTruck t = (BumperTruck)bumpertrucks.get(i);
    			t.draw(g2);
    		}
        }

    Basically, I'm trying to implement a conditional statement before my two For Loops, to ascertain which of the two ActionListener's is calling the paintComponent method, so that the correct For Loop can be run through to draw the correct object.

    I'm stumped how to reference either of these?!

    Can someone please point me in the right direction? I appreciate this is probably fairly simple to do.. I'm overlooking something here.

    Cheers

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    That ActionListener doesn't call the paint method. It gets called when an event happens. Then you are expected to do what you need to do from within that method.

    What is the statement below supposed to do?
    Java Code:
    panel.add();
    Maybe you could have a private enum for cars trucks, etc. And change the value of the enumeration when the ActionListener is called then check the value of the enumeration during the paint method to see which object needs to be drawn.

    Since you only have two options you could use a boolean as well. However, this would limit you down the road if you decided to add something like motorcycle.

    Mr. Beans

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    We may be better at helping you if you post
    1) a paragraph or 3 describing in a bit of detail just what you're trying to do here keeping the descriptions as clear and simple as possible. This would not decribe the inner workings of the program but what you're trying to get the user to experience, and
    2) a small compilable program that illustrates your problem. By knowing the issues in 1) well, we may be able to suggest a way around your current problem perhaps by ways you haven't tried yet.

    Best of luck.

    edit: my guess is that your addCarButton will want to add a new BumperCar to the BumperCars list, and then call repaint on whatever panel is drawing the cars and trucks. Similar for the addTruckButton, except that it will add a BumperTruck to the BumperTrucks list. If so, then I'm not sure that I'd change the paintComponent method, but rather I'd let it draw all the cars and trucks.
    Last edited by Fubarable; 05-25-2009 at 08:49 PM.

  4. #4
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Hi,

    Yes, basically I have a JFrame containing two buttons, addBumperCars and addBumperTrucks. Both buttons have their own listener defined that calls the add() method of the carPanel class which contains two arraylists: one for bumpercar objects, and one for bumpertruck objects. The carPanel class also has a paintComponent() method to paint the objects on the panel.

    A click on either of these buttons by the user is meant to add one new object of the relevant type (add bumpercars adds a bumpercar) to an arrayList containing objects of the respective type (bumpercars or bumpertrucks). Where I'm struggling is how to determine in the paintComponent() method which of the two lists requires redrawing. For example:

    Java Code:
     public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
    
            for (int i = 0; i < bumpercars.size(); i++) {
                BumperCar c = (BumperCar)bumpercars.get(i);
                c.draw(g2);
            }
           // for (int i = 0; i < bumpertrucks.size(); i++) {
    		//	BumperTruck t = (BumperTruck)bumpertrucks.get(i);
    		//	t.draw(g2);
    		//}
    What I want to do with the above is determine which type of object / event source is being triggered so that the relevant for loop accessed.

    I can't really post more code as this is for an assessment. I appreciate your advice though. :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by siamino View Post
    What I want to do with the above is determine which type of object / event source is being triggered so that the relevant for loop accessed.
    Unless I'm mistaken, I don't think that you really want to do this. Have you tried what I suggested in my post above as I think that is your true solution.

    Let me see if I have this straight: if I press the addCarButton, then it should instruct the GUI to draw another bumper car, correct? But the big question is, will the GUI still draw all the other cars and trucks that have been added previously? If so, then you'll want to leave both for loops in the paintComponent method unchanged. You'll only want the actionlistener's to add to the correct arraylist and call repaint (as I indicated in my post above). If you only want the GUI to paint either cars or trucks and not both, then you'll need to have a boolean switch variable in your class that the paintComponent will read and that the actionlisteners will set, and this will determine which arraylist gets iterated and drawn.
    Last edited by Fubarable; 05-25-2009 at 10:21 PM.

  6. #6
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Could you elaborate what you mean? I can't see how else the two event listeners can be differentiated by the external carPanel class in order to draw the relevant object to the frame.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by siamino View Post
    Could you elaborate what you mean? I can't see how else the two event listeners can be differentiated by the external carPanel class in order to draw the relevant object to the frame.
    Please first answer my questions, otherwise I won't know how to answer you.

  8. #8
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Ah ok! Apologies, I didn't see the edit to your last post before I replied. Yes, adding a new car or truck (via the relevant addButton) adds a new car/truck to the existing window containing any car / trucks previously added.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Then if you had your way, you pressed the addCar button, and you somehow disabled the truck portion of the loop in the paintComponent method, all the trucks previously drawn would disappear. Are you sure that you'd want to do that?

    My other question still unanswered: have you tried adding to the arraylists from within the action listeners?

  10. #10
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    I see what you're saying - NO that I definitely don't want to do that!

    The arraylists are declared in a separate class containing the paintComponent method. How can I reference them from within the action listeners? - I haven't tried this, but would do if I could. Wouldn't the compiler then complain that it cannot find the arraylist variables when I compile the carPanel class?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Then you'll have to use public methods of the drawing class. Hopefully it will have (or is going to have) an addCar() or addTruck() method or some-such. And hopefully your listener class will have a reference to the drawing class so it can call these methods on it.

  12. #12
    siamino is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    It does. I have an public add() method in the carPanel drawing class. This adds a new object to either the car or trucks arraylist, then calls the repaint() method. I shall try that out.

    Mind if I ask one further question on action listeners? Can I throw an exception from inside the action listeners? I have a save method I need to call - again from the same carPanel class. The compiler complains that:

    unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

    Can this be thrown from an action listener?

    Thanks for all your help, it is appreciated.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The last part may be best answered with: "try it and see what happens". Let us know please how it turns out! Much luck.

Similar Threads

  1. Need Help with determining the problem in my Code
    By KillingKiller in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 12:24 AM
  2. Determining the readiness of another program
    By jmHoekst in forum New To Java
    Replies: 10
    Last Post: 06-19-2008, 08:56 AM
  3. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM
  4. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:24 PM
  5. Determining Midi Length
    By Usagi in forum New To Java
    Replies: 0
    Last Post: 12-08-2007, 11:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •