简体   繁体   中英

How to add an image icon to a confirmation dialog box?

The position and size does not matter for now, all I require is to set a custom image icon instead of a default java icon into the dialog box.

Here's what I have done so far:

public void actionPerformed(ActionEvent a) {
  int x=0;

  // Import Asus logo
  ImageIcon img3 = new ImageIcon("AsusConfirmation.jpeg");

  // Create user-friendly information message
  int c = JOptionPane.showConfirmDialog(null, "Your grand total is $"+x+"!\nIs this order correct?", "Checkout", JOptionPane.YES_NO_OPTION, img3);

  if(c==JOptionPane.YES_OPTION) {
    System.exit(0);
  }
}

The error I get with this code is that I can't convert the Image into an int, and I remove the int part, then I won't be able to use:

if(c==JOptionPane.YES_OPTION) {
  System.exit(0);
}

You are not using the right method signature. See at the Javadoc :

public static int showConfirmDialog(Component parentComponent,
                Object message,
                String title,
                int optionType,
           ->   int messageType,  <-
                Icon icon)
                         throws HeadlessException

What you are doing is falling in a different signature that does not give an Icon :

public static int showConfirmDialog(Component parentComponent,
                Object message,
                String title,
                int optionType,
                int messageType)
                         throws HeadlessException

But fail to give an int , causing the compiler to complain about a type mismatch.

What you have to do is, add the missing messageType argument, then you'll match the first method signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM