0

Using JFileChooser for Java Swing projects.

I made a code to load the image and print it out to the label. However, the image rendering on the label is too slow. (30 seconds to 60 seconds)

The file size is 30KB to 50KB.

moviePosterButton.addActionListener(new ActionListener() {
        
        @Override
        public void actionPerformed(ActionEvent e) {
            
            JFileChooser movieImageChooser = new JFileChooser();
            movieImageChooser.setDialogTitle("image load");
            int returnVal = movieImageChooser.showOpenDialog(frame);
            
            if(returnVal == JFileChooser.APPROVE_OPTION) {
            filePath = movieImageChooser.getSelectedFile().getPath();
            fileName = movieImageChooser.getSelectedFile().getName();
            System.out.println(fileName);
            System.out.println(filePath);
            image_save = new File(filePath);
            
            JLabel moviePosterPrintLabel = new JLabel();
            moviePosterPrintLabel.setIcon(new ImageIcon(filePath));
            moviePosterPrintLabel.setBounds(70, 140, 230, 342);
            movieUploadPanel.add(moviePosterPrintLabel);
            }
        }
    }); 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Suho Lee
  • 3
  • 2
  • Boy oh boy.. where to start. 1) The 5 code lines from / to `filePath = movieImageChooser.getSelectedFile().getPath(); ... image_save = new File(filePath);` can be replaced with `image_save = movieImageChooser.getSelectedFile().getParentFile();` 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. 3) Add an empty `JLabel` to the `movieUploadPanel` when it is constructed. Set the icon when needed. 4) `setBounds(70, 140, 230, 342);`.. – Andrew Thompson Jun 02 '21 at 21:20
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 5) I doubt the file chooser is of any relevance to the problem. That is yet to be proven. It's all about the image itself and the rest of the code. 6) Speaking of the image, please upload it somewhere we can access. .. – Andrew Thompson Jun 02 '21 at 21:22
  • 7) Speaking of the code .. For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 8) And referring back to uploading the image, hot link to it. Here is a copy/paste tip I commonly use in regard to that: One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jun 02 '21 at 21:24
  • 9) *"The file size is 30KB to 50KB."* How big is the image in pixels? Some image compression formats can produce a huge reduction in file size, but as it is loaded, it needs to be expanded back to the large (in pixels) image without compression. – Andrew Thompson Jun 02 '21 at 21:29
  • 1
    I really want to thank you for your help!! – Suho Lee Jun 03 '21 at 12:47

0 Answers0