3

I want to know how I can make a JFileChooser in the Swedish language. Unfortunately it turns out that if I could use a JDK version 10 or earlier i could actually do this by creating a Locale object and make it the default. But we are discouraged to use this old version for any new programs. But from JDK version 11 and forward choosing Swedish Locale no longer works.

So I have seen that people have been able to change individual strings for the FileChooser like in this example: https://coderanch.com/t/475470/java/customizing-JFIleChooser But this feels unsatisfactory to me. At least for as long as I do not know how I can learn about how I can find those Strings and where they are located. Maybe I will run into the same problem again with some other GUI component and then I still don't know what to do.

This works with JDK v. 10 or earlier:


//This is in my Main:

        Locale sverje = new Locale("sv", "SE"); //Creates a instance of Locale for Swedish, Sweden.
        Locale.setDefault(sverje); //Sets this to default.

//Then I call a class that opens a Frame and handles the FileChooser.

This is an example of modifying an individual string:

UIManager.put("FileChooser.cancelButtonText", "Cancelar"); //Changing cancel button text to Portugese

If I need to use this method it would be nice to know how to find the strings like I said before.

It would also be nice to know what is going on when you "put" in the new String value like in the example.

I believe that the JFileChooser somehow inherits the Strings in several steps, and that is what allows us to write "FileChooser.cancelButtonText" even if there actually is no "cancelButtonText" in the FileChooser class, rather (I'm guessing!) it inherits it from a so called ResourceBundle and then from there to the Localeclass and from there to some other class or classes before the FileChooser uses it.

So is this a correct assumption and how can you trace the Key / name / location of those strings from the FileChooser class to where the actually are?

Is it the ResourceBundle or the FileChooser or some other part that was changed since it stopped working from version 11? Can / should I try to make changes to the ResourceBundle to fix this in the most professional way?

Puce
  • 37,247
  • 13
  • 80
  • 152
  • [Another similar question](https://stackoverflow.com/q/74475473/6746785) was asked earlier, so I just link to it here. As for the question I think the `UIManager` keys depend on the selected [L&F](https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html). If that's true, but you also go with the `UIManager` solution, then you will be tied to the selected L&F (which in its turn should mean the default cross platfrom L&F since this is the reason I am assuming you are using Java in the first place). – gthanop Jan 30 '23 at 20:31

2 Answers2

1

You can find the full list (including FileChooser keys) in the first answer of this post: List of Java Swing UI properties?

I think it will be enough setting only the properties ending in "Text".

AFAIK UIManager.put is just updating UIDefaults (the map that contains all the default names) so, as long as there is just one language available in the application, there is no need to use a ResourceBundle.

alcan
  • 31
  • 1
  • 8
  • Thank you! It looks really promising. I will test it as soon as I get a chance. However I would also like to know how I can find and interact with the Resource Bundles that come as standard. It would be interesting to look for the keys there and compare the different Languages / Locales. any ideas where to look for those? – Oscar Josefsson Jan 30 '23 at 21:14
  • I just found this post where they explain how setDefaultLocale is affecting to the JFileChooser (it doesn't change the language of the user interface), so if you want to translate the entire UIDefaults, you can create a ResourceBundle with all the names and do UIManager.getDefaults().addResourceBundle. This way UIManager will search each key in the bundle before looking in UIDefaults. https://stackoverflow.com/questions/58873985/how-can-my-default-locale-be-taken-into-account-by-the-swing-components (also edited my response) – alcan Jan 30 '23 at 22:04
  • Thanks again for your support! I have now looked at this link: https://stackoverflow.com/questions/1951558/list-of-java-swing-ui-properties From there I felt like this link was the best for text keys: https://thebadprogrammer.com/swing-uimanager-keys/ If one is looking for graphics and icons some of the other links will be better. Still this key was missing: "saveInLabelText". This one is for the "Save in" text in the save file dialogue. But I managed to figure this out. :p Seems like I have managed to replace all the texts in both the open and save windows of the JFileChooser. – Oscar Josefsson Jan 31 '23 at 18:56
  • Yes I am very curious about ResourceBundles. But not really in creating my own. I am much more interested in finding the built in ones. Any idea how I can get to them? They must exist I guess since changing Locale to e.g. Germany is able to automatically replace all texts in the FileChooser. – Oscar Josefsson Jan 31 '23 at 18:56
  • I think it works with germany because this language is in the user interface translation list of the JRE version that you are using (for example, this is the list of jre10 supported locales: https://www.oracle.com/java/technologies/javase/jdk10-and-jre10-suported-locales.html#translation). After setting one of these supported locales, you should be able to access to the translated texts using UIManager.getDefaults() (that return the default hashtable of all the texts). HTH! – alcan Feb 01 '23 at 20:28
1

So correct me if I am wrong. But I think the answer is something like this: If you want to use one of the languages that is supported for the Swing Component just set the correct Locale and you are done.

If not it seems like using UIManager.put is the best way to go.

I was hoping to find a resource bundle containing all of the text strings but now I do no longer think that such a thing exists.

So my understanding at the moment is this:

The JComponents such as the FileChooser are generalized Components that are meant to work on many different platforms and operative systems.

And they use classes called "Look & Feel" to get their final form depending on the intended Platform.

This is where the UIManager comes in.

It allows you to decide what "Look & Feel" you want to use.

So in my case it will use a look and feel called: "com.sun.java.swing.plaf.windows.WindowsLookAndFeel".

You can get this information using:

System.out.println(UIManager.getSystemLookAndFeelClassName()); 

And I believe that the UIManager will create classes called UIDefaults using some algorithm.

The UIDefaults will contain the values that gives the JComponents their "Look & Feel" i.e. their appearance & behaviour.

But the UIDefaults classes are not pre-existing. They are created based on setup.

However I believe that they will get some of their data from a resource file that is not truly a ResourceBundle but at least extends the ResourceBundle class. But this still does not contain all the necessary Strings to fully customize the FileChooser. This link shows an example of these resource files that are specific to the Windows version (L&F) of the FileChooser: https://code.yawk.at/java/6/com/sun/java/swing/plaf/windows/resources/

So the data is apparently split up and it does not come from a single source. It is collected from different sources and put into the UIDefaults class that works like a Database for the values.

To sum it all up. The best way to go might be to create a class that has a method to put all the String values into the UIDefaults using the UIManager. And I suppose you could prepare for internationalization by letting this method get it's strings from a resource bundle that you have created yourself.

The best list of Keys seems to be this one: https://thebadprogrammer.com/swing-uimanager-keys/

But unfortunately although seemingly exhausting there is still information missing.

To fully customize all the Strings in the FileChooser in addition to the Keys you will find in this list you will also need to change the value of this Key:

"saveInLabelText".

So these are all the String I changed and I believe they will customize most of the Strings for the Windows version of the FileChooser:

        //Strings for Open file-dialogue:   ***************************************************************************
    
    //The numbers in the comments refers to the order of appearance of the Stirng values in the Frame
    
    UIManager.put("FileChooser.openDialogTitleText","Open");                //1.        
    UIManager.put("FileChooser.lookInLabelText","Look in:");                //2.
    UIManager.put("FileChooser.fileNameLabelText","File name:");                //3.
    UIManager.put("FileChooser.filesOfTypeLabelText","Files of type:");     //4.     
    UIManager.put("FileChooser.openButtonText","Open");                 //5.            
    UIManager.put("FileChooser.cancelButtonText", "Cancel");                //6.        
    UIManager.put("FileChooser.acceptAllFileFilterText","All Files");       //7.
    
    //End of Open file-dialogue ***************************************************************************
    
    //Strings for Save file-dialogue    ***************************************************************************
    
    UIManager.put("FileChooser.saveDialogTitleText","Save as");     //1.        
    UIManager.put("FileChooser.saveInLabelText","Save in:");        //2.  This was not in the list but works!
    // The strings with number 3 & 4 are the same as the ones for the Open file-dialogue so you do not need to set them here.
    UIManager.put("FileChooser.saveButtonText","Save");     //5.