hi i am creating an applet where you click on the button and it adds an image and text to a list, which then will be displayed in a JOptionPane but the Image will not display but the text does. code is as followed;

Add the items

String additem = (String)itemList.getSelectedItem();
        if(additem.equals("Justin Bieber - Baby")){
                    list.add("Justin Bieber - Baby");
                    list.add(new ImageIcon("justin.jpg"));
        } else if (additem.equals("Britney Spears - Hit me baby")) {
                list.add("Britney Spears - Hit me baby");
            }

Display in OptionPane

 JOptionPane Order = new JOptionPane();
        Order.setMessage(list);
        Order.setMessageType(JOptionPane.INFORMATION_MESSAGE);
        JDialog dialog = Order.createDialog(null, name.getText()+"'s"+" " + "Order");
        dialog.setVisible(true);

the result is [Justin Bieber - Baby] don't understand why the [] are around the words.

also i know britney and justin are gay so no payouts its just for an assignment.

regards
jdiddy

Recommended Answers

All 6 Replies

How/where is "list" declared?

How/where is "list" declared?

it is declared in the class:
Vector<Object> list = new Vector<Object>();

i have also tried Arraylist

jdiddy

Can you confirm that
new ImageIcon("justin.jpg")
is working?
There are very often problems finding files like this (path problems), because new ImageIcon just returns null with no exception thrown. If the downstream code (eg setImageIcon) also accepts null as a parameter (which it does without any complaints) the the whole thing just doesn't work, but there are no errors.

yes it is working i have created a method

private void justinpic(){ //Create image and set size
    Image justin = getImage(getDocumentBase(),getParameter("justin"));
    justin = justin.getScaledInstance(50,50, justin.SCALE_DEFAULT);
     justinpic = new ImageIcon(justin);
    ImageLabel.setIcon(justinpic);

which is called in the ImageLabel and the picture displays fine....

OK. Personally wouldn't jut assume that because that code works a new ImageIcon("justin.jpg") will also work, but let's move on.
Here's an extract from the JOptionPane API

message
A descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant. However, the type of this parameter is actually Object. Its interpretation depends on its type:

Object[]
An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type.
Component
The Component is displayed in the dialog.
Icon
The Icon is wrapped in a JLabel and displayed in the dialog.
others
The object is converted to a String by calling its toString method. The result is wrapped in a JLabel and displayed.

This suggests that a List may not be interpreted as multiple objects - it's only arrays that are guaranteed. Try
Order.setMessage(list.toArray());

OK. Personally wouldn't jut assume that because that code works a new ImageIcon("justin.jpg") will also work, but let's move on.
Here's an extract from the JOptionPane API


This suggests that a List may not be interpreted as multiple objects - it's only arrays that are guaranteed. Try
Order.setMessage(list.toArray());

That has helped to sort the list but the result for the picture is still the text justin.jpg not an image.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.