0

Here, ImageIcon imageIcon = new ImageIcon(ImageIO.read(flist[i])); it is taking too much time (12 to 15 sec) so I want to create JProgressBar when it is taking time, once JList is open thn Jprogress bar should be disable or dispose, please help me..

public static void searchPath(JComboBox<String> comboBox_1) throws IOException {

        DefaultListModel<Serializable> model = new DefaultListModel<Serializable>();
        int count = 0;

        String dDrivePath="\\StandardPath\\MyFolder\\"+comboBox_1.getSelectedItem() + "\\";
        
        String filename = "..\\config\\config.txt";
        
        BufferedReader reader = new BufferedReader(new FileReader(filename));

        try {
            String line; // as long as there are lines in the file, print them
            while ((line = reader.readLine()) != null) {

                File file = new File("\\"+"\\"+line+"\\d$" + dDrivePath);
                System.out.println("Access Path : "+ " "+file);
                File[] flist = file.listFiles();
                @SuppressWarnings("unchecked")
                JList<Serializable> sList = new JList<Serializable>(model);
                sList.setBackground(UIManager.getColor("Button.background"));
                for (int i = 0; i < flist.length; i++) {
                    // model.addElement(flist[i]);
                    String name = flist[i].toString();
                    if (name.endsWith("jpg")) {
                        ImageIcon imageIcon = new ImageIcon(ImageIO.read(flist[i]));
                        //Taking Too Much Time...
                        model.add(count++, scaleImage(imageIcon, 100, 120));
                    }
                }

                sList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                sList.setVisibleRowCount(-1);
                sList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
                sList.setFixedCellWidth(250);
                sList.setFixedCellHeight(250);

                JFrame frame = new JFrame(comboBox_1.getSelectedItem() +" "+ "Color");
                frame.getContentPane().add(new JScrollPane(sList));
                frame.getContentPane().setPreferredSize(new Dimension(1360, 768));
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
            
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    
Kaira
  • 3
  • 2
  • 2
    Don't do this in the main event thread. You will need to load the images in a background thread and then, once loaded, update the UI. You can monitor the progress using something like [this](https://stackoverflow.com/questions/24815494/using-jprogressbar-while-converting-image-to-byte-array/24815609#24815609) for example, but updating the UI is a little more complicated and will probably be best severed through the use of a `SwingWorker` – MadProgrammer Nov 07 '20 at 05:45
  • 2
    For a, somewhat, more complete example, have a look at [this](https://stackoverflow.com/questions/24835638/issues-with-swingworker-and-jprogressbar/24835935#24835935) – MadProgrammer Nov 07 '20 at 05:46

0 Answers0