Pregunta

I am new to java, I facing a problem while trying to change the look and feel of a java file when invoked from another class. I have two classes, main.java and auth.java. I have set the look and feel as nimbus in the auth.java file. When I try to invoke auth class from main.java(i have attached the code snippet below) the UI doesn't change. But when i use public static void main(String[] args) in auth.java and try to run that file individually the UI of the jFrame changes. Kindly let me know if there is a way to change the UI of a jFrame when called from another class. And is there any problem invoking the jFrame from another class, is that a good practice? because we are onto doing a big project and will require calling authentication frame once in a while, so any suggestion guys? Thank you all in advance! :)

main.java:

package com.package.name;
public class main {
    public static void main(String[] args) {
        new auth();
    }
}

This is a section of my auth.java code:

package com.package.name;
import javax.swing.*; 
import java.awt.*;

public class auth extends JFrame {
    public auth() {
        initComponents();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel ( "javax.swing.plaf.nimbus.NimbusLookAndFeel" );
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }
                setVisible(true);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                pack();
            }
        });
    }
¿Fue útil?

Solución

You've already called initComponents before changing the look at feel. This will install what ever is the currently installed look and feel into those components.

Without having to go and instruct each component to update its UI, you could simply swap the installation of the look and feel with the initComponents method, for example...

try {
    UIManager.setLookAndFeel ( "javax.swing.plaf.nimbus.NimbusLookAndFeel" );
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
    e.printStackTrace();
}
initComponents();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();

Now, having said that. You really should have been within the context of the EDT before you called the auth constructor and auth shouldn't really be making decisions about the Look and Feel. What happens if you want to use this frame again with a different look and feel?

Instead could do something more like

public class main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel ( "javax.swing.plaf.nimbus.NimbusLookAndFeel" );
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }
                new auth();
            }
        });
    }
}

public class auth extends JFrame {
    public auth() {
        initComponents();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

You should also consider taking a look through Code Conventions for the Java Programming Language, it will make your code much easier to read ;)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top