JFileChooser on Mac cannot see files named by Chinese chars?
Asked Answered
B

1

1

The program worked fine when running in Intellij (it can see the Chinese named files).

I built it into a .jar file. Executed the jar and the JFileChooser cannot see those files.

And I tried the jar in Windows, it works completely fine.

Beezer answered 28/12, 2012 at 16:32 Comment(1)
So it works on Mac in IntelliJ, but does not work when you ... what? Also, when you say "cannot see" do you mean that they do not appear at all, or that they are shown but not properly (like garbled characters, etc.)?Orvalorvan
O
1

This works file for me on Mac OS X 10.8.2:

import java.io.File;
import javax.swing.JFileChooser;
public class JFileChooserTest
{
  public static void main(String[] args)
  {
    System.out.println("file.encoding=" + System.getProperty("file.encoding"));
    String path;

    if(args.length > 0)
      path = args[0];
    else
      path = System.getProperty("user.dir", ".");

    File dir = new File(path);

    JFileChooser jfc = new JFileChooser(dir);
    int result = jfc.showOpenDialog(null);

    switch(result) {
      case JFileChooser.CANCEL_OPTION:
        System.out.println("User cancelled OPEN dialog.");
        break;
      case JFileChooser.APPROVE_OPTION:
        System.out.println("User chose file: " + jfc.getSelectedFile());
        break;
      case JFileChooser.ERROR_OPTION:
        System.out.println("User encountered an error");
        break;
     default:
       System.out.println("Confused");
       break;
    }

    System.exit(0);
  }
}

Here's a sample run:

$ java -showversion JFileChooserTest 
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

file.encoding=UTF-8
User chose file: /.../测试文件.txt

Here's another sample run:

$ java -showversion -Dfile.encoding=ISO-8859-1 JFileChooserTest 
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

file.encoding=ISO-8859-1
User chose file: /.../????.txt

In both cases, the file-selection dialog properly displayed the name of the file (测试文件.txt).

Note that using java.awt.FileDialog will get you the platform-specific file dialog that most Mac OS users are used to seeing. Though it's not strictly Swing (and has an abysmally small feature set), it is probably superior to JFileChooser for things like OPEN and SAVE dialogs. (It also shows Chinese characters without a problem on my system).

Orvalorvan answered 28/12, 2012 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.