Adding an image into a GUI made with Eclipse - java

I'm still pretty new to Java, and in our programming class we are working in a group. My part of the assignment is to insert an image at the top part of the GUI.
this is the code I have so far..
ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
JPanel.add(image, BorderLayout.NORTH);
but right under the .add part of the Jpanel.add is that red squiggly line telling me that I should change my Image to a component and when I do that it tells me to switch it back to an Image?? this is what I'm getting confused on why would it tell me to change it back if it won't use it the way it is now?
So I guess my question is what should I do to fix this problem?
Also exactly how would I position it, I know it goes into the layout spot in the North but will that be dead center? or does it start from the 0,0 top left and then pixel in?
Thank you in advanced!
(P.s. this is the path to the image file if it should be different please tell me otherwise it's fine -- Project 3/Images/EXTS.png)

An Icon is not a component. You need to add the Icon to a component like a JLabel:
ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
//JPanel.add(image, BorderLayout.NORTH);
JPanel.add(new JLabel(image), BorderLayout.NORTH);

Assuming that the path to the image is correct, you should use a JLabel to show the image. See How to Use Labels for more details.
I'd also consider using ImageIO to read the image instead of ImageIcon as ImageIO will throw an IOException if the image can't be loaded for some reason. See Reading/Loading an Image for more deatils
ImageIcon image = new ImageIcon(
ImageIO.read(getClass().getResource("/EXTS.png")));
JPanel.add(new JLabel(image), BorderLayout.NORTH);

Related

Java - Change IconImage in JLabel created with JScrollPane

Probably a stupid question. My apologies if so. I couldn't find what I needed with google searches, but probably because I'm not really sure how to word what I need.
I have this line of code:
JScrollPane scrlPane = new JScrollPane(new JLabel(imgIcon));
This does exactly what I need it to do. However, I'm not sure how to change the image in the imgIcon after it's created with this method.
Again, I apologize if this is a stupid question. I've tried to create a separate JLabel outside of this method and add it to the JScrollPane, but for some reason, it draws a gray box over the image. I know it's drawing the image, because I can see 1 pixel of the image loaded around the edge of the gray box.
Thanks for your time!
The easiest thing to do would be to keep a reference to your JLabel object, and to to use its setIcon(Icon icon) method to change it to your new ImageIcon object.
Or you can do somehting like this.
JViewport viewport = scrollPane.getViewport();
JLabel label = (JLabel)viewport.getView();
if (label != null) {
label.setIcon(newImgIcon);
}

Graphical anomalies when imports images and updating jLabels / jTextArea

I have an application that whenever I load in any image or update the JTextArea it always places the new object / text in the top left of the frame instead of simply updating whatever object it is supposed to be updating..
I am completely dumbfounded to why it is updating the screen in this way, does anyone have any hints or tips as to how to change this?
Below is the code for adding a jLabel into a jTabbedFrame
JLabel tempJLabel = new javax.swing.JLabel();
//tempJLabel.setLocation(1200,1200);
BufferedImage img = scaleImage(getStoredProductImage(photoDir[i]), 190); //scale down found image to whatever is needed
String filename = photoDir[i].getName();
Image tempImage = new Image(img,photoDir[i].getName(),photoDir[i],figureSaveDir(gtinTextBox.getText(), uidTextBox.getText()),tFrame,tempJLabel); //create ImageObj for later use
if(filename.length()>20){
tFrame.addTab(photoDir[i].getName().substring(15,19), tempJLabel);
tempJLabel.setIcon(new ImageIcon(tempImage.getImg()));
}
Unsure if I should also append information of the GUI construction...
i assume you just use it in any wrong way.
The ScreenShot of the Tab-Layout looks really strange to me.
But maybe i am wrong (i do not know what a JTabbedFrame is?)
When you mean a JTabbedPane, maybe you need to read this:
http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
before using it.

JLabel Icon resisting change

I have an icon for a JLabel which I can see the change for only once. When blank, a new set image for the below code works as it should. But after that, the image is stuck. No new image can replace it. When I use repaint on panelPainting without revalidate(), I get no pictures at all. Thats also weird.
Here is the code, (panelMain houses panelPainting)
//get image from somewhere
JLabel imageLabel = new JLabel();
Icon imageIcon = new ImageIcon(image);
imageLabel.setIcon(imageIcon);
panelPainting.setAlignmentX(JLabel.CENTER);
panelPainting.add(imageLabel); // default center section
//my insanity starts here
panelPainting.revalidate();
panelMain.remove(panelPainting);
panelMain.revalidate();
EDIT: I double checked that the image does change every time.
use JLabel.setIcon() as standard way, then there no reason to remove, modify and add a new JComponents on runtime
in some cases there is issue with repainting Icon in the JLabel (from external sources, www sites, etc.), then you have to call,
myIcon.getImage().flush();
myLabel.setIcon(myIcon);
use CardLayout with a few views, then any action is only to switch betweens cards
otherwise
have to call container.revalidate() and container.repaint(), as last code lines, one time, after all changes are done
for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame with JLabel contains ImageIcon / Icon created on fly

Add images to a jframe

I keep trying all this example code and none of it works, it always wants me to have a try/catch which means I have to ini. the variable anyway and I am tearing my hair out on the one, I have a jframe the exact size of my image, all I want is it to fill the entire jframe, also, could you make it so that if the image is transparent, that you can see though the entire jframe.
Thanks in advance
frame.add(new JLabel(new ImageIcon("path/to/image.png"));
There is a class called ImageIcon that can be used for images in Swing. Javadocs for ImageIcon
There are also many other ways to use ImageIcon.
From your saying that your example code want's you to use try / catch blocks, I'm guessing you are using the ImageIO class. It returns you with a BufferedImage which is not able to added to the frame. See the ImageIcon class here which you can use with the JLabel
frame.add(new JLabel(new ImageIcon("my_image.png"));
This should work if your image is outside of your jar. If it's included in the jar, use the classloader to get the resource for you.
URL url = getClass().getClassLoader().getResource("my_image.png");
ImageIcon icon = new ImageIcon(url);
frame.add(new JLabel(icon));
Hope this works.

How to add an Image to Form in java

I am designing a form in java using JDeveloper.
I am new to JDeveloper.
In JDeveloper tool I didn't found any option to directly add image to form like .Net.
And I don't know how to add image to form manually.
is there any other way to solve it out.
So please help me to solve it.
As simple as this :
image = ImageIO.read(new File(path));
JLabel picLabel = new JLabel(new ImageIcon(image));
Yayy! Now your image is a swing component ! add it to a frame or panel or anything like you usually do! Probably need a repainting too , like
jpanel.add(picLabel);
jpanel.repaint();
Don't know about JDeveloper but in code you have following possibilities:
Create an ImageIcon of the image then set that to a jLabel and add jLabel to your frame.
Override paintComponents() of your frame to draw image using Graphics in it. {Not sure about this}
Override paintComponent() of some panel or any other component to draw image using Graphics in it and then add that component to frame..
You can use Labels as Sanjay says.
also using layered pane you can use as background image.
You can try doing it this way:
ImageIcon image = new ImageIcon(getClass().getResource("imageName.png"));
JLabel lblImage = new JLabel(image);
line 1 of the code will get the image ensure that the image is in the same folder you are saving your work

Categories

Resources