Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Default system look and feel does not work

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    21
    My Mood
    Sleepy
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Default system look and feel does not work

    Hi,
    I'm trying to create a simple JFrame with a button on it, but I want this button to look like a native system one.
    Here's the code

    package tests;
     
    import java.awt.Button;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
     
    public class MyFrame extends JFrame
    {
        private Button _button;
     
        public MyFrame() throws ClassNotFoundException, InstantiationException, UnsupportedLookAndFeelException, IllegalAccessException {
            this.setLayout(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            _button = new Button("My Button");
            _button.setSize(100, 30);
            _button.setLocation(10, 20);
            this.add(_button);
     
            this.setSize(400, 300);
            this.setVisible(true);
        }
    }

    But all I get is this:

    It doesn't look like a Windows 7 button at all.
    What am I doing wrong?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Default system look and feel does not work

    Why are you using a Button instead of a JButton?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    21
    My Mood
    Sleepy
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Default system look and feel does not work

    I tried it. It looks like this:


    With this code:
    package differenttests;
     
    import java.awt.Point;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
     
     
    public class NativeLooks extends JFrame
    {
        public NativeLooks() throws ClassNotFoundException, InstantiationException,
                                    IllegalAccessException, UnsupportedLookAndFeelException {
            this.setDefaultCloseOperation(3);
            this.setLayout(null);
     
            JButton button = new JButton("Click");
            button.setSize(100, 30);
            button.setLocation(new Point(10, 10));
            this.add(button);
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     
            this.setSize(320, 240);
            this.setVisible(true);
        }
    }
    Last edited by goodguy; April 6th, 2011 at 12:43 AM.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    21
    My Mood
    Sleepy
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Default system look and feel does not work

    I found what the problem was. Such a stupid blunder of mine
    I had to set up look and feel before creating a button:
    package differenttests;
     
    import java.awt.Point;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
     
     
    public class NativeLooks extends JFrame
    {
        public NativeLooks() throws ClassNotFoundException, InstantiationException,
                                    IllegalAccessException, UnsupportedLookAndFeelException {
            this.setDefaultCloseOperation(3);
            this.setLayout(null);
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     
            JButton button = new JButton("Click");
            button.setSize(100, 30);
            button.setLocation(new Point(10, 10));
            this.add(button);
            this.setSize(320, 240);
            this.setVisible(true);
        }
    }
    Now the button looks like it's supposed to

    Last edited by goodguy; April 6th, 2011 at 12:50 AM.

Similar Threads

  1. Sharing my ByteStream, feel free to improve!
    By Verficon in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 7th, 2011, 10:44 AM
  2. Feel like I'm overthinking this problem.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2011, 08:02 AM
  3. Look & Feel
    By Asido in forum AWT / Java Swing
    Replies: 3
    Last Post: September 10th, 2010, 10:13 PM
  4. get proxy settings from the default system browser
    By reguapo in forum Java Networking
    Replies: 0
    Last Post: January 15th, 2010, 08:43 AM
  5. Replies: 2
    Last Post: October 14th, 2009, 11:10 AM