I am building an application in java swing and I am using the following code to give the UI a native OS look
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
On a OS X, the look is fine, but on windows (XP and 7) the buttons look like this.
alt text http://img710.imageshack.us/img710/8735/buttonsoc.png
I have used this exact same code on other projects and it works fine. But in this particular project I get a completely different look.
I am using Java 1.6
Thanks in advance!
Are you possibly creating your GUI elements before actually setting the L&F? If you already created (e.g.) JButton instances and called methods on them, they allocate their UI peer - changes to the L&F after that won't affect the already created instances.
This would explain why it works on Mac (the L&F defaults to Mac on Apple's JVM IIRC), but not on Windows. You can test this quickly if you move setting the L&F directly into your main method as the very first call (this assuming your main class does NOT contain any statically initialized GUI instances of course).
Related
I have a java SE application which sits in system tray and shows a popup message thru JavaFX. Now I need to host this app in a cloud, so I converted it (project facets) to Dynamic Web Project and deployed it to Tomcat. Everything works fine - now entry point is not main(), but ServletContextListener. But at some point control goes to starting JavaFX and showing popup. So I would like to be able to code something like this:
if(onDesktop){
startJavaFX();
showMyPopupWnd();
}
If it helps, so far app is supposed to run only on Tomcat 7 and higher.
Besides, what is best way to maintain two versions of same app (desktop and App-Server web project)? Now I am working in two IDEs - Eclipse SE and Eclipse extends (two projects respectively - old SE and new extends with ServletContextListener entry point instead of main), but I understand that it is a bad idea to code two actually separate logic workflows...
You can check whether you are running in a headless environment using GraphicsEnvironment.isHeadless()
Tests whether or not a display, keyboard, and mouse can be supported in this environment. If this method returns true, a HeadlessException is thrown from areas of the Toolkit and GraphicsEnvironment that are dependent on a display, keyboard, or mouse.
I think the best way would be setting an environment variable for a different environment with respective value and access it from java application. Like for env variable, we can name like envType and you can put a value like local and server. You can access that variable from the java application using System#getenv.
String envType = System.getenv("envType")
if(envType.equals("local")){
startJavaFX();
showMyPopupWnd();
}
Make a global boolean named e.g. onDesktop you only set to true in the main-method.
Regarding how to do it, you frequently split core logic into a separate module and then have a module for each target environment. This is typically the point where you need to start to learn Maven.
Although I think that embedding JavaFX code into a server WAR is a bad design, here is how to determine if your app is running on a server or not by checking the availability of javax.servlet.ServletException in the classpath.
public static boolean isDesktop()
{
boolean isDesktop;
try
{
Class.forName("javax.servlet.ServletException");
isDesktop = false;
log.info("This program runs on a server");
}
catch (ClassNotFoundException ex);
{
isDesktop = true;
log.info("This program runs on a desktop");
}
return isDesktop;
}
I am writing an application with GUI using Java Swing drag-and-drop in NetBeans IDE. But when I add a component in a small space, it looks correct in the design preview but when I run it, the size is different.
As you can see in the picture, windows 1 and 2 are the same, but when I run the code, the gap on the right side is different (3). Why is that?
I am writing the code in java 8 (1.8.0_05-b13).
This is probably a consequence of the look-and-feel used in the preview as opposed to the actual execution. Try executing the following at the start of your program to set the LaF to the system one (windows-looking one on Windows):
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
You'll want to catch and handle UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, and IllegalAccessException.
Im building a GUI in Netbeans, it looks good in the designer but when I run the program it looks pretty rubbish. Im using a Mac and so I feel that it would look better using the Mac OS LAF but then when I run it on Windows, what will happen? Is a Windows system able to use the Mac LAF and vice-versa?
I dont usually mind the Ocean/Metal LAF but it looks better on Windows than it does on Mac, is there a way to set a theme as a fallback? Or to change depending on the system its running on?
The UIManager.setLookAndFeel(String className) Loads the LookAndFeel specified by the given class name:className, using the current thread's context class loader.
To set look and feel of your current system, make use of UIManager.getSystemLookAndFeelClassName(): Returns the name of the LookAndFeel class that implements the native system look and feel if there is one, otherwise the name of the default cross platform LookAndFeel class.
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
put this code before you create instance of your application window: JFrame or any such Top-level Container.
In my Java application I have a window which holds a JTextArea within a JScrollPane with scrollbars policies set to AS_NEEDED.
As I run my application I see that JTextArea this way:
Why am I seeing the scrollbars with that cutaway knob (which doesn't reflect a "standard" representation like this)?
The Layout for the frame is GridBagLayout, and I'm on Mac OS X 10.8.2, should that matter.
This is based on the Look and Feel your app is using, and the limitations of Java's integration with the native OS layout components. The one in your screenshot looks like Nimbus.
Swing applications always custom-render the look and feel, and don't do a very good job of using the native OS widgets everywhere. The result is that you get weird looks that might be consistent the OS only some of the time, or only with certain layout components.
Welcome to developing cross-platform desktop apps in Java. :(
To attempt to get the system look and feel when your application starts you can do this:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassname());
} catch (Exception e) {
// Handle exception
}
This will set the look and feel to that of the system regardless of what you run it on.
And as mentioned, the default look and feel for your application appears to be Nimbus and not OSX's Aqua, which again can be fixed with he above snippet and you could (should you care to) offer a UI option to the user to change the look and feel of the application to whatever they chose.
You are with Nimbus LookAndFeel
http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html
I'm trying to change LAF of my program in this way:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception ex) {
Logger.getLogger(MainWin.class.getName()).log(Level.SEVERE, null, ex);
}
But this doesn't affect and program still looks as METAL while this reports "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" that means it must be changed:
UIManager.getLookAndFeel().getClass().getName();
Changing to other LAFs has the same problem!
What's the problem?
Thanks
Do you set look and feel before you create your GUI? Because if your GUI is already created (even if not shown), you have to tell Swing that LAF was updated:
SwingUtilities.updateComponentTreeUI(frame);
GTKLookAndFeel only gets applied if the operating system is Linux.
Using GTKLookAndFeel for a application running on Windows does not change the Look and Feel.
It will still display the Metal Look and Feel.
Note: The GTK+ L&F will only run on UNIX or Linux systems with GTK+
2.2 or later installed, while the Windows L&F runs only on Windows
systems. Like the Java (Metal) L&F, the Motif L&F will run on any
platform.
More info on Modifying the Look and Feel