1

I am trying to update my look and feel without any errors, but I can't figure out what I am doing wrong.

This is my Window class:

public class Window extends JFrame {
    private static final long serialVersionUID = 1L;

    public Window() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new MigLayout());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setMinimumSize(new Dimension(600, 700));
        setVisible(true);

        setContentPane(new JPanel() {
            private static final long serialVersionUID = 1L;

            public void paintComponent(Graphics g) {
                g.drawImage(new ImageIcon("start.jpg").getImage(), 0, 0, getWidth(),     
                    getHeight(), this);
            }
        });
    }
}

And this is my main where I update the UI (Look and Feel)

public class Main {
    public static void main(String[] args) {
        Window.setDefaultLookAndFeelDecorated(true);
        try {
            UIManager.setLookAndFeel(new SubstanceGraphiteLookAndFeel());
        }
        catch (UnsupportedLookAndFeelException e) {}
        Window window = new Window();
    }
}

The console says my error comes from this line: Window window = new Window();

Then this line: setContentPane(new JPanel() {

But if I delete the whole setContentPane bloc, the error then points to the constructor.

Any help would be appreciated. Thank you!

Lucia Pasarin
  • 2,268
  • 1
  • 21
  • 37
  • Sorry, but what error? – Lews Therin May 14 '13 at 21:11
  • The error is like 200 lines :S That's why I just posted where the errors were pointing. –  May 14 '13 at 21:16
  • You can't have a static reference in an anonymous class for starters. If Window is inner class, then the same thing applies. – MadProgrammer May 14 '13 at 21:24
  • Going to need some information on the error you're getting. "The error" just isn't enough to go on. – Adrian May 14 '13 at 21:24
  • Btw, you are setting the layout manager on the JFrame but change the content pane afterwards (calling setLayout on the JFrame will actually set it on the content pane), so the call is useless. You should make sure to call `JFrame.setVisible(true);` as the last line. Consider also calling `JFrame.pack()` just before making it visible – Guillaume Polet May 14 '13 at 21:32

1 Answers1

3
  1. SubstanceGraphiteLookAndFeel() must be wrapped into invokeLater()

  2. Window window = new Window(); shold be wrapped into invokeLater(), more see in Oracle tutorial Initial Thread

  3. g.drawImage(new ImageIcon("start.jpg").getImage(), 0, 0, getWidth(), getHeight(), this);

    • use BufferedImage, load as local variable

    • paintComponent is invoked from every mouse, key events and internal methods implemented in APIs, then quite hard and long ImageIcon("start.jpg").getImage() is called repeatly, how many times any event is fired for repaint,

    • load to local variable

  4. setContentPane(new JPanel() {, override getMin/Max/PreferredSize

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you for the answer. Can you briefly explain why I need to wrap those elements into invokeLater()? –  May 14 '13 at 21:30
  • 1
    @Maxwell To ensure that all UI-related operations are performed on the EDT (Event Dispatching Thread). Google it to know more). +1 – Guillaume Polet May 14 '13 at 21:34
  • 1
    Substance must be set on EDT then wrapped into invokeLater, Window window = new Window() please to read official Oracle tutorial about Initial Thread and Event Dispatch Thread, for more info to click to Substance tag – mKorbel May 14 '13 at 21:34