JLabel Icon resisting change - java

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

Related

When updated, JLabel changes order and comes to the front

I'm trying to make a visual novel using Java Swing, and so far it has been going smoothly. I'm using a JLabel that is attached to a JPanel to make the background image, and it starts good: image of a background behind a textbox + choice buttons, but when I try to update the image using Game.backgroundLabel.setIcon(newBackground);, it brings the background label to the very front: image of a background in front of the textbox, with only one of the three buttons showing. I'm new to the entirety of Java Swing, and mediocre at Java, but I'll try to include only the code that I believe to be relevant.
FROM THE MAIN CLASS (Game.java):
Container con;
JPanel backgroundLabel;
JLabel backgroundLabel;
// Creates background
backgroundPanel = new JPanel();
backgroundPanel.setBounds(0, 0, 800, 600);
backgroundLabel = new JLabel();
backgroundPanel.add(backgroundLabel);
con.add(backgroundPanel);
FROM A DIFFERENT CLASS (Story.java):
ImageIcon inCarBackground = new ImageIcon("C:\\Users\\kiwid\\eclipse-workspace\\FatuiBusiness\\backgrounds\\inCarBackground.png");
ImageIcon scaraSprite = new ImageIcon("C:\\Users\\kiwid\\eclipse-workspace\\FatuiBusiness\\sprites\\ScaraSprite.png");
This first update works, and the JPanel stays in the back where I added it in the Game.java class, as shown in the first image.
public void gameStart() {
position = "inCar00";
Game.nameLabel.setText("");
Game.mainTextArea.setText("My name is Lumine. I’ve recently sided with the Fatui - a huge mafia\norganization - through Scaramouche, who I befriended in Teyvat\nUniversity. I’m on a mission for the Fatui to steal a Blue Diamond\nring that once belonged to the Tsaritsa herself.");
Game.backgroundLabel.setIcon(inCarBackground);
Game.choice1.setText("");
Game.choice2.setText(">");
Game.choice3.setText("");
}
However, this update seems to change the order of the JPanel, and brings it to the front of the screen.
public void inCar01() {
position = "inCar01";
Game.mainTextArea.setText("Here with me are Tartaglia, Scaramouche, and Mona. Or, as I’m\nsupposed to call them here, Childe, Balladeer, and the Prophesizer.");
Game.backgroundLabel.setIcon(scaraSprite);
}
I've searched online for a solid 2 hours, but can't find anything good. I've read a little bit about JLayeredPane, but it seems like that revolves around user input, and when I try to use it, it says that I cannot add a JPanel to it.
Thank you all so much in advance! Please tell me if there's any other code I need to include, or if there's any code that I did not need to include (so I know for the next time I need help here).

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

Adding an image into a GUI made with Eclipse

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

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.

Why will image not add when JFrame has .setSize

I'm a somewhat novice programmer and I'm have some trouble adding an image to my frame. While I know how to add images generally, this specific case it does not work.
public class Tutorial extends JFrame{
Tutorial(){
JFrame frame = new JFrame("ImageTutorial");
frame.setVisible(true);
frame.setSize(750,850);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
ImageIcon image = new ImageIcon(getClass().getResource("Green Block.png"));
JLabel imagelabel = new JLabel(image);
imagelabel.setBounds(10, 10, 75, 75);
imagelabel.setOpaque(true);
frame.add(imagelabel);
Now, I've located the problem but I don't understand 'why' its a problem. When I remove
frame.setSize(750,850);
the image shows, but when its there it doesn't. How can the frame's size impact the image showing and how can I get around it?
Just curious, logically, what makes you think a frame should be visible before you add any components? Logically speaking, wouldn't it seem right to add your components first, then make the frame visible. It's like displaying a painting in an art gallery even before the painter has painted anything on it. It just makes no sense. I highly doubt setting the size has anything to do with it. IF you don't set the size of the frame, then the frame appears as small as possible. When you resize the frame, it causes a repaint, then showing the label you add. But generally, you want to always set frame visible after all you components are added, to avoid this problem.
Side note: You should stay away from null layouts. You need to learn to use Layout Managers and let them do the dynamic sizing and locating for you.

Categories

Resources