pabloFdz
pabloFdz

Reputation: 157

How to choose LookAndFeel from file

I'm using JTattoo to change my application LookAndFeel. I know how to set any of the available skins, but now I want the user to be able to choose anyone of them. A string with the LAF is being saved in a file, so everytime you run the program it's supposed to read this file and set the skin according to your choice.

I've made a JDialogBox appear before the GUI just to show if the file has been read properly, and it was.

This is working:

UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");

But this is not:

UIManager.setLookAndFeel(chooseSkin());

Any idea?

PS: The String in the file is correct, I've checked it like a million times, I've tried it with quotes, without them... I don't know what to do now

EDIT: Note that there is no method called chooseSkin(), it was just an easy refference, the real method is called readFile()

Skins.java

public static String readFile() {
        String content = "";
           File file = new File("skins.txt");
           try {
               FileReader reader = new FileReader(file);
               char[] chars = new char[(int) file.length()];
               reader.read(chars);
               content = new String(chars);
               reader.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
           System.out.println(content); // ---> returns: "com.jtattoo.plaf.aluminium.AluminiumLookAndFeel"
           return content;
    }

Main.java

public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    System.out.println(Skins.readFile() =="com.jtattoo.plaf.aluminium.AluminiumLookAndFeel"); // ---> returns false
                    UIManager.setLookAndFeel(Skins.readFile());                    
                    MainGUI frame = new MainGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
}

Upvotes: 0

Views: 377

Answers (2)

Linus
Linus

Reputation: 1518

Well, there might be an extra space or new line or something in your file, there are alot of debugging tips like printing out the length of your strings.

String laf = "com.jtattoo.plaf.aluminium.AluminiumLookAndFeel";
System.out.println(Skins.readFile().length() + " , " + laf.length())

if the length isn't the same you need to make sure you write to the file correctly. It might contain a newline "\n" character at the end.

There is a method called 'equals' which will strictly compare the character-sequence. For instance:

Skins.readFile().equals("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel") 

should return true.

Also does UIManager throw an exception? (eg. java.lang.ClassNotFoundException) it should throw an exception if it can't load the LookAndFeel. If you still can't get it to work try using:

Skins.readFile().intern();

which will return the string equal to it according to the 'equals' method. (Where the character-sequence is equal)

Upvotes: 1

Menelaos
Menelaos

Reputation: 26279

Chooseskin must return a string equaling a valid path to a valid look and feel class that exists, eg "com.etc...AluminumLookAndFeel". You need to check your code that reads the file and that the string is valud. A debugger or println csn br used for this.

Upvotes: 0

Related Questions