Вопрос

hello Guys. I would like fist to say thanks for all who help us in this greet website.. I am facing problem in passing path of an image from a frame to another class when user click the button.. the calling is:

Filename=txt_attach.getText();
LoadSickLeaveImage sickLeave=new LoadSickLeaveImage();
sickLeave.ImageIcon(Filename);

and I want to pass the path for the picture from txt_attach into another class to ImageIcon method, but nothing shows. No mistake or no errors.

public class LoadSickLeaveImage {
    public ImageIcon ImageIcon(String Filename){
        ImageIcon icon = new ImageIcon(Filename);
        return icon;
    }

    public static void main(String[] args) throws IOException {
      LoadSickLeaveImage icon = new LoadSickLeaveImage(); 
      ImageIcon image = icon.ImageIcon("");

      //Check if file exists or not.
      File file = new File(image.toString()); 
      boolean exists = file.exists();
      if (!exists) {
        JOptionPane.showMessageDialog(null, "the file or directory you are searching does not exist");
      }else{
        JLabel label = new JLabel(); //Make new label where image will be on printed.
        label.setIcon(image);
        JFrame gallery = new JFrame();
        gallery.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        gallery.getContentPane().add(label);
        gallery.pack();
        gallery.setLocation(500,450);
        gallery.setVisible(true);
      }
    }
}
Это было полезно?

Решение

No need for main method, just provide all stuff in the Constructor in second class. the code==>

import java.io.; import javax.swing.; public class LoadSickLeaveImage extends Treatment{

public LoadSickLeaveImage(String fileName){
    ImageIcon image = new ImageIcon(fileName);
    File file = new File(image.toString());
    boolean exists = file.exists();
if (!exists) {    JOptionPane.showMessageDialog(null, "the file or directory you are searching does not exist");  
}else{
  JLabel label = new JLabel(); 
    label.setIcon(image);
    JFrame gallery = new JFrame();
    gallery.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    gallery.getContentPane().add(label);
    gallery.pack();
    gallery.setLocation(500,450);
    gallery.setVisible(true);

}

} }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top