Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How do you stop this behavior in JFileChooser?  (Read 923 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2015-06-24 02:49:48 »

I got to try this new GIF video player. Never saw this before when I returned 4 days ago.

Click to Play


When I double-click on a file or a folder, the text field pointed out by the red rectangle will be filled in with the full absolute path to that file/directory. I would like to remove that behavior.

This is my attempt:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
JFileChooser opener = new JFileChooser();
opener.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
opener.setCurrentDirectory(FileControl.lastSavedDirectory);
opener.setFileFilter(new FileNameExtensionFilter("PNG files", "png"));
opener.addPropertyChangeListener(new PropertyChangeListener(){
   public void propertyChange(PropertyChangeEvent event){
      if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(event.getPropertyName())){
         JFileChooser chooser = (JFileChooser) event.getSource();
         File selectedFile = chooser.getSelectedFile();
         MetalFileChooserUI chooserUI = (MetalFileChooserUI) chooser.getUI();
         if (selectedFile != null && chooserUI instanceof BasicFileChooserUI){
            chooserUI.setFileName(selectedFile.getName());
         }
      }
   }
});
opener.setVisible(true);


It works when I click on a file or directory folder 1 mouse button click at a time, but when I double-click it, the issue remains unsolved. Can anyone help me fix this?
Offline tom_mai78101
« Reply #1 - Posted 2015-06-24 05:58:43 »

Okay, I figured it out. I got myself a clue to fix this, thanks to this post here.

First, you need to have a method capable of returning a JList full of all Components in JFileChooser:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
@SuppressWarnings("unchecked")
private JList<Class<?>> findFileList(Component comp){
   if (comp instanceof JList){
      return (JList<Class<?>>) comp;
   }
   if (comp instanceof Container){
      for (Component c : ((Container) comp).getComponents()){
         JList<Class<?>> list = findFileList(c);
         if (list != null){
            return list;
         }
      }
   }
   return null;
}


Next, you can then remove all MouseListeners, including MouseListeners associated with double-clicks. Unfortunately, this will also remove the ability to double-click on files you actually want to open. I added back in a custom MouseListener that you can use to handle those scenarios.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
final JFileChooser opener = new JFileChooser();
JList<Class<?>> list = findFileList(opener);
LOOP_TEMP1: for (MouseListener l : list.getMouseListeners()){
   if (l.getClass().getName().indexOf("FilePane") >= 0){
      list.removeMouseListener(l);
      list.addMouseListener(new MouseListener(){
         @Override
         public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 1){
               File file = opener.getSelectedFile();
               if (file != null){
                  MetalFileChooserUI ui = (MetalFileChooserUI) opener.getUI();
                  ui.setFileName(file.getName());
               }
            }
            else if (e.getClickCount() == 2){
               File file = opener.getSelectedFile();
               if (file != null){
                  if (file.isDirectory()){
                     opener.setCurrentDirectory(file);
                  }
                  else if (file.isFile()){
                     opener.setSelectedFile(file);
                  }
                  BasicFileChooserUI ui = (BasicFileChooserUI) opener.getUI();
                  ui.setFileName(file.getName());  
               }
            }
         }
         @Override
         public void mouseEntered(MouseEvent e) {
         }
         @Override
         public void mouseExited(MouseEvent e) {
         }
         @Override
         public void mousePressed(MouseEvent e) {
         }
         @Override
         public void mouseReleased(MouseEvent e) {
         }
      });
      break LOOP_TEMP1;
   }
}


That fixes the issue. Now, the user has to press the "Open" or "Save" button, depending on what dialog was shown by JFileChooser, and it's all good to go.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (545 views)
2018-06-10 19:43:48

EgonOlsen (649 views)
2018-06-10 19:43:44

EgonOlsen (445 views)
2018-06-10 19:43:20

DesertCoockie (790 views)
2018-05-13 18:23:11

nelsongames (1024 views)
2018-04-24 18:15:36

nelsongames (1058 views)
2018-04-24 18:14:32

ivj94 (1637 views)
2018-03-24 14:47:39

ivj94 (562 views)
2018-03-24 14:46:31

ivj94 (1443 views)
2018-03-24 14:43:53

Solater (567 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!