Graphical anomalies when imports images and updating jLabels / jTextArea - java

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.

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

When updating JLabel, previous text stays, and old text is placed on top of it

So, I'm making a program, and on the program, it displays your health in this format:
Health : 82/82
When you take Damage, I ask the program to update the JLabel, and it would display that you took damage like this
Health: 60/82
The JLabel is a public variable, and is only being created once,
public JLabel UIHealth = new JLabel();
and from then on is updated with the below code.
Here is the code I use to update the UIHealth JLabel updating the text after an action occures:
UIHealth.setText("Health: "+Health+"/"+PlayerHealthBar.getMaximum()+"");
Is there a simpler way to display text that will be updated?
Does it matter that my Frame and ALL panels are set to be transparent to see the image behind it that acts as a HUD?
Here is the code I applied to everything that is transparent, but still interactive.
public Color Clear = new Color(0,0,0,0);
and I would of course call Clear when using the .setBackground component.
here is an image of after taking a bit of damage (4-5 hits) looks on the UI. (Take note of how the text just stacks on top of itself)
Thanks in advance for all your time.
Please let me know and ask me if something seems unclear, or you need other snippets of my program.
You have a JPanel that you are adding your label to. You need to call myPanel.setOpaque(false); after creating 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

Categories

Resources