views:

228

answers:

3

I use JButtons in my application. They need to have different colors. First I used that btn.setBackground(col);. It works on my computer and on another computer my button just gray (not red, as it's supposed to be).

Trying to solve this problem I decided to use images. I do it in the following way: tmp = new JButton(newIcon);

Again, it works fine on my computer and on another computer I see just gray buttons.

Does anybody have any ideas what can be the reason of the problem and how it can be solved? I heard it can be related to "look-and-feel of the native system". But I do not know what it means and what should I do if it is the case? Can anybody pleas, help me with that?

+2  A: 

Swing has the concept of pluggable look and feels. Some of those look and feels mimic the look of native components from Windows, GTK+, etc. Even on Windows there are two separate look and feels - Classic and Vista I think. Maybe you're using different OSes on your two systems and an GUI designer that sets the look and feels automatically. Most don't however - the default look and feel Metal(before Java 1.6.10) and Nimbus look the same on every OS.

   // build the look and feel section
    final LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
    List<String> lookAndFeelNames = new ArrayList<String>();
    lookAndFeelNames.add("System");

    for (LookAndFeelInfo lookAndFeelInfo : lookAndFeelInfos) {
        if (!lookAndFeelInfo.getName().equals("CDE/Motif")) {
            lookAndFeelNames.add(lookAndFeelInfo.getName());
        }
    }

  if (selectedLookAndFeel.equals("System")) {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                        java.util.logging.Logger.getLogger(SpellbookFrame.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        java.util.logging.Logger.getLogger(SpellbookFrame.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        java.util.logging.Logger.getLogger(SpellbookFrame.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (UnsupportedLookAndFeelException ex) {
                        java.util.logging.Logger.getLogger(SpellbookFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    for (LookAndFeelInfo lookAndFeelInfo : lookAndFeelInfos) {
                        if (lookAndFeelInfo.getName().equals(selectedLookAndFeel)) {
                            try {
                                UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
                            } catch (ClassNotFoundException e1) {
                                e1.printStackTrace();
                            } catch (InstantiationException e1) {
                                e1.printStackTrace();
                            } catch (IllegalAccessException e1) {
                                e1.printStackTrace();
                            } catch (UnsupportedLookAndFeelException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                }

                SwingUtilities.updateComponentTreeUI(tabbedPane);
                SwingUtilities.updateComponentTreeUI(parent);

The first part of the code build a list of the available look and feel names and the second acts upon one of them being selected. But since you want always to use the same laf you can use something like:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
Bozhidar Batsov
But what can I do to fix the problem? Can I force my software to use a fixed Look and feels? Can it be that the look and feel that I use in my software is not available on another computer?
Roman
Yes, you can. I'll update my post to feature a code snippet that I use.
Bozhidar Batsov
+2  A: 

Yes, it depends upon the OS. The same buttons may be look different in the different OS like Windows, WindowsClassic,etc., You can view the difference your system by applying the different "Look And Feel". For that you should aware about UIManager Class.

For ex, For WindowsClassic,

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");

For Nimbus look and feel,

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

For more details, see the link as follows.

Look And Feel Tutorials

Venkats
Is it correct to say that I can remove the mentioned problem if I explicitly set a specific look and feel? In this way my program will use the same look and feel on different computers and everything should look the same. Or I am wrong?
Roman
A: 

The look and feel (LaF) is the appearance your java app will get, and it depends of your operational system.

You can set an external look and feel for your application to avoid this problem

Try to look Substance look and feel. The web site contains the library and teachs how to use it https://substance.dev.java.net/

You can define the appearance of your GUI and exporting the library with your program everybody will get the same Look and Feel.

marionmaiden