Search code examples
javaswingjframejfilechooser

How can I not show the open dialog first?


Well, the only problem I am having is that the open dialog is showing first. What I want is to only to put it inside the JFrame and then do the rest inside the JFrame like opening image and display it. It should be like this

The problem is that my JFileChooser is showing first. And also the thing is I want it all to be inside the JFrame just like in the image shown.

Here is my code:

import java.awt.Image;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.border.*;

import java.awt.Color;
import java.awt.Dimension;


public class imgviewer extends JFrame{
    
    JButton button;
    JLabel label;
    
    public imgviewer() {
        super("Image viewer");
        label = new JLabel();
        label.setBounds(400, 10, 180, 300);
        Border b = BorderFactory.createLineBorder(Color.ORANGE, 2);
        JFileChooser file = new JFileChooser(".");
        label.setBorder(b);
        add(label);
        file.setPreferredSize(new Dimension(400, 300));
        
        file.setCurrentDirectory(new File(System.getProperty("user.home")));
        FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg", "png", "gif");
        file.addChoosableFileFilter(filter);
            int result = file.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = file.getSelectedFile();
                String path = selectedFile.getAbsolutePath();
                label.setIcon(ResizeImage(path));
            }
                
            else if (result == JFileChooser.CANCEL_OPTION) {
                System.out.println("No File Selected");
            }
        
        add(file);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(600, 350);
        setVisible(true);
    }
    
     public ImageIcon ResizeImage(String ImagePath) {
         ImageIcon MyImage = new ImageIcon(ImagePath);
         Image img = MyImage.getImage();
         Image newImg = img.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
         ImageIcon image = new ImageIcon(newImg);
         return image;
     }
    
    public static void main(String[] args) {
        new imgviewer();
    }
}

Solution

  • I'm not sure how to do what you want to do, but where you put the line int result = file.showOpenDialog(null); into your code, that's a line that explicitly means "open this JFileChooser in its own file dialog NOW". In the order of your code, that clearly happens before the setVisible(true) line which would open the wrapper you wish to put around the file dialog.

    Is there a reason the code after file.addChoosableFileFilter(filter);, and before add(file), is indented? If you were imagining that this code is in a separate block which will prevent it from being executed until later, it isn't.

    I haven't worked with JFileChooser, or Swing in general, for a long time now, so I don't know off hand how well a JFileChooser will work if treated as a separate component embedded inside another component, but if that's going to work at all, you definitely cannot use JFileChooser's showOpenDialog method.