Display image on JFrame form Java NetBeans - java

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.

Related

java JLabel text exceeds parent JLabel width

I have a class called iconLabel that extends JLabel to make buttons.
I use the Font Awesome font to set the text of the JLabel to an icon. I also add another JLabel to that iconLabel object in the constructor to display text on the icon. The problem is that the text on the icon exceeds the width of the icon, so you'll get "...". How can I make it so the JLabel text may exceed the icon width?
Here is an image for clarification.
The problem is that the text on the icon exceeds the width of the icon, so you'll get "...".
A JLabel can display an Icon with text painted on top of the Icon. There is no need for a custom class to do this.
The basic code is:
JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Easy Way" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
The thing is that I want to use the font awesome icons
So you use the setText() method and you get an Icon painted? Cool!
Maybe you can use a JPanel with an OverlayLayout. Then you add the two labels to the panel, making sure the alignmentX/Y values are both 0.5f so the components are centered in the panel. Now the panel should be the size of the largest label.
Or maybe you can use the Text Icon. This class allows you to create an Icon from a text String. You will still need to labels but the sizing should work correctly.
The code should be something like:
JLabel iconLabel = new JLabel();
iconLabel.setFont(...);
TextIcon icon = new TextIcon(iconLabel, "...")
Jlabel label = new JLabel("some text");
label.setIcon(icon);

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.

Image autofit in a JButton?

I am new to Java and I met a little problem.
Can someone please tell me how I can set the Image of a JButton to be auto resizable/fitable/stretchable in a JButton when I resize the whole JFrame? The JButton is in a JPanel with a GridLayout.
It will be great if someone could write a brief explanation. I need this to complete a calculator I am working on.
Add component listener to the button.
Resize the Image on Fly and update Image on the button.
Image img = icon.getImage();
Image newimg = img.getScaledInstance(NEW_WIDTH, NEW_HEIGHT, java.awt.Image.SCALE_SMOOTH);
icon.setImage(newimg);

Java - multiple JLabels to one single JPanel

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

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