Loading Image in Java using JFrame - java

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.

Related

Switch between multiple JFrames

I'm relatively new to Java and I'm trying to make some kind of quiz. I created 3 JFrames, all in the same package. On my main frame, there are two buttons (one for the english version and the other one for the german version). I want to switch JFrames after pressing these buttons (so that I can, by pressing "English", see and interact with my english quiz frame). Looking it up didn't help me the slightest, because I'm not really experienced with it. Is it even possible to do it like this? If not, how could I do it?
The standard approach is to use the Card Layout, which allows you to use the same JFrame as you populate it with different things at different points in your application. So initially, your JFrame would show the loading screen, then the user presses a button and you load a new set of components without discarding the current JFrame you have. In some cases, you might also need to make some size adjustments.
It is difficult to say without seeing any code, but usually, what is done is that you do something like so:
new Frame(args);
this.dispose();
The code above assumes that the constructor of Frame takes care of launching and making the components visible. The this.dispose(); disposes of the current JFrame (assuming your class extends JFrame).
You have two buttons in your frame 1 right? So first, double click the button which says "English". Lets say the variable name for that button is jButton1. Inside that button type this.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
EnglishFrame eng = new EnglishFrame();
eng.setVisible(true);
}
Then double click the other button which says "German" (jButton2). Inside that type this.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
GermanFrame german = new GermanFrame();
german.setVisible(true);
}
this.dispose() - This will cause the jFrame window to close
Then you create an object of the other two forms. (In your case the form for English and Germany)
.setVisible(true) - This will show you the form.
Create a single Jframe window. After that create JPanels with all the compenents such as buttons, textfields and labels you want. Make sure the panel is the same size as your Jframe. Panel's work about the same as JFrame's, code wise.
This code will stitch everything together for you:
panel.setSize(Jframe.getSize()) //That wont
panel.add(button); //Just remember you need to add more code to position the buttons correctly.
//If you using netbeans builder:
//You just have to use this one line in the constructor/intialiser method
frame.add(panel); //This will add the panel to the Jframe/Window
//No need to add extra code for positioning.
If you want to swap between the panels. In the button press method, use this
frame.setContentPane(panel); //panel = panel you want to change too.
frame.repaint(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
frame.revalidate(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
When you first start the java application you have to set the content pane or else it will appear as a blank window.
frame.setContentPane(panel); //Starting Panel
frame.setVisible(true); //Make the frame visible
Sorry if the explanation is bad, I don't have enough time to explain it fully.

Can I add and draw a component through code in Swing (Java)?

I'm trying to add and draw buttons using merely my code. To be more specific, my goal is visualizing several buttons after another button has been pressed. This must be done programmatically because the number of buttons that have to be drawn depends on the user's input.
I've seen this piece of code in another thread and tried to implement it, but it didn't quite work:
// Adds a single button to a panel and paints it programmatically
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
JButton button = new JButton();
jPanel1.add(button);
jPanel1.revalidate();
jPanel1.repaint();
}
Any suggestions?
Thanks in advance.
Note: I'm using NetBeans 8.0.2.

How to load Jpanel in a Jframe using button click (Netbeans)

I'm using netbeans IDE and I created a Jframe with two Jpanels one is for Jbuttons and other one is for load another Jpanels to it when clicks those buttons.
I tried to do it from buttonclick action.
Jpanel2 j2=new Jpanel2();
JPanel1.add(j2);
j2.setVisible(True);
but this code is not working. I want to know how can I do this.
(I think this is also same as loading JinternelFrames)
Try to call revalidate() method.
Use a card layout and do it correctly.You can learn how to use card layout here

JLabel from one panel link to another JPanel

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();

Image not getting displayed on a JPanel

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.

Categories

Resources