Hi everyone,

I have an ArrayList containing 40 or so JPanels for a board game developed in Netbeans with java - is it possible to have an event listener or something similar that will "monitor" the ArrayList for any of the JPanels being clicked, rather than having to create an event for every JPanel there?

Many thanks in advance! :)

5 Years
Discussion Span
Last Post by SCass2010
Featured Replies
  • 1
    Ezzaral 2,714   5 Years Ago

    [B]> or am I trying to do something that is either impossible or close to impossible[/B] Not at all. You should definitely use a loop to create all of those components. They only differ by the player index. You could also use a for-each style loop[CODE]for(Player p : players){...}[/CODE] Read More


So basically I could have something like

for (int i = 0; i < ArrayListOfJPanels.size(); i++)
    if (e.getSource() == myButton[i])
         // code here

and it would pick any mouse clicks on the JPanels within the ArrayList?



myPanel = new JPanel
MouseListener myMouseListener extends MouseAdapter {
for (int i = 0; i < myPanel.size(); i++) {

please check that, cos I write that from "memory by born instaled in my head"


Yeah starting to get an idea of what I need to do now thanks...

Still a bit confused but I'll give it a shot!! :S


OK one last, if slightly crazy, question...

I have an ArrayList containing up to 6 players. To display the details of the 6 players I have a set of textboxes and labels set up like this

        this.jTextField1.setText("£" + players.get(0).getBalance());
        this.jTextField2.setText("Pos : " + players.get(0).getBoardPosition());
        this.jTextField3.setText("PO : " + players.get(0).getPropertiesOwned().size());

        this.jTextField4.setText("£" + players.get(1).getBalance());
        this.jTextField5.setText("Pos : " + players.get(1).getBoardPosition());
        this.jTextField6.setText("PO : " + players.get(1).getPropertiesOwned().size());

        this.jTextField7.setText("£" + players.get(2).getBalance());
        this.jTextField8.setText("Pos : " + players.get(2).getBoardPosition());
        this.jTextField9.setText("PO : " + players.get(2).getPropertiesOwned().size());

        this.jTextField10.setText("£" + players.get(3).getBalance());
        this.jTextField11.setText("Pos : " + players.get(3).getBoardPosition());
        this.jTextField12.setText("PO : " + players.get(3).getPropertiesOwned().size());

        this.jTextField13.setText("£" + players.get(4).getBalance());
        this.jTextField14.setText("Pos : " + players.get(4).getBoardPosition());
        this.jTextField15.setText("PO : " + players.get(4).getPropertiesOwned().size());

        this.jTextField16.setText("£" + players.get(5).getBalance());
        this.jTextField17.setText("Pos : " + players.get(5).getBoardPosition());
        this.jTextField18.setText("PO : " + players.get(5).getPropertiesOwned().size());

While this works no problem when there are 6 players, any less it will crash :( is there a way of grouping together the textboxes for one player, then only adding them if a player exists?


for (int i = 0; i < playerList.size(); i++)
    playerDisplay pd = new playerDisplay();
    and so on.....

or am I trying to do something that is either impossible or close to impossible lol :P


> or am I trying to do something that is either impossible or close to impossible
Not at all. You should definitely use a loop to create all of those components. They only differ by the player index. You could also use a for-each style loop

for(Player p : players){...}


Sorry again but a bit stuck....

I have my Game now creating a JPanel containing the player details for each player in the game when it first starts

private void createPlayerDetails()
    {   //  For each player, create a PlayerDetails JPanel and add it to the ArrayList;
        //  Then add the current PlayerDetails JPanel to the GameStatistics JPanel
        for (int i = 0; i < this.players.size(); i++)
        {   this.playerDetailsPanels.add(new PlayerDetails(this.players.get(i)));

So the initial values for the player are loaded and displayed in the JPanel, but how do I update it each time something happens :S the only way I can think of is by removing the JPanels, recreating them and passing in the new Player object, but wouldn't that be wasting alot of resources, not to mention taking up alot doing it every second or so?


Doesn't your PlayerDetails panel keeping a reference to the Player object that it is displaying? There isn't any reason to remove the panel. Just update the info that it is showing by getting the values from the player object.


Yeah thats the thing I don't really understand but :S the PlayerDetails Panel has a variable Player that I set to the Player that is passed to it, but does that not just store a sort of screen shot of that Player objects details when it was passed?

Or is it just sending an address reference, so if i create an update method within the PlayerDetails panel, it would call the details from the address rather than the object it was pass (I hope some of this waffle I'm coming out with makes sense to you, not too sure I understand what I'm talking about!!!)

Edited by SCass2010: n/a


It is a "live" reference to the Player object itself - not a snapshot. Any data that your panel gets from the object will be current.


Ah yes, I get it now thanks finally got it working! Think I was just over complicating things in my head :P

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.