Let's say I'm building a chess app with swing. I'm using an array of JLabels to represent the checkerboard (each has its appropriate icon set as a lightly/dark shaded box). I've created another array of JLabels to hold the icons of the chess pieces, but I'm not familiar enough with swing to know how to implement them to display on top of the checkerboard. Anyone know of any techniques?
Asked
Active
Viewed 1,648 times
0
-
`JLabel` is just another component, it's capable of containing other components, but you will need to set it's layout manager, maybe use `BorderLayout` – MadProgrammer Feb 17 '17 at 06:17
-
1As an [example](http://stackoverflow.com/questions/33115320/having-a-jlabel-on-top-of-another-jlabel-that-has-an-image/33115415#33115415) – MadProgrammer Feb 17 '17 at 06:21
-
1See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556) for a starting point. – Andrew Thompson Feb 17 '17 at 07:18
1 Answers
0
I have written a small example that builds a window and two JLabels on top of each other.
Note that the grey.jpg and pawn.png images have 128x128 size and the pawn one has transparent background (this way I prevent the background of the pawn image from hiding the grey rectangle box).
Here is the ChessFrame class that builds the window and adds the components:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ChessFrame extends JFrame {
private JPanel panel;
private JLabel greyBox;
private JLabel pawn;
public ChessFrame() {
super();
/* configure the JFrame */
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addComponents() {
panel = new JPanel();
greyBox = new JLabel(new ImageIcon("images/grey.jpg"));
pawn = new JLabel(new ImageIcon("images/pawn.png"));
/* add the pawn inside the grey box (we have to set a layout for the grey box JLabel) */
greyBox.setLayout(new BorderLayout());
greyBox.add(pawn);
/* add grey box to main JPanel and set its background to white so we observe the result better */
panel.add(greyBox);
panel.setBackground(Color.WHITE);
this.getContentPane().add(panel);
}
@Override
public void setVisible(boolean b) {
super.setVisible(b);
}
}
And here is a Main class that creates a ChessFrame object and shows the window:
public class Main {
public static void main(String[] args) {
ChessFrame chessFrame = new ChessFrame();
chessFrame.addComponents();
chessFrame.setVisible(true);
}
}

Valy
- 573
- 2
- 12
-
1) While it is possible to add components to labels, it has a downside in that the label is no longer able to provide an appropriate preferred size to a layout manager. A better approach would be to set the BG color to either the label that displays the icon, or a panel that the label is added to. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 17 '17 at 22:37
-
Went with this implementation, because Bryan asked about JLabels on top of JLabels :) so I wanted to go on the same path as the question. Also, thanks for the suggestion with the hot links to images. – Valy Feb 19 '17 at 01:20
-
1*"I wanted to go on the same path as the question."* See [Is “Don't do it” a valid answer?](http://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer). (Yes it sure is, if the OP is taking the wrong approach and there is a better solution.) Concrete examples can be seen in my comment on this question (a comment in reply to the question itself), as well as [my answer](http://stackoverflow.com/a/42312098/418556) to [Moving JPanels using TimerTask](http://stackoverflow.com/q/42310947/418556) - the (accepted) answer neither moves panels nor uses `TimerTask`. – Andrew Thompson Feb 19 '17 at 01:27