Home > other >  Incompatible Types JOptionPane java string to png
Incompatible Types JOptionPane java string to png

Time:02-17

Well, I have a little problem here, I want to add an image to a JOptionPane but I can't because I am converting it as a string! I don't know how to solve it! Help! This is the code:

import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
public class TextBox {
        
        ImageIcon icon0 = new ImageIcon("Picture2_2.png");
        String user_input = JOptionPane.showInputDialog(null, "Please enter a card number!", "¡Tu puedes!", JOptionPane.INFORMATION_MESSAGE,icon0,null,"");
   
    public static void main (String [] args)
        
        {
           
            int g = -1;
            while (g<0)
            {
                
                TextBox n = new TextBox();
                
                if(n.user_input.length() > 0 )
                        {
                            numbersreader t= new numbersreader();
                            t.checkLuhn (n.user_input);
                              g  ;
                                                    
                        }else
                            {
                            ImageIcon icon1 = new ImageIcon("Picture2_2.png");
                            JOptionPane.showInputDialog(null, "Please enter a card number!", "¡Ya valio!", JOptionPane.INFORMATION_MESSAGE,icon1, null, "");
                            }
            }
        }


}

If it wasn't for this the code does work, a version of the code that works is down below but I really want to put that picture in that JOptionPane, there is got to be a way to!

import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

public class TextBox {
        
        
        String user_input = JOptionPane.showInputDialog(null, "Please enter a card number!", "¡Tu puedes!", JOptionPane.INFORMATION_MESSAGE);
   
    public static void main (String [] args)
        
        {
           
            int g = -1;
            while (g<0)
            {
                
                TextBox n = new TextBox();
                
                if(n.user_input.length() > 0 )
                        {
                            numbersreader t= new numbersreader();
                            t.checkLuhn (n.user_input);
                              g  ;
                                                    
                        }else
                            {
                            ImageIcon icon1 = new ImageIcon("Picture2_2.png");
                            JOptionPane.showInputDialog(null, "Please enter a card number!", "¡Ya valio!", JOptionPane.INFORMATION_MESSAGE,icon1, null, "");
                            }
            }
        }


}

CodePudding user response:

When I try to compile your code the compiler tells me "error: incompatible types: Object cannot be converted to String".

That is because JOptionPane.showInputDialog() returns some Object, but on line 6 you try to assign the return value to a String variable.

After changing line 6 to

String user_input = (String) JOptionPane.showInputDialog(null, "Please enter a card number!", "¡Tu puedes!", JOptionPane.INFORMATION_MESSAGE,icon0,null,"");

(and providing a png file "Picture2_2.png" and a class numbersreader with a method void checkLuhn(String s) {}) I can compile and run your code.

  • Related