Reputation: 53
I'm suppose to create a game using java app. I have a few JLabel with images in a JPanel and I would like to link these JLabels from a JPanel to different JPanel. Is it possible to do so? As in when the Jlabel is being clicked, another page will appear.
Thanks in advance.
Upvotes: 0
Views: 838
Reputation: 168825
Cursor.HAND_CURSOR
.MouseListener
to each label.
mouseEntered()
, set the color of the link text to a different color to high-light it (like a browser would).mouseClicked()
, change cards in a CardLayout
to show the other components, or otherwise reveal the other components.Upvotes: 2
Reputation: 5134
You can share the JLabel by making it a singleton like class.
public class SharedJLabel {
private static JLabel imageLabel;
static {
//init the imageLabel here
}
public static JLabel getImageLabel() {
return imageLabel;
}
}
In your different JPanel classes, you can just use this class to use the shared JLabel:
SharedJLabel.getSharedImageLabel();
Upvotes: 0
Reputation: 23
You have to be more specific. You need to include things like what technologies you're using. Is this a web based project? Is it a stand alone application? And what specifically you're trying to do. And "link" has too broad of a meaning. That could mean a lot of different things. You have to be more specific in order to get the proper help.
Upvotes: 0
Reputation: 324128
Swing components can't be shared as they can only have a single parent.
However you can share the Icon of the label with another Swing component. So in your MouseListener you can use the getIcon()
method of the label you clicked on. Then you can add the icon to another component is the second panel using the setIcon(...)
method.
Upvotes: 2