Using Napkin Lookandfeel

Asked

Viewed 121 times

1

I’m using the Eclipse IDE and I have this code:

package br.com.caelum.argentum.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class ArgentumUI {

    private JFrame janela;
    private JPanel painelPrincipal;
    public static void main(String[] args) {
        try {  
            UIManager.setLookAndFeel("napkin.NapkinLookAndFeel");  
        } catch (Exception e) {  
               e.printStackTrace();  
        }
        new ArgentumUI().montaTela();
    }

    public void montaTela() {
        preparaJanela();
        preparaPainelPrincipal();
        preparaBotaoCarregar();
        preparaBotaoSair();
        mostraJanela();
    }


    private void mostraJanela() {
        // TODO Auto-generated method stub
        janela.pack();
        janela.setSize(540, 540);
        janela.setVisible(true);
    }

    private void preparaBotaoSair() {
        // TODO Auto-generated method stub
        JButton botaoSair = new JButton("Sair");
        botaoSair.setMnemonic(KeyEvent.VK_S);
        botaoSair.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        painelPrincipal.add(botaoSair);
    }

    private void preparaBotaoCarregar() {
        // TODO Auto-generated method stub
        JButton botaoCarregar = new JButton("Carregar XML");
        botaoCarregar.setMnemonic(KeyEvent.VK_C);
        botaoCarregar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new EscolhedorDeXML().escolhe();
            }
        });
        painelPrincipal.add(botaoCarregar);

    }

    private void preparaPainelPrincipal() {
        // TODO Auto-generated method stub
        painelPrincipal = new JPanel();
        janela.add(painelPrincipal);
    }

    private void preparaJanela() {
        // TODO Auto-generated method stub
        janela = new JFrame("Argentum");
        janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Napkin is already in Referenced Libraries, but I’m getting these errors:

keys we didn't overwrite: []
Exception in thread "main" java.awt.IllegalComponentStateException: The frame is decorated
    at java.awt.Frame.setBackground(Frame.java:986)
    at javax.swing.JFrame.frameInit(JFrame.java:253)
    at javax.swing.JFrame.<init>(JFrame.java:219)
    at br.com.caelum.argentum.ui.ArgentumUI.preparaJanela(ArgentumUI.java:77)
    at br.com.caelum.argentum.ui.ArgentumUI.montaTela(ArgentumUI.java:26)
    at br.com.caelum.argentum.ui.ArgentumUI.main(ArgentumUI.java:22)

How to correct?

1 answer

1


Victor, here it is (This code is hard to run).

Aplicativo rodando no Windows 7

Aplicativo rodando no openSUSE 13.1 / KDE Plasma

There appears to have been a compatibility breakdown between JDK 6 and JDK 7 for translucent windows (see that post on Soen). Napkin dependent on that Feature (see Error in Napkin Look and Feel at Coderanch).

The workaround suggested by Rob Spoor is:

  • Do not configure the look & Feel right away.
  • Create your user interface.
  • Call setUndecorated(true) in frame.
  • Configure the look & Feel.
  • Call SwingUtilities.updateComponentTreeUI to the frame.
  • If necessary, call setUndecorated(false) in frame.

In your case it was not necessary to call setUndecorated.

I’ve made the following changes:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                ArgentumUI ui = new ArgentumUI();
                ui.montaTela();
                UIManager.setLookAndFeel("napkin.NapkinLookAndFeel");
                SwingUtilities.updateComponentTreeUI(ui.janela);
                ui.mostraJanela();
            } catch (Exception ex) {
                Logger.getLogger(ArgentumUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

And updated the method montaJanela not to display the window immediately after construction:

public void montaTela() {
    preparaJanela();
    preparaPainelPrincipal();
    preparaBotaoCarregar();
    preparaBotaoSair();
    //mostraJanela();
}

And it all worked out the way it should.


The issue of SwingUtilities.invokeLater is not strictly necessary to make the code run (see Swing Application. Why the main method should dispatch the creation of the GUI to the EDT?), however, it is better to be safe than sorry. Oracle tutorial makes clear that almost every code that creates or interacts with Swing components should run on EDT.

  • Thanks for the help Anthony, but another problem has arisen now ... When you click XML load the new Chooserdexml.choose() line is executed, and it generates a dialog box with nothing... Not to abuse your goodwill, but could you give me a hand? I’ll leave the link to Gist with the code of that class. Thank you https://gist.github.com/VictorGee/430474575c68c04d3212

  • Hi Victor, it’s the same problem only inside a JDialog created by JFileChooser, I don’t see any simple way to make it spin. You can try opening another question, but I think you should try running the app with another Look And Feel.

  • Okay, Anthony, thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.