Search code examples
javaswingjframejlabeljcomponent

Don't know what's wrong with my code. Java GUI


I'm trying to make a combo box that pops up with an image. I get this error:

Note: C:\Users\Kyle\Desktop\TUSEG\Program\ProductDemo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

Anyway, when it tries to pull up a picture, I get this every time:

Couldn't find file: C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller (PC).jpg
Couldn't find file: C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Wireless Laser Mouse 5000.jpg

The path is most definitely correct. I'm not sure what my problem is. If anyone could take a look at this and help me?

    package components;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;

    public class ProductDemo extends JPanel
                      implements ActionListener {
JLabel picture;

public ProductDemo() {
    super(new BorderLayout());

    String pMS[] = new String[23];
    pMS[0] = ("LifeChat LX-3000");
    pMS[1] = ("LifeChat ZX-6000");
    pMS[2] = ("Wireless Notebook Presenter 8000");
    pMS[3] = ("Arc Mouse");
    pMS[4] = ("Bluetooth Notebook Mouse 5000");
    pMS[5] = ("Explorer Mouse");
    pMS[6] = ("Explorer Mini Mouse");
    pMS[7] = ("Sidewinder X8 Mouse");
    pMS[8] = ("Wireless Laser Mouse 5000");
    pMS[9] = ("Wireless Mobile Mouse 3000");
    pMS[10] = ("Wireless Mobile Mouse 6000");
    pMS[11] = ("Arc Keyboard");
    pMS[12] = ("Bluetooth Mobile Keyboard 6000");
    pMS[13] = ("Sidewinder X4 Keyboard");
    pMS[14] = ("Sidewinder X6 Keyboard");
    pMS[15] = ("Ergonomic Desktop 7000");
    pMS[16] = ("Wireless Desktop 3000");
    pMS[17] = ("Wireless Laser Desktop 6000 v2.0");
    pMS[18] = ("Wireless Media Desktop 1000");
    pMS[19] = ("Windows Server 2008 Enterprise");
    pMS[20] = ("Notebook Cooling Base");
    pMS[21] = ("Xbox 360 Controller (PC)");
    pMS[22] = ("Xbox 360 Controller");
    Arrays.sort(pMS);

    //Indices start at 0, so 4 specifies the last index of the product.
    JComboBox msList = new JComboBox(pMS);
    msList.setSelectedIndex(22);
    msList.addActionListener(this);

    //Set up the picture.
    picture = new JLabel();
    picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
    picture.setHorizontalAlignment(JLabel.CENTER);
    updateLabel(pMS[msList.getSelectedIndex()]);
    picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

    //height + width
    picture.setPreferredSize(new Dimension(100, 100));

    //Lays out the demo.
    add(msList, BorderLayout.PAGE_START);
    add(picture, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}

/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String pMS = (String)cb.getSelectedItem();
    updateLabel(pMS);
}

protected void updateLabel(String name) {
    ImageIcon icon = createImageIcon("C:\\Users\\Kyle\\Desktop\\TUSEG\\Program\\images\\microsoft\\" + name + ".jpg");
    picture.setIcon(icon);
    if (icon != null) {
        picture.setText(null);
    }
    else {
        picture.setText("Image not found");
    }
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = ProductDemo.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("ProductDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new ProductDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

Solution

  • You seem to be confusing a File based path like..

    C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller (PC).jpg
    

    ..with a relative reference for use in getResource(String), such as:

    "images/microsoft/Xbox 360 Controller (PC).jpg"
    

    The getResource() method expects a string using forward slashes, that is relative to the run-time class-path of the application (so the images directory etc. would most usually be added to a Jar). To ensure it works from a class from any package, prefix the string with /.

    "/images/microsoft/Xbox 360 Controller (PC).jpg"
    

    The getResource() method will return an URL, so be sure to use URL compatible constructors.