How to make a window look like this in Java?
Solution 1
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.
Solution 2
that's called look and feel, you can find a detailed explanation here http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
Solution 3
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.
Solution 4
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);
}
friedkiwi
About me Frequent hackerspace visitor. Stubborn. Likes to take a hike. infosec webapps RE ** mobile/desktop/embedded crypto + implementations Specialities (dev) ASM x86 x86_64 ARM PPC PPC64 Ancient hardware Discontinued IBM products and clones C64 COBOL/RPG C# Javascript Specialities (sysadmin) Windows Servers/clients Ubuntu clients Unix/Windows interopability PKI/SSL complex multi-arch/multi-platform setups.
Updated on June 24, 2022Comments
-
friedkiwi about 2 years
How do I create a window which looks like this in Java:
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: