0

I'm trying to get the PayPal logo come up when you click the 'rightbutton' is clicked. Unfortunately, all that shows is the default Java logo with the cup of coffee and a pen.

Also, how can I make it so once you click "OK" or "Cancel" it closes the JOptionPane, currently, when you click "OK" nothing happens, it keeps giving you the "OK" / "Cancel" option.

rightbutton = new JButton("Right.");
add(rightbutton);
rightbutton.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent event){
                //what do we want to happen when we
                //click the button
                final ImageIcon icon = new ImageIcon("C:\\Users\\Scr3am\\Desktop\\paypal.jpg");
                JOptionPane.showOptionDialog(null, "Congratulations, you clicked the button.", "Congrats", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { panel }, icon);
            }
        }
);

FULL CODE

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Password extends JFrame {

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




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

        //left stuff in here
        leftbutton = new JButton("Left");
        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);

                    }


                }

        );

        final JPanel panel = new JPanel();
        panel.add(new JButton("OK"));
        panel.add(new JButton("Cancel"));

        //center stuff in here
        centerbutton = new JButton("Center");
        add(centerbutton);
        centerbutton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        //what do we want to happen when we
                        //click the button
                        layout.setAlignment(FlowLayout.CENTER);
                        layout.layoutContainer(container);

                    }
                }
        );

        //right stuff in here
        rightbutton = new JButton("Right.");
        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(new File("paypalicon.gif")));

                            JOptionPane.showOptionDialog(
                                    null,
                                    "Congratulations, you clicked the button.",
                                    "Congrats",
                                    JOptionPane.OK_OPTION,
                                    JOptionPane.PLAIN_MESSAGE,
                                    icon,
                                    new Object[]{"Okay"},
                                    "Okay");
                        } catch (IOException exp) {
                            exp.printStackTrace();
                        }



                    }
                }
        );
    }


}

Errors:

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1275)
at Password$3.actionPerformed(Password.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6414)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Container.processEvent(Container.java:2084)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
at java.awt.Container.dispatchEventImpl(Container.java:2128)
at java.awt.Window.dispatchEventImpl(Window.java:2492)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3377462
  • 13
  • 1
  • 4

2 Answers2

3

I think you have your parameters around the wrong way

enter image description here

try {
    final ImageIcon icon = new ImageIcon(ImageIO.read(new File("paypalicon.gif")));

    JOptionPane.showOptionDialog(
            null,
            "Congratulations, you clicked the button.",
            "Congrats",
            JOptionPane.OK_OPTION,
            JOptionPane.PLAIN_MESSAGE,
            icon,
            new Object[]{"Okay"},
            "Okay");
} catch (IOException exp) {
    exp.printStackTrace();
}

JOptionPane declares showOptionDialog as having the following parameters (in order)

  • Component, the parent component associated with the dialog
  • Object, the message to be displayed
  • String, the dialog title
  • int, the type of options (if not specified), such as JOptionPane.OKAY_CANCEL_OPTION
  • int, the message type, such as JOptionPane.INFORMATION_MESSAGE, which can define the icon that the dialog will use
  • Icon, the icon to be displayed on the dialog
  • Object[], the options to be made available to the user (buttons)
  • Object, the initial option which is given focus

You seem to be passing...

  • null as the parent, okay.
  • "Congratulations, you clicked the button.", okay
  • "Congrats", okay
  • JOptionPane.DEFAULT_OPTION, okay
  • JOptionPane.INFORMATION_MESSAGE probably okay, I'd use JOptionPane.PLAIN_MESSAGE, but that's me
  • null...no icon?
  • new Object[] { panel }, not sure if this is okay, but at least it's in the right place
  • icon...this isn't even a value i the option parameters you passed ... think this is missed place.

The problem with the buttons comes down to the fact that they are disconnected from the dialog. The dialog has absolutely no way to know that the buttons are been clicked, you'd have to provide that functionality via some kind of ActionListener...to be honest, it'd just pass String values as the options parameter and let JOptionPane render them as buttons, as it will then deal with closing the dialog, otherwise it gets real messy, real quick...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ahh, remember to replace the `final ImageIcon icon = new ImageIcon(ImageIO.read(new File("paypalicon.gif")))` with the source of your own image ;) – MadProgrammer Mar 05 '14 at 01:35
  • Haha, oops… xD I'll try it with the correct one on my desktop. EDIT: Still didn't work :( – user3377462 Mar 05 '14 at 01:46
  • @user3377462 Then your file is in the wrong location (a.k.a your path is wrong) - The way you`re doing it (the path), the file needs to be in the project root folder. This is discouraged though. See [this answer](http://stackoverflow.com/a/9866659/2587435) for how to use embeddeded resources. – Paul Samsotha Mar 05 '14 at 01:49
  • @user3377462 Where is you icon stored? Because it works fine for me. – MadProgrammer Mar 05 '14 at 01:51
  • @peeskillet I'd argue that that is conceptually specific. Embedded resources are definitely easier to deploy and manager (generally) and absolute references should be discouraged, but there's nothing saying that having external resources is wrong, in some cases, it's actually a better solution - but it's all about context - just saying – MadProgrammer Mar 05 '14 at 01:53
  • @MadProgrammer can you embed resources that aren't in the class path? I've never done it before, outside a development environment – Paul Samsotha Mar 05 '14 at 01:57
  • @peeskillet No, then they're not embedded resources ;). You can resources that a relative to the applications execution context and are loaded as normal `File`s, but this is all contextual and comes down to requirements (and I think your right about this specific case). I also agree, we should avoid absolute references, but there's nothing wrong with have using relative referenced resources (with context) - that's all I'm saying ;) – MadProgrammer Mar 05 '14 at 02:00
  • @MadProgrammer I just now saw the absolute path, before the OP only had the image file name as the path, hence my earlier comment – Paul Samsotha Mar 05 '14 at 02:01
  • @MadProgrammer Well I made a new folder titled 'res' and dragged the image in there. I'm guessing that's not what I'm supposed to do. lol sorry i'm a beginner when it comes to imageicons – user3377462 Mar 05 '14 at 02:07
  • What IDE are you using? Where is `res` in relationship to your application? – MadProgrammer Mar 05 '14 at 02:09
  • @peeskillet No issue from me (and I appreciate the discussion) ;) – MadProgrammer Mar 05 '14 at 02:15
  • @MadProgrammer Most recent IDE. I right clicked 'src' and Create > New Folder - then dragged PayPal.jpg into the res folder. – user3377462 Mar 05 '14 at 02:15
  • @user3377462 Yes, but which one, eclipse, netbeans...? – MadProgrammer Mar 05 '14 at 02:15
  • Eclipse- @MadProgrammer – user3377462 Mar 05 '14 at 02:16
  • @user3377462 From my understanding Eclipse, you need to create a `resources` directory in you project (at the same level as your `src` directory). You then need to reference the resources using `Class#getResource`, for example `final ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("/PayPal.jpg")))`. Not an Eclipse user, so this might need some tweaking to get right, but that's the general idea – MadProgrammer Mar 05 '14 at 02:18
  • Aight, thanks so much for your help. I did that but added "/res/paypal.jpg" and it worked but now it's red (for some reason, have no idea why, original picture is white) and hella big http://imgur.com/mdqb1vE – user3377462 Mar 05 '14 at 02:31
  • This is an issue with `ImageIO` and transparent jpegs. The only solution is not use transparent jpegs, but instead use gifs or pngs – MadProgrammer Mar 05 '14 at 02:33
  • @MadProgrammer Aight, I used a new image. It looks better. http://imgur.com/lrOEpwU I just have one last thing, how can I make the "OK" farther to the left and make the "Cancel" appear? I'm guessing id have to change the size? – user3377462 Mar 05 '14 at 02:46
  • You can't change the button positions. If you included "Cancel" in the options, along with "Okay", it will appear, for example `new Object[]{"Okay", "Cancel"},`. The `JOptionPane` will now return either `0` for "Okay" or `1` for "Cancel" – MadProgrammer Mar 05 '14 at 02:50
  • aight, damn, one more thing @MadProgrammer. how would you go about adding an image without creating a new JOptionPane? So it probably be somewhere in the JPanel block of code but would the command/concept be the same? – user3377462 Mar 05 '14 at 22:50
  • Use a `JLabel`, see [How to Use Labels](http://docs.oracle.com/javase/tutorial/uiswing/components/label.html) – MadProgrammer Mar 05 '14 at 22:56
3

For the ImageIcon, it's because you have the arguments in the wrong order:

JOptionPane.showOptionDialog(
    null,
    "Congratulations, you clicked the button.",
    "Congrats",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    null,
    new Object[] { panel },
    icon
);

So right now you are providing null for the icon and the icon for the initial value. It should be:

JOptionPane.showOptionDialog(
    null,
    "Congratulations, you clicked the button.",
    "Congrats",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    icon,
    new Object[] { panel },
    null
);

For the other question, it looks like you are providing your own options as some kind of panel. I guess technically what you could do is use a constructor so you have a reference to the JOptionPane that you can dismiss programatically but you do not need to do this. Instead, I would recommend using the regular OK_CANCEL_OPTION which will do this for you.

int optionChosen = JOptionPane.showConfirmDialog(
    null,
    "Congratulations, you clicked the button.",
    "Congrats",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    icon
);

Edit for your comment:

  • Try replacing the backslashes in the path with forward slashes. The doc says the path is converted to URL and to use forward slashes. See also File.separator vs Slash in Paths.
  • The doc says the image is loaded in the background. So in other words you might be displaying the image before the data is loaded. There's two things you could do. First, preloading the image at the start of the program by making it a static member, second using ImageIO which will load the image fully immediately.

You should really preload the image anyway. Otherwise you are reading the file every time you show the dialog.

static final ImageIcon paypalIcon;
static {
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("your/path"));
    } catch(IOException ioe) {
        ioe.printStackTrace(System.err);
    }

    // set to null if the image failed to load
    // if you pass null to showXXXDialog it uses the Look & Feel default
    // this way the program still works if it can't find the file
    if(img == null) {
        paypalIcon = null;
    } else {
        paypalIcon = new ImageIcon(img);
    }
}
Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125