Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查Jlabel是否有图标_Java_Image_Swing_Jlabel_Imageicon - Fatal编程技术网

Java 检查Jlabel是否有图标

Java 检查Jlabel是否有图标,java,image,swing,jlabel,imageicon,Java,Image,Swing,Jlabel,Imageicon,我在将图像保存到数据库时遇到了一个大问题 我有一个名为personImage的JLabel,当用户想要插入图像时,他必须单击personImage,然后出现一个JFileChooser,用户可以选择图像。所选图像将加载到personImage中 当用户选择并保存图像时,它工作正常,但当用户不选择图像并要保存详细信息时,它会给出NullPointerException。我认为这是因为没有路径将图像获取到文件对象。如何知道JLabel中是否有图像?我想看看是否有图像 try { String

我在将图像保存到数据库时遇到了一个大问题

我有一个名为personImage的JLabel,当用户想要插入图像时,他必须单击personImage,然后出现一个JFileChooser,用户可以选择图像。所选图像将加载到personImage中

当用户选择并保存图像时,它工作正常,但当用户不选择图像并要保存详细信息时,它会给出NullPointerException。我认为这是因为没有路径将图像获取到文件对象。如何知道JLabel中是否有图像?我想看看是否有图像

try {
    String fname = txt_Fname.getText();
    String lname = txt_Lname.getText();
    String mobile = txt_mobile.getText();
    String home = txt_home.getText();
    String work = txt_work.getText();
    String fax = txt_fax.getText();
    byte[] image_detail;

    PersonDAO perDAO = new PersonDAO(); //create person object
    if (status == 1) // used status  for check whether Jlabed is clicked
    {
        File image = new File(path);   
        FileInputStream fis = new FileInputStream(image); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for (int readNum; (readNum = fis.read(buf)) != -1; )
        {
            baos.write(buf, 0,readNum);
        }
        image_detail =  baos.toByteArray();

        Person person1 = new Person(fname, lname, mobile, home, work, fax, image_detail); // call the person
        // constructer when there is an image. I did validate with status variable
            perDAO.InsertPerson(person1); // call the personDAO to insert the Person to database
        }
        else
        {
            Person person2 = new Person(lname, lname, mobile, home, work, fax); // if there is not an image call this constructer .
            perDAO.InsertPerson(person2); // then call to personDAO object to insert the person to  databasee
        }
    }
    catch (Exception exc)
    {
        System.out.println(exc + "sssssss");
    }


    // >>> when click on the JLabel, the JFileChooser appears
    int i = jFileChooser2.showOpenDialog(this);
    try {
        f = jFileChooser2.getSelectedFile();
        path = f.getAbsolutePath();
        ImageIcon image = new ImageIcon(path);
        status =  1;
        personImage.setIcon(image);
    }
    catch (Exception exc)
    {
        System.out.println(exc);
    }
如果您有JLabel标签,则执行此操作以检查其是否有图标:

if (label.getIcon() == null) {
  // this means there is no icon
}
如果您有JLabel标签,则执行此操作以检查其是否有图标:

if (label.getIcon() == null) {
  // this means there is no icon
}

逻辑上要做的事情是存储加载的图像集合。是的,如果有图像,我想存储图像逻辑上要做的事情是存储加载的图像集合。是的,如果有图像,我想存储图像