Java - multiple JLabels to one single JPanel - java

I am currently reading from my database where i am able to read the location strings and convert them to images that are able to appear on a jframe. I create a JLabel and then change this to an ImageIcon. The code for that is below. How do I put all of these imageicons into one single JPanel. So far, I have only been able to put the image into a separate JPanel and all of these jpanels appear on a jframe as the method iterates over every image that is matched in the database.. Any help would be appreciated
JLabel image = new JLabel(new ImageIcon(pic));
image.setIcon(new ImageIcon(pic));
panel.add(image);

Since a JPanel by default uses a FlowLayout, you can just use subsequent add calls
panel.add( image1 );
panel.add( image2 );
panel.add( image3 );
and all images will be added to the panel

Related

How to add JLabel in a JPanel Vertically?

I'd like to add JLabels dynamically in a JPanel vertically like the image that I've attached. After loading all images, I need to select an image, then selected the image should appear in another JPanel. I am reading Images from an ArrayList which contains the paths.
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
This is the code that I've used to add Jlabels:
for(String file: files) {
JLabel JLabelPicture = new JLabel(new ImageIcon(file));
panel_images.add(JLabelPicture);
}
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
A GridLayout will allow you to display the components vertically. When you create your panel you just use:
JPanel imagePanel = new JPanel( new GridLayout(0, 1) );
This will resize all the images to the same size.
Another option is to use a vertical BoxLayout. In this case you can use:
Box imagePanel = Box.createVerticalBox();
In this case the images will retain their preferred size.
In both case you add to the panel to a scroll pane which is added to your frame:
frame.add( new JScrollPane( imagePanel ) );
Read the Swing tutorial on Layout Managers for more information and working examples of each layout.
Edit:
after listing the images I need to select one of them,
Well where was that requirement in your original question. The complete requirement should be defined in the question so all the information is in one place for everybody to see.
So I would suggest you should be using a JList to display an Icon. Read the section from the Swing tutorial on How to Use Lists for more information.

Display image on JFrame form Java NetBeans

I want to show a image on top this form, but I don't know how. I have tried many times, but I cant find any image container in Toolbox.
Screenshot of JFrame:
but i cant find any image container I
Add an ImageIcon to a JLabel.
Icon icon = new ImageIcon(...);
JLabel label = new JLabel( icon );
frame.add( label );
Read the section from the Swing tutorial on How to Use Icons for a working example.

Putting a Panel on top of a JLabel backGround (Java)

I've been fruitlessly searching the internet and nothing that people suggest seems to have any effect for me.
I have a JFrame which I'm trying to put a JPanel in. That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground. The issue is every type of layout manager I've seen suggested just does not work as advertised for me. The best I've gotten to work so far is this approach:
public MenuBackgroundPanel(AsteroidsFrame frame)
{
this.gameFrame = frame;
this.setLayout(new OverlayLayout(this));
ImageIcon image = new ImageIcon(getClass().getResource("/resources/background1.gif"));
imageLabel = new JLabel(image, JLabel.CENTER);
mp = new MainMenuPanel(gameFrame);
mp.setMaximumSize(new Dimension(300,200));
this.add(mp);
this.add(imageLabel);
this.setVisible(true);
}
Unfortunately, I'm getting really strange alignments and trying to set location on the background (to actually get it to start at the JFrame's (0,0) or moving the button panel just seems to have no effect. Printing the location of each object says they're both at (0,0) but the image I'll link shows this is just not the case. My point is, I've tried things like JLayeredPane or setting the JLabel as the contentpane of the Jframe and making it transparent but nothing seems to do anything. One or the other of the two objects just covers the other completely.
As you can see the objects are not at all aligned.
Could anyone help me with this?
That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground
Easiest way for something like this when the child panel is fully contained in the label image is to just set the layout manager of the JLabel and then add your components to the label.
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
JPanel buttons = new JPanel();
buttons.setOpaque( false );
buttons.add(...);
background.add(buttons, new GridBagConstraints() );
Now the button panel will be centered on the label.
As you can see the objects are not at all aligned
If you want to use the OverlayLayout then you need to play with the alignmentX/Y properties of each component. You would probably want to set them both to .5. Check out: Java Layout with Component always in Top Right for an example of how changing these values can affect the layout.

How to add a background image to a JFrame with no panels, without using a JLabel?

I want to add a background image to a JFrame which doesn't have any panels. It is a project I'm working on and I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code to do that and also I'm using netbeans. Is there any solution for this?
I want to add a background image to a JFrame which doesn't have any panels.
The content pane of the frame is a JPanel, so yes it does have panels.
I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code
If you want a background image then you will need to change your code to make sure the content pane can display the image. So yes you will need to change your code whether you use a JLabel of a JPanel that paints an image.
Check out Background Panel for code that will allow you to use either approach.
The key is that you need to set the content pane of your frame BEFORE you start adding components to the frame. So the code might look something like:
BackgroundPanel panel = new BackgroundPanel( yourImage );
frame.setContentPane( panel );
frame.add(northPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);
I don't know what the Netbeans generated code looks like so I'll leave it up to you to figure out where to put the code.

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.

Categories

Resources