When updated, JLabel changes order and comes to the front - java

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

Related

Panels not being added to the GUI

I recently started learning Java and aam writing a gui for a character creation sheet for personal use. I believe i have gotten all of the back end stuff working properly, but I can't test it outside of the console because I can't get the GUI to display anything other than a gray box.
I have a main class that creates the JFrame and calls the constructors for my other character classes to do all of the work, but for some reason, nothing shows up in the GUI. I'm not getting any errors or anything and I have spent several hours researching possible solutions to no avail.
My code:
The main method:
public static void main(String[] args) throws IOException
{
JFrame main = new JFrame();
JMenuBar menu = new MenuBar();
JPanel character = new CreatorGUI();
main.getContentPane().add(character);
main.add(menu);
main.setJMenuBar(menu);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setSize(10000, 10000);
main.pack();
main.setVisible(true);
}
The CreatorGUI:
public CreatorGUI() throws IOException
{
JPanel container = new JPanel();
JPanel stats = makeStats();
JPanel mods = makeMods();
JPanel character = makeChar();
container.add(mods);
container.add(stats);
container.add(character);
add(container);
}
I am trying to get the GUI working in its most basic form to make sure everything is working before I go back and refine the GUI, but I have hit a wall.
Any help is much appreciated!
main.getContentPane().add(character);
main.add(menu);
main.setJMenuBar(menu);
Using main.getContentPane().add(...) is the same as main.add(...). That is the component gets added to the content pane.
The default layout manager of a JFrame is a BorderLayout.
When you don't specify a constraint the component is added to the CENTER. However only a single component can be added to the CENTER so the "menu" is replacing the "character" panel.
Get rid of the main.add(menu) statement. It should only be added to the menubar so you only need to use the setJMenuBar(...) method.
The most suspect method call there is setSize which takes the pixel height and width. 10000 is probably too large as a width and a height and so you may only be seeing a portion of what you're trying to display. Changing it to something like 800 x 800 may allow you to see new things.

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.

JFrame Questions (deleting textimages adding new ones)

So I am making a game, and I want to know if it is possible so when lets say "You created a fire" it deletes that line and then displays "Your fire turns into ashes".
two more,
I want to make a jframe background, and let's say I "login" the background disappears, and a new background comes in(but the game, not a background).
I want to add a image icon( already added) (IMAGE = FIRE) it deletes that image and a new one appears( IMAGE = ASHES), how can I do this?
public class FireLabel extends JPanel {
public LabelDemo() {
super(new GridLayout(3,1)); //3 rows, 1 column
JLabel label1;
//Create the first label.
label1 = new JLabel("You created a Fire", JLabel.CENTER);
//Add the labels.
add(label1);
add(label2);
add(label3);
}
The context is a little light, however.
For swicthing from one view to another, I would suggest using a CardLayout, which would allow you to change from the login screen to the game screen.
If you're using JLabel as you primary output...simple change the text or icon using setText or setIcon as required...
To change that, use JLabel.setText. You will then have to call validate for the change to take effect.
I recommend swapping out the content pane for this. Put the login screen in a JPanel and set that as the content pane, then when needed, change the content pane to a second JPanel for the game.
Use the same technique as #1. Use a JLabel to display the image.

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