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.
Set the cursor for the label to Cursor.HAND_CURSOR.
Add a MouseListener to each label.
On mouseEntered(), set the color of the link text to a different color to high-light it (like a browser would).
On mouseClicked(), change cards in a CardLayout to show the other components, or otherwise reveal the other components.
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.
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.
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();
Related
so i have a very basic java application, there is a panel and inside that is a button. When a user clicks the button, i want a picture to come up in another panel in the same form.
I searched up ways to load images from web/from my folder and this is the code i've come up with:-
private void buttonActionPerformed(java.awt.event.ActionEvent evt)
{
ImageIcon icon = new ImageIcon("URL-of-the-image");
panel2.setIcon(icon);
}
~~what panel 2 looks like~~
where am i going wrong?
it says that the method "setIcon()" is causing a problem but i don't know any other method to do this job. Please Guide!
If panel2 is a JPanel, it doesn't have a setIcon method.
Use a JLabel instead , it has such a method.
I want to add JLabel to JPanel (say VisualPanel,which is inside a JFrame) based on the action-performed event (JButton) occurred on another JPanel(say JobPanel) (separate class but add to the JFrame).
How to get the VisualPanel object inside the JobPanel to add the JLabel?
I tried importing the JFrame into JPanel and get the VisualPanel instance but somehow i am getting into infinite recursion.
My question is my design approach correct?. If not how should i go about it?
if my design is correct any suggestion in the right direction is highly appreciated. Thank you.
Give VisualPanel a CardLayout with two panels: one panel that has the JLabel and one that doesn't. Give your JButton an ActionListner that will show() the card you want. Then you can use setSelectedIndex() to select the VisualPanel tab. See the tutorial for examples.
I have found that there are many similar topics here, but my problem is something more complicated.
Background of my problem:-
I have a JFrame called Main. On this JFrame I have two buttons and one JPanel called WorkingPanel. Then I have another JPanel(called PlayerPanel) but this one is a seprate file (as a class).
Now I want that when I click a button, it should change WorkingPanel to PlayerPanel. I have wrote following code.
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
WorkingPanel = new PlayerPanel();
System.out.println(WorkingPanel.getName());
WorkingPanel.revalidate();
WorkingPanel.repaint();
WorkingPanel.setVisible(true);
Window.revalidate();
Window.repaint();
}
Please guide me, Thanks.
I have found that there are many similar topics here, but my problem is something more complicated.
On the contrary, your description is of a rather basic problem that is very easily solved by using a CardLayout. I suggest that you do this now. If you had it in place your method could be as simple as:
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
cardLayout.show(cardPanel, WORKING_PANEL);
}
where cardLayout is your CardLayout variable, cardPanel is the JPanel that displays the "cards" that displays the swapping JPanels, and WORKING_PANEL is a String constant that you used when you added your WorkingPanel instance to the cardPanel.
Point 2:
Don't use a MouseListener on a JButton as it won't behave correctly. For instance, if you disable the button via setEnabled(true) the button won't truly be disabled. Instead use an ActionListener with JButtons as the tutorials will show you. That is what they are for.
Edit
For examples of CardLayout-using GUI's, please check out:
getting Jcomponent from other class changes frame size
Java CardLayout Main Menu Problem
Change size of JPanel using CardLayout
Java CardLayout JPanel moves up, when second JPanel added
Java swing; How to toggle panel's visibility?
Clear components of JFrame and add new componets on the same JFrame
gui multiple frames switch
JLabel displaying countdown, java
This one is unusual in that it uses a CardLayout and has one panel fading into the other panel:
CardLayout showing two panels, flashing.
You could use CardLayout instead of that approach. You will be able to switch between different panels very easy and efficiently. It's also wort of mentioning that use of CardLayout is less verbose approach.
Use a CardLayout, containing your two panels, but only showing one at a time. The CardLayout is documented, with examples, in the Swing tutorial.
I'm trying to set a variable to be a new JPanel and then add it once a button is pressed, but it is not working and I don't know why.
code:
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
remove(scriptPanel);
scriptPanel = new GemPanel();
add(scriptPanel);
validate();
repaint();
pack();
}
GemPanel is just a JPanel class I made. When I press the next button, it re-sizes the frame to be as small as possible and nothing actually happens. If I re-size it to normal, the original scriptPanel is still there.
What gives?
Instead of trying to remove and add entire panels, a better, less problem prone approach would be to use a CardLayout that will allow to swap views. You can see more at How to use Cardlayout
Also, by the looks of your method signature, it seems you're using the Netbeans builder too. You may also want to take a look at How to Use CardLayout with Netbeans Gui Builder
class Deal implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dl.setDeck();
dl.shuffle();
dl.firstDraw(pl);
for(Card c:pl.showHand())
panelplay.add(new JLabel(c.getImageIcon()));
panelplay.validate();
}
}
This is an event handler for a Jbutton. The method pl.showHand() returns a ArrayList of a user defined class 'Card'. Inserting a println() inside the loop shows the print, so the code is being executed but the Panel panelplay isnt showing card Images.
What about the existing labels on the panel? You don't remove them. I'm guessing you are using a FlowLayout and the labels just get added to the end of the panel so you don't see them.
So one solution is to use panel.removeAll() before adding the labels back to the panel. I then use:
panel.revalidate();
panel.repaint();
Or the better option as suggested earlier is to not replace the labels but just replace the Icons using the setIcon() method.
Do as Gilbert says, look at the Swing Tutorial part that concerns Labels.
JLabel has the following methods...
void setIcon(Icon)
Icon getIcon()
Also look at the SplitPaneDemo It does exactly what you want, you can even run it with JNLP to see.
You don't want to add the JLabel in the ActionListener.
You want to use an already added JLabel setText() method in the ActionListener.
You define all the Swing components once, when you create the GUI.