Panel losing color

给你一囗甜甜゛ 提交于 2019-12-02 12:59:28

This is not an appropriate use of updateUI(), which "Resets the UI property to a value from the current look and feel." It's not clear why you invoke the method, but it may cause the color change you observe. Start by deleting the line. Failing that, please edit your question to include an sscce that exhibits the problem you describe.

Also consider using a non-null layout manager.

Change last few lines of actionPerformed method as below:

     int returnVal = filechooser.showOpenDialog(new pan());
     if(returnVal == JFileChooser.APPROVE_OPTION) {
         //since its multiselection enabled, 
         //get the selected files not selected file
         File[] files=filechooser.getSelectedFiles();
         if(files != null){
           for(File file: files){
                listModel.addElement(file);
           }
         }
     }

EDIT: Please make sure the proper exception handling for expected exceptions such as HeadlessException is done appropriately.

EXPLANATION:

  1. When browse panel is open, user may cancel the operation. In that case you shouldn't be reading the files as they were not selected. This is why, need to add a check on user selection, i.e. files were selected or not.

  2. Since filechooser is opened with setMultiSelectionEnabled as true, you need to get selected files as File[] in place of getting a single file.

  3. Since you are getting multiple files, you need to add each file in the listModel. One way is to iterate the File array and add one file at a time. Other option could be to use Arrays.asList(), get the list and add all the the files at once.

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!