How to make a window look like this in Java?
Asked Answered
P

5

6

How do I create a window which looks like this in Java:

frame

I want that window layout, instead of the standard Windows-borders, and I don't know how this is called.

Edit: look and feel doesn't work for me:

not working for me

Pepe answered 22/2, 2011 at 7:24 Comment(1)
Are you using swing or awt? I know that Netbeans platform has good support for multiplatform look and feel design - its garanteed to look the same.Magnetize
T
10

If you want your Look and Feel to draw the window decoration (that's what the "border" is called), then you need to call JFrame.setDefaultLookAndFeelDecorated(true) before creating your JFrame objects and JDialog.setDefaultLookAndFeelDecorated(true) before creating your JDialog objects.

Thinnish answered 22/2, 2011 at 8:29 Comment(0)
A
3

that's called look and feel, you can find a detailed explanation here http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

Annulus answered 22/2, 2011 at 7:29 Comment(2)
does it chagne the window borders too? I knew that those existed, but I thought they only changed the button/text field/... layouts...Pepe
Yes, it changes everything in the window tAnnulus
G
2

You will need to first set the look and feel to use the cross platform look and feel (As someone commented before it's called metal). Then before you create the Frame you need to request that the borders are drawn by the look and feel.

try
{
    UIManager.setLookAndFeel(
        UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) { }

This will set the look and feel to the one you want. As the cross platform look and feel is metal in Sun's JRE.

// Get window decorations drawn by the look and feel.
JFrame.setDefaultLookAndFeelDecorated(true);

// Create the JFrame.
JFrame frame = new JFrame("A window");

And this will make the created JFrame have borders like you describe.

Gottwald answered 22/2, 2011 at 8:22 Comment(2)
The Look And Feel in the question is the Metal look and feel. Motif would be the name of the Look And Feel (and toolkit) of old Unix UIs.Thinnish
Yeah, I have meddeled with L&Fs in nearly 3 years, so I've forgotten the names. Thanks for pointing that out, I'll edit the answer to make it clearer.Gottwald
P
1

Add this to your main() method:

try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
Pedestrian answered 5/6, 2013 at 17:37 Comment(0)
B
0

To set windows look and feel for swing write following code in main method.

public static void main(String args[]) {
try {

         for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("windows".equalsIgnoreCase(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }

    } catch (Exception ex) {
         System.out.println(e);
    } 


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            MainFrame mainFrame = new MainFrame();
            mainFrame.setExtendedState(MAXIMIZED_BOTH);
            mainFrame.setVisible(true);

            InitDatabaseDialog initDatabaseDialog = new InitDatabaseDialog(mainFrame, true);
            initDatabaseDialog.setVisible(true); 
        }
    });
}
Brierwood answered 30/11, 2012 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.