0

I am trying to set a background image for this window and I have tried multiple ways but I am compiling/saving it, it's giving an error as shown below:

enter image description here

Here is my code:

public class Admin_hs extends JFrame {

JButton bking_btn= new JButton("Bookings");
JButton fd_btn= new JButton("Financial Data");
JButton ctm_btn= new JButton("Customers");
JButton room_btn= new JButton("Rooms");
JButton adc_btn= new JButton("Additional Costs");
JButton endb_btn= new JButton("Ending Bookings");

//Images
JLabel bking_img= new JLabel();
JLabel fd_img= new JLabel();
JLabel ctm_img= new JLabel();
JLabel room_img= new JLabel();
JLabel adc_img= new JLabel();
JLabel endb_img= new JLabel();
JLabel logout= new JLabel(); 
JLabel copyrightL = new JLabel("Hotel Management System \u00a9 2016");
private BufferedImage image;
///Panels

JPanel pnl1= new JPanel();//panel for buttons and images
JPanel pnl2= new JPanel(new GridBagLayout());//panel for copyright label
JPanel pnl3= new JPanel(); //panel for logout button


///Constructors

public Admin_hs(){

    this.setTitle("Welcome Admin!");
    this.setLayout(new GridBagLayout());

    ///Setting a layout
    pnl1.setLayout(new GridBagLayout());

    logout.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/logout.jpg"));

     pnl3.setLayout(new GridBagLayout());
        //logout = new JLabel( "Logout" );
        pnl3.add(logout, new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, GridBagConstraints.EAST,
                GridBagConstraints.NONE, new Insets(6, 6, 6, 6), 0, 0));

    //background image
        try {
            image = ImageIO.read(new File("‪C:/Users/Diksha/Desktop/simple-blue.jpg"));

            this.setContentPane(new JLabel(new ImageIcon(image)));
        } catch (IOException e) {
            e.printStackTrace();
        }



    ///First Column of Grid
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth= GridBagConstraints.REMAINDER;
    gbc.fill= gbc.HORIZONTAL;
    GridBagConstraints gc= new GridBagConstraints();

    gc.insets = new Insets(6, 6, 6, 6);
    gc.anchor = GridBagConstraints.EAST;
    gc.weightx = 0.5;
    gc.weighty = 0.5;

    gc.gridx = 0;
    gc.gridy = 0;
    bking_btn.setPreferredSize(new Dimension(130,30));
    bking_btn.setMinimumSize(new Dimension(130,30));
    pnl1.add(bking_btn, gc);


    gc.gridx = 0;
    gc.gridy = 1;
    fd_btn.setPreferredSize(new Dimension(130,30));
    fd_btn.setMinimumSize(new Dimension(130,30));
    pnl1.add(fd_btn, gc);

    gc.gridx = 0;
    gc.gridy = 2;
    ctm_btn.setPreferredSize(new Dimension(130,30));
    ctm_btn.setMinimumSize(new Dimension(130,30));
    pnl1.add(ctm_btn, gc);

    gc.gridx = 0;
    gc.gridy = 3;
    room_btn.setPreferredSize(new Dimension(130,30));
    room_btn.setMinimumSize(new Dimension(130,30));
    pnl1.add(room_btn, gc);

    gc.gridx = 0;
    gc.gridy = 4;
    adc_btn.setPreferredSize(new Dimension(130,30));
    adc_btn.setMinimumSize(new Dimension(130,30));
    pnl1.add(adc_btn, gc);

    gc.gridx = 0;
    gc.gridy = 5;
    endb_btn.setPreferredSize(new Dimension(130,30));
    endb_btn.setMinimumSize(new Dimension(130,30));
    pnl1.add(endb_btn, gc);

    gc.gridx = 0;
    gc.gridy = 8;
    pnl1.add(copyrightL, gc);


    /////second column of grid



    gc.anchor = GridBagConstraints.WEST;
    gc.gridx = 1;
    gc.gridy = 0;
    bking_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/ReserveNowIcon.png"));
    pnl1.add(bking_img, gc);

    gc.gridx = 1;
    gc.gridy = 1;
    fd_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/booking.jpg"));
    pnl1.add(fd_img, gc);

    gc.gridx = 1;
    gc.gridy = 2;
    ctm_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/guest.jpg"));
    pnl1.add(ctm_img, gc);

    gc.gridx = 1;
    gc.gridy = 3;
    room_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/room.jpg"));
    pnl1.add(room_img, gc);

    gc.gridx = 1;
    gc.gridy = 4;
    adc_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/fd.jpg"));
    pnl1.add(adc_img, gc);

    gc.gridx = 1;
    gc.gridy = 5;
    endb_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/endb.png"));
    pnl1.add(endb_img, gc);


    pnl1.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        pnl2.add(copyrightL, gbc);

        gbc.gridwidth = 1;
        gbc.weightx = 1.0;
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(pnl3, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        add(pnl1, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        add(pnl2, gbc);

        this.setSize(image.getWidth(),image.getHeight());
        this.setLayout(new FlowLayout());
        this.setResizable(true);
        this.setLocationRelativeTo(null);

    }

}

Main

 public class Admin_main {

public static void main(String[] args) {

    Admin_hs adm= new Admin_hs();

    adm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    adm.pack();
    adm.setVisible(true);
    adm.setSize(800,701);

     }

}

I have added images on the window using setIcon but I didn't get this problem upon compilation. Even when I am commenting out all the lines added for the background image, I am getting the same error. Only when I remove it that I am able run it.

Here is a pic to get an idea of how it looks: enter image description here

EDIT When I clicked on "Save as UTF", it's throwing an exception:

enter image description here

I would like to know any alternative ways of how I can set a background image as I have tried overriding the paint method also but it didn't work. Thanks.

Tia
  • 1,220
  • 5
  • 22
  • 47

1 Answers1

2

So, I took your code, replaced the background image with one of my own and ran it...

Background

So, either C:/Users/Diksha/Desktop/simple-blue.jpg doesn't exist or isn't an image format that ImageIO can read.

Also, as you can see, the JPanel is opaque, so it doesn't show the background beneath it. You will need to change the opaque property to false

The encoding problem is associated with the inclusion of unicode characters (\u00a9) and if you save the file as UTF-8 it should solve the problem.

The layout issue is because you've used a FlowLayout (this.setLayout(new FlowLayout());) for the contentPane

Now, before you tell me the image is fine, I have a running example which says otherwise

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • is it the '-' (dash or minus) in the file name simple-blue.jpg ? if i.e. gimp can open it JVM should also... – ralf htp Feb 28 '16 at 21:34
  • I don't know, I don't have access to your computer – MadProgrammer Feb 28 '16 at 21:35
  • @MadProgrammer I really don't know what's wrong because the image path is correct as it is with other images which I have used which are showing up.. I changed the image, path, format(used .png) but getting the same error. – Tia Feb 28 '16 at 21:57
  • @MadProgrammer I removed the encoding but it threw the same exception – Tia Feb 28 '16 at 21:58
  • 1
    *"same exception"* isn't very informative. Here's a problem, all I did with your code was change the image of the background and removed all the other `ImageIcon` references and it worked just fine - maybe you should start by commenting out all the images and see if that works, re-introducing one at a time to see which one doesn't work. – MadProgrammer Feb 28 '16 at 22:02
  • Anime. Does it always have to be anime? ;) 1+ – Hovercraft Full Of Eels Feb 28 '16 at 22:09
  • @HovercraftFullOfEels You honestly have to ask the question ... sometimes it ponies! – MadProgrammer Feb 28 '16 at 22:10
  • @MadProgrammer By same exception, I meant this `javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(Unknown Source)` and it points to the line where the image is being read `ImageIO.read` – Tia Feb 28 '16 at 22:11
  • 1
    @Diksha Okay, it's time to find and use a different file. You need to verify that the exists, which you can use `File#exists` for. You should also test to see that the image can be loaded by another program as well – MadProgrammer Feb 28 '16 at 22:13
  • @MadProgrammer Thanks a lot! I loaded the image(simple-blue.jpg) I used in another new program and it didn't work at first, then added a new image in the same program and it worked. Then I added the first image to the same new program and somehow it worked. I added the image path simple-blue.jpg in the program which I posted here again and I don't know how but it worked! – Tia Feb 28 '16 at 22:24
  • I have set the panel's opacity to false..I am having some problem with the layout though.. When I am calling `this.setLayout(new FlowLayout());`, the labels and images are getting distorted like in the screenshot you posted and when I am not calling it, the background image is covering everything so that only the background image is appearing. How can I fix that? – Tia Feb 28 '16 at 22:30
  • @MadProgrammer Any suggestions? I am new to Java (and Layouts) so would appreciate your help – Tia Feb 29 '16 at 06:04
  • `BorderLayout`, `GridBagLayout` – MadProgrammer Feb 29 '16 at 06:17
  • Thanks. BorderLayout covered the all the components but GridBagLayout worked partly – Tia Feb 29 '16 at 16:20
  • I fixed it by setting the GridBagLayout before adding the panels to JFrame as I guess the constraints are not saved by the layout manager, since by default a JLabel (which is now the content pane) uses a null layout until specifically the layout is set. – Tia Feb 29 '16 at 21:03
  • `GridBagLayout` needs to be setup with the conrrect constraints – MadProgrammer Feb 29 '16 at 21:03