Wednesday, 13 November 2013

Using setDefaultLookAndFeelDecorated() in JFrame

The following illustrates setting a default look and feel deocrated in JFrame. I think, I have done it once, got the desired output. But later, I forgot so I didn't share it in my blog. I know the method, but the ridiculous thing I am doing is that I recklessly forgot the static keyword in front of the setDefaultLookAndFeelDecorated() method. I've created a JFrame object, made it visible and then I am trying to set the default look and feel decorated. How funny that I forgot such a simple point, I'll have to confess. When the frame is visible, the such changes to it doesn't occur. Just like in the JFileChooser, where you cannot set the accessory after the JFileChooser is shown, we can't do this too.

But what is default look and feel decorated? Well, I can't describe it in words, just see the output window, you'll understand what it really is?

A window with default look and feel (metal) decorated


import javax.swing.*;
import java.awt.*;
class LookAndFeelDecorated extends JFrame
{
JButton b;

    public LookAndFeelDecorated()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("Look And Feel Decorated");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        b=new JButton("Button");
        add(b);
       
        setSize(400,400);
        setVisible(true);
    }
   
    public static void main(String args[])
    {
        // Do it here, before creating object for the
        // JFrame!, Mandatory!
        JFrame.setDefaultLookAndFeelDecorated(true);
        new LookAndFeelDecorated();
    }
}

The method setDefaultLookAndFeelDecorated() works only in MetalLookAndFeel as far as I am concerned because it didn't in others in my practice.

Also see double titlebar for JFrame

The greatest compliment you can give to me is sharing this post. I sincerely appreciate it :)

“When you are afraid to act for fear of being wrong, then you have ignored the chance of doing right.” - AK

No comments:

Post a Comment