0

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();
                }
            }
        });
}
pabloFdz
  • 157
  • 1
  • 11
  • 1) Make sure chooseSkin() is a method of return-type 'String' and also include the code in order for us to help you. 2) Try this before setting the LookAndFeel: `System.out.println(chooseSkin());` you could also try doing this: `System.out.println(chooseSkin() =="com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");` (should print out "true" if it is correct) printing out variables is great for debugging. Alternatively you can try using `chooseSkin().toString();`, in some cases it will work. – Linus Jan 11 '14 at 17:37
  • I didn't know that `System.out.println(chooseSkin() =="com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");` would return me **true** or **false**, I've just tried it and it returns false... I'm editing the question to show you the code. Thanks – pabloFdz Jan 11 '14 at 18:12

2 Answers2

1

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)

Linus
  • 1,516
  • 17
  • 35
  • Thanks for your answer but I couldn't find the error... but thanks to it, I could see the Strings were not equals. I discovered that I can use an **if** to check if String **contains** "aluminium" for example, and then create a switch() where I return different Strings deppending on the case... quite messy, but that's the only way to make it work. – pabloFdz Jan 11 '14 at 19:48
  • 1
    Okay, glad I could help. But from what you're saying it really seems like there is a error in your file. Have you tried **manually** putting the string into skins.txt? I ran your code and it worked with the 'nimbus look and feel'. So if it works by putting the exact string in the file, it has probably something to do with how you write to the file. It might also be how you read the file, check [this](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file?answertab=votes#tab-top) out – Linus Jan 11 '14 at 19:59
  • Wow... I feel so stupid right now, I was trying the app with "skins.txt" file opened in my .txt editor... that was why it was not working. Thanks for telling me that my code worked :) – pabloFdz Jan 11 '14 at 20:13
  • Do you know the reason of that? Now I would like to know why if I had the file opened it didn't work. The String was the same, nothing changed. – pabloFdz Jan 11 '14 at 20:18
  • @Mr.Koala Well, I think you had a cached verision open in your txt editor, in that case it wouldn't be guaranteed to be equal to the txt file on your disk. But it sounds weird.. – Linus Jan 11 '14 at 20:49
0

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.

Menelaos
  • 23,508
  • 18
  • 90
  • 155