Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to tell a JPanel, that one of the JPanels it contains has been clicked?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to tell a JPanel, that one of the JPanels it contains has been clicked?

    Hi, I am trying to make a tile puzzle game. I am having trouble in getting the tiles (JPanels) to move.

    I have a Puzzle class that extends JPanel, and this contains either a 3*3 or a 4*4 grid of tiles, which are instances of my PuzzlePanel class, which also extends JPanel, these PuzzlePanels have parts of an overall image drawn on them...so that correctly arranged, they show the image. The instance of the Puzzle class is also contained in another JPanel.
    PuzzlePanel implements MouseListener, on mouseEntered() a PuzzlePanel, it is highlighted with a rectangle around it, and unhighlighted on mouseExit().

    Is there a way that I can inform the Puzzle class that one of the instances of PuzzlePanel has been clicked, through using mouseClicked() in PuzzlePanel?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to tell a JPanel, that one of the JPanels it contains has been clicked?

    There is probably some fancy way of doing it, but the easiest way I can think of is to make the PuzzlePanels have a backwards reference to the Puzzle class (I assume each PuzzlePanel is only in one Puzzle at a time). Then have a variable in the Puzzle class for "selected" and make the selected PuzzelPanel update that variable when it's selected.
    Alternatively, you can add a "selected" boolean to the PuzzlePanel, and simply make the Puzzle class recurse through its child PuzzlePanel objects to find a PuzzlePanel with "selected" set to "true"...
    Just brain storming. There are probably "better" solutions.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to tell a JPanel, that one of the JPanels it contains has been clicked?

    Thank you for your reply.

    Quote Originally Posted by aussiemcgr View Post
    There is probably some fancy way of doing it, but the easiest way I can think of is to make the PuzzlePanels have a backwards reference to the Puzzle class (I assume each PuzzlePanel is only in one Puzzle at a time). Then have a variable in the Puzzle class for "selected" and make the selected PuzzelPanel update that variable when it's selected.
    Yes, each PuzzlePanel is in only one Puzzle.
    I'm not sure I know how to do this, though I understand the idea.
    Is there an example you could direct me to, or if you have the time could you give an example of the code to do this?


    Quote Originally Posted by aussiemcgr View Post
    Alternatively, you can add a "selected" boolean to the PuzzlePanel, and simply make the Puzzle class recurse through its child PuzzlePanel objects to find a PuzzlePanel with "selected" set to "true"...
    Just brain storming. There are probably "better" solutions.
    I think I had part of this in my head earlier, but was unsure how to get the Puzzle class to go through the PuzzlePanels after a PuzzlePanel had been clicked, though now the Swing.Timer class comes to mind, but that seems unnecessary.
    Am I missing something, that would get the Puzzle class go through the PuzzlePanels in this case?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to tell a JPanel, that one of the JPanels it contains has been clicked?

    Do me a favor:
    Add a MouseListener to Puzzle, and print something out when mouseClicked(), to see if it picks it up.

    If it does pick it up, we can listen to the mouse clicks from the Puzzle instead of the individual PuzzlePanels, and then determine which PuzzlePanel was clicked. This way we don't have to implement any timers or anything and we can keep it event-driven (like the Java Gods intended).
    If it doesn't pick up the mouse click, we can set up our own event trigger to send back up to Puzzle.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to tell a JPanel, that one of the JPanels it contains has been clicked?

    Tested it out.
    It will only pick it up if the MouseListener is taken off the PuzzlePanel or if I click in the area of a PuzzlePanel that is not visible.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to tell a JPanel, that one of the JPanels it contains has been clicked?

    I presume your Puzzle contains references to the PuzzlePanel's (if not directly, it does so via its JPanel tree of children), so you can just add a MouseListener (from the Puzzle class) to each of the child PuzzlePanels. Note in so doing, the fired event will have it's data relative to the PuzzlePanel who fired the event (for instance, getPoint() refers to the event location in the child PuzzlePanel responsible for the event).

  7. The Following User Says Thank You to copeg For This Useful Post:

    Ludo (March 20th, 2014)

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to tell a JPanel, that one of the JPanels it contains has been clicked?

    Ok, so the MouseListener doesn't work on Puzzle if one is on PuzzlePanel? That sucks... Ok, we will be a bit more inventive and do something a bit more "fun".
    There are easier ways to do this, but I'll teach you a nifty little trick. We'll create our own event-handling.
    To do this, we need a new interface:
    import java.util.EventListener;
    import java.util.EventObject;
    public interface ClickedPuzzleEventListener extends EventListener {
    	/**
    	 * @param event the being received
    	 */
    	public void clickPuzzleEventRecieved(EventObject event);
    }
    Since we just need the source of the event, we can use a normal old EventObject. If you wanted to include more information in the event's payload, you would create a class which extends from EventObject and add the info for your payload.
    Now, ClickedPuzzleEventListener is our new Listener, so we want Puzzle to implement it.
    In the Puzzle class, modify it to include:
    public class Puzzle extends JPanel implements ClickedPuzzleEventListener {
    	private PuzzlePanel selected;
    	...
    	@Override
    	public void clickPuzzleEventRecieved(EventObject event) {
    		selected = (PuzzlePanel)event.getSource();
    		// Do something now that a PuzzlePanel has been selected
    	}
    	...
    }
    And in your PuzzlePanel class, modify it to include:
    public class PuzzlePanel extends JPanel ... {
    	private Puzzle puzzleParent;
     
    	public PuzzlePanel(Puzzle par) {
    		super();
    		puzzleParent = par;
    	}
    	...
    	/* where ever your mouseClicked method is, perhaps in a MouseListener
    	 * implementation or something
    	 */
    	mouseClicked(MouseEvent e) {
    		puzzleParent.clickPuzzleEventRecieved(new EventObject(PuzzlePanel.this));
    		// if your MouseListener is its own inner class, you need to say: PuzzelPanel.this
    		// if your PuzzlePanel just implements MouseListener, you can just say: this
    	}
    ...
    }

    And...that should pretty much do the trick. You COULD have skipped the listener and just made the PuzzlePanel set the "selected" variable in Puzzle, but that would not notify Puzzle that a change has been made. With the use of our custom Listener, we can set the "selected" variable AND notify Puzzle when we do, so we can then perform whatever actions in Puzzle which you need to do when the selected PuzzlePanel changes.
    Tell me what makes sense and what doesn't.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Ludo (March 20th, 2014)

  10. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to tell a JPanel, that one of the JPanels it contains has been clicked?

    Thank you, copeg. That actually gets me going again.

    aussiemcgr, thank you for that. I will try this out, and see if I can get it working too. It should help me out if I run into any other troubles.
    Will be useful knowing how to create a custom Listener.

    Thank you both.

Similar Threads

  1. JPanel within JPanel causing odd stretching behavior.
    By mstabosz in forum AWT / Java Swing
    Replies: 7
    Last Post: September 2nd, 2013, 06:32 AM
  2. When a button is clicked: get my Data!!!
    By aStudentofJava in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 15th, 2012, 08:45 AM
  3. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  4. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM