Java 我想在For循环运行时运行JProgressBar,是否可能?

Java 我想在For循环运行时运行JProgressBar,是否可能?,java,Java,这里,ImageIcon=newImageIcon(ImageIO.read(flist[i]);它占用了太多的时间(12到15秒),所以我想在需要时间时创建JProgressBar,一旦JList打开,JProgressBar应该被禁用或处置,请帮助我 public static void searchPath(JComboBox<String> comboBox_1) throws IOException { DefaultListModel<Serial

这里,ImageIcon=newImageIcon(ImageIO.read(flist[i]);它占用了太多的时间(12到15秒),所以我想在需要时间时创建JProgressBar,一旦JList打开,JProgressBar应该被禁用或处置,请帮助我

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
        }
    }
    
publicstaticvoidsearchpath(JComboBox组合框_1)抛出IOException{
DefaultListModel=新的DefaultListModel();
整数计数=0;
字符串dDrivePath=“\\StandardPath\\MyFolder\\”+组合框\u 1.getSelectedItem()+“\\”;
字符串filename=“..\\config\\config.txt”;
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
试一试{
String line;//只要文件中有行,就打印它们
而((line=reader.readLine())!=null){
File File=新文件(“\\”+“\\”+行+“\\d$”+dDrivePath);
System.out.println(“访问路径:“+”+文件);
File[]flist=File.listFiles();
@抑制警告(“未选中”)
JList sList=新JList(型号);
sList.setBackground(UIManager.getColor(“Button.background”);
for(int i=0;i
不要在主事件线程中执行此操作。您需要在后台线程中加载图像,然后在加载后更新UI。例如,您可以使用类似的方法来监控进度,但更新UI要复杂一些,最好使用
SwingWorker
来断开。对于更完整的示例,请查看