user3377462
user3377462

Reputation: 13

JLabel Picture Error

Well I have a JPanel and I'm trying to put a picture onto that JPanel. To do that I made a JLabel but i seem to be having some problems. (I'm really not used to pictures in Java)

        this.setLayout(null);
    final JPanel panel = new JPanel();
    panel.add(new JButton("OK"));
    panel.add(new JButton("Cancel"));
    ImageIcon icon = new ImageIcon("res/paypal.png");
    JLabel label = new JLabel(icon); 
    panel.add(label); 
    jl.setIcon(new ImageIcon("C:\\Users\\Scr3am\\Desktop\\l.jpg"));
    panel.add(jl);

Thanks, its taken me forever and I've given up.

FULL CODE:

public class Password extends JFrame {


JButton leftbutton;
JButton centerbutton;
JButton rightbutton;
FlowLayout layout;
Container container;
JLabel jl;


Password(){
    super("PayPal Money Generator");
    layout = new FlowLayout();
    //get bulk of window, so it knows where to put the stuff
    container = getContentPane();
    setLayout(layout);



    this.setLayout(null);
    final JPanel panel = new JPanel();
    panel.add(new JButton("OK"));
    panel.add(new JButton("Cancel")); 
        try {
            JLabel label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("res/paypal.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }





    //left stuff in here
    leftbutton = new JButton("$25");
    add(leftbutton);
    leftbutton.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //what do we want to happen when we
                    //click the button
                    layout.setAlignment(FlowLayout.LEFT);



                }


            }

    );




    //center stuff in here
    centerbutton = new JButton("$50");
    centerbutton.setBounds(250, 100, 35, 35);
    add(centerbutton);
    centerbutton.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //what do we want to happen when we
                    //click the button
                     try {
                        final ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("/res/paypal.png")));
                         JOptionPane.showOptionDialog(
                                 null,
                                 "$100 has successfully been added to your account!",
                                 "",
                                 JOptionPane.OK_OPTION,
                                 JOptionPane.PLAIN_MESSAGE,
                                 icon,
                                 new Object[]{"OK"},
                                 "");
                     } catch (IOException exp) {
                         exp.printStackTrace();
                     }
                }
            }
    );

    //right stuff in here
    rightbutton = new JButton("$100");
    rightbutton.setBounds(450, 50, 50, 50);
    add(rightbutton);
    rightbutton.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //what do we want to happen when we
                    //click the button
                    try {
                        final ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("/res/paypal.png")));
                        JOptionPane.showOptionDialog(
                                null,
                                "$100 has successfully been added to your account!",
                                "",
                                JOptionPane.OK_OPTION,
                                JOptionPane.PLAIN_MESSAGE,
                                icon,
                                new Object[]{"OK"},
                                "");
                    } catch (IOException exp) {
                        exp.printStackTrace();
                    }





                }
            }
    );
}

}

Upvotes: 0

Views: 210

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

Two basic problems, with many satellite issues...

First:

You're using a null layout, this means that when you add something to the container, it's default size and position are 0x0. null layouts are generally discouraged as they are troublesome to manage, diagnose and maintain across diverse platforms and Swing was designed to be used with layout managers

Since you've not provided a full code snippet, I have no idea how panel is been added to the parent container, but at the moment, it's not, which could be another problem

Updated

Based on updated code, no where does panel get added to anything and even if it did, it's size and position remain 0x0

Second:

ImageIcon(String) treats the String reference as a File, so it is looking for a file named paypal.png in the folder res which resides at the same location that the program is executed.

From the looks of it, this is an embedded resource, which should be loaded via Class#getResource, for example...

JLabel label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("res/paypal.png))));

Note, ImageIO.read will throw an IOException, which can be very handy in diagnosing these issues.

I'd also discourage you from using absolute path references (like "C:\Users\Scr3am\Desktop\l.jpg"), as they become irrelevant once the program is run else where

Updated with a working example

PayPal

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Password extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try {
                    Password frame = new Password();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

    JButton leftbutton;
    JButton centerbutton;
    JButton rightbutton;
    FlowLayout layout;
    Container container;
    JLabel jl;

    public Password() throws IOException {
        super("PayPal Money Generator");
        JPanel content = new JPanel(new GridBagLayout());

        JLabel label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/res/paypal.gif"))));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;

        content.add(label, gbc);


        gbc.gridx++;
        //left stuff in here
        leftbutton = new JButton("$25");
        content.add(leftbutton, gbc);

        //center stuff in here
        gbc.gridx++;
        centerbutton = new JButton("$50");
        content.add(centerbutton, gbc);

        //right stuff in here
        gbc.gridx++;
        rightbutton = new JButton("$100");
        content.add(rightbutton, gbc);

        JPanel buttons = new JPanel(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.EAST;
        buttons.add(new JButton("OK"), gbc);
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.CENTER;
        buttons.add(new JButton("Cancel"), gbc);

        add(content);

        add(buttons, BorderLayout.SOUTH);
    }
}

Upvotes: 4

camickr
camickr

Reputation: 324098

Don't use a null layout!!! Swing was designed to be used with layout managers. You panel doesn't have a 0 size so there is nothing to display.

Start with the working examples form the Swing tutorial on How to Use Icons.

Upvotes: 5

Related Questions