4
\$\begingroup\$

I'm making a 2D RTS Map Editor and here came the moment to make a Palette which is just a popup frame showing tiles to be clicked on and then applied on the map.

I'm wondering which Java Elements to use for this palette? At the moment I have set a JFrame which will contain Jlabels which have their Icon set as some ImageIcon (which are the tiles of my game). (I have a List<ImageIcon> tiles which contains every tiles)

mainFrame.setLayout(new GridLayout(tiles.size() / 2, 2, 5,5));
    JLabel tilePanel;

    for (int i = 0; i < tiles.size() ; i++) {
        tilePanel = new JLabel();
        tilePanel.setIcon(tiles.get(i));
        mainFrame.add(tilePanel);
    }

Here's what I have:

JFrame with JLabels Icon set as my Tiles

However, I want to be able to click on these JLabels to be selected and so that I can use a function like getSelectedTile in my main JFrame to place them on the Map. But adding a mouseListener/Event to each and every JLabel doesn't seem right.

My questions are:

  • Am I going the right way? If so, how should listen to my JLabels to know which tile is selected.

  • What SHOULD I use if this isn't the way to go?

Thanks in advance! All suggestions will be taken into consideration :)

\$\endgroup\$
1
\$\begingroup\$

I'd go with an MouseListener per JLabel. Make a custom subclass of MouseListener which is parameterized with the tileId (or whatever you are using to identify the tiles).

Furthermore would not recommend using Swing for a game /mapeditor, we build a map editor using jmonkeyengine3 and Swing which is not working very well. You might want to look into JavaFX instead.

\$\endgroup\$
1
  • \$\begingroup\$ Would it make sense to use JavaFX TilePane ? @Eashi ? \$\endgroup\$
    – Pixel
    Jul 26 '18 at 22:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .