Setting Java Swing application name on Mac - java

I'm writing a Java Swing application for the Mac using Java 1.6. I've read a number of tutorials that step you through how to better integrate your Java application with OS X, but there's one thing I haven't been able to get working. I can't get the application name (the first, bolded menu item in the Mac menu bar) to display. By default, the fully-qualified class name of the main class is shown and I can't get it to change.
This site says that you have to set the following property:
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "AppName");
But that doesn't work (I'm running 10.6, so maybe the property name changed?).
When I create a new Java project in XCode (I normally use Eclipse), the name somehow magically gets set! (it starts you out with a runnable, boiler-plate application) I've looked all around the XCode project for how this is done, but I can't figure it out!
My guess is that it only sets the application name if you wrap your Java application up in a Mac *.app package, but was wondering if anyone knew the answer. Thanks.
EDIT: Interestingly, it sets the application name if I package my application in a runnable JAR file, but not if I run it from Eclipse.

You should do the following during app initialization, before GUI is built:
// take the menu bar off the jframe
System.setProperty("apple.laf.useScreenMenuBar", "true");
// set the name of the application menu item
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "AppName");
// set the look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UPDATE.
Above code works in Java 1.5, this code may not work in 1.6
For new java see documentation:
Either use -Xdock:name command-line property: -Xdock:name=YourAppName
Or set CFBundleName in information property list file (plist)

On Mac 10.7.5, programatically setting the property will work with with Java 1.6 but not with Java 1.7.

Related

Override install4j UnixLauncher WM_CLASS property

Is it possible to override the WM_CLASS set by install4j on Linux environments?
At the moment, executing
xprop WM_CLASS
on my application deployed with install4j always returns com-install4j-runtime-launcher-UnixLauncher.
This is a problem when creating proper .desktop files, I do not want to group together all applications deployed with install4j.
I gather this question deals with a similar problem. However, the answer does not work for me. The mentioned sys.ext.windowClass compiler variable is not documented anywhere I can find and as far as I can see it does nothing.
On Linux, window managers group windows and assign the correct icons by using the WM_CLASS property.
This property can be inspected using xprop:
xprop WM_CLASS
To specify which WM_CLASS a .desktop file should affect, you can use the StartupWMClass property in a desktop file:
StartupWMClass=<my-applications-wm-class>
The problem with install4j is that all deployed applications will use
com-install4j-runtime-launcher-UnixLauncher as WM_CLASS.
As Ingo Kegel mentions in his now edited answer to this question, this can be circumvented by setting the WM_CLASS window property explicitly in your Java application using the following code:
String wmClass = ...;
Toolkit toolkit = Toolkit.getDefaultToolkit();
Class<?> toolkitClass = toolkit.getClass();
if (Objects.equals("sun.awt.X11.XToolkit", toolkitClass.getName())) {
Field awtAppClassName = toolkitClass.getDeclaredField("awtAppClassName");
awtAppClassName.setAccessible(true);
awtAppClassName.set(null, wmClass);
}
If you have preliminary dialogs when your application starts (e.g. for selecting a workspace), it is important that you do this for all dialogs separately.

How to attach a javaagent to all java applets

I wrote a sample java agent that creates a "hello world" file in a specific place. I want it to run whenever any java applet is run (definitely including ones I didn't write myself.) How do I do that?
My agent works when I run it manually from the command line (as in, running another java file with the agent attached to it.) On the automatic front, I tried going into the java configuration panel -> Java -> View -> JRE Configuration Settings -> setting the optional arguments to attach my agent, but that doesn't seem to do the trick. I browse with FF to someplace that makes me run a java applet but no file is created.
I'm guessing I'm missing something basic here, because when I try to google this people all around various forums seem to answer it as if it's the most trivial thing: "just use -javaagent, read more about it in this (link)."
If it helps at all, this is the optional parameter I add in the configuration panel (which works when I use it manually):
-javaagent:C:\Users\admin\workspace\poc\bin\poc\myagent.jar
Update: I found out I can set an environment variable (JAVA_TOOL_OPTIONS) to -javaagent:(agentpath). Once again it works flawlessly with local java applications, but now when I browse over to a webpage with an applet, firefox auto-closes itself. IE declares the webpage broken. Chrome doesn't even display the applet. The agent itself at this point does nothing - it just has an empty premain method. Anyone?
Setting the environment variable JAVA_TOOLS_OPTIONS=-javagent:(path) is right.
My error was that I misconstructed the .jar file. Once I fixed that (simply used the jar.exe tool to make my .jar) it worked properly - locally and on all applets via firefox. Until then, that was what caused firefox to either hang or crash when encountering an applet.

Adding MenuBar Space on Mac

On a PC, this issue would not arise, but I have a mac and the java menu bars typically appear on the top left hand area of the screen. For example (from the apple developers website):
The blue highlighted menu is the same name as the main class in a java program. I was wondering how I could name my main method class with a space in between two words rather than an underscore.
I tried...
Using the -Xdock:name="My App Name" on the command line
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Not My Mac App"); within the main method before before and after setting the look and feel
Only -Xdock:name worked.
Tested using Java 8 on 10.9.4, so the settings might be different for earlier version of Java and OS combinations
You may also consider creating a proper Mac .app bundle, which should allow you to set these properties via the plist file...
Take a look at Packaging a Java App for Distribution on a Mac. It is "slightly" out of date, but should lead you in the right direction

How to change the LookAndFeel of the NetBeans Platform Framework Application

I cannot figure out how to change the LookAndFeel of the application I built on the NetBeans platform framework, can anybody please help? I want to change its look using the TinyLAF java api http://www.muntjak.de/hans/java/tinylaf/index.html. I know how to change the LookAndFeel when developing a regular Swing application in the NetBeans IDE, but not when developing it on the NetBeans Platform framework.
This is the code, for TinyLAF, that I use for regular Swing applications:
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try {
UIManager.setLookAndFeel("de.muntjak.tinylookandfeel.TinyLookAndFeel");
} catch(Exception ex) {
ex.printStackTrace();
}
TinyLaF looks for a default theme file named 'Default.theme' (case-sensitive). If it finds one, this file will be loaded at startup.
(The 'Default.theme' file is an ordinary TinyLaF .theme file, just with a special name, you can take any .theme file and rename it to 'Default.theme').
TinyLaF will search the following URLs:
TinyLookAndFeel.class.getResource("/Default.theme");
finds 'Default.theme' if it is inside tinylaf.jar
Thread.currentThread().getContextClassLoader().getResource("Default.theme");
finds 'Default.theme' if it is inside your application's JAR
new File(System.getProperty("user.home"), "Default.theme").toURI().toURL();
finds 'Default.theme' if it is inside the home directory
new File(System.getProperty("user.dir"), "Default.theme").toURI().toURL();
finds 'Default.theme' if it is inside the working directory
Please note that my question is not how to change the LookAndFeel of the NetBeans IDE, but how to do it for the Java Application built on top of NetBeans Platform framework.
I found the following searching the net: you need to do it in the module "Installer".
check this link for where you need to add: http://joshiegeek.blogspot.co.il/2012/01/netbeans-platform-custom-laf.html
this one has actual code sample (please ignore the title :)) : http://forums.netbeans.org/topic39450.html
and finally this one talks of a specific plaf but has few comments along the way : https://blogs.oracle.com/geertjan/entry/blue_look_and_feel_for
1. You can change the look and feel of Swing very easily as its based on MVC architecture.
2. Swing is also known as PLAF (Pluggable Look And Feel), so keep the same Model part and change the View, example like same model for desktop and web application.
3. Use
UIManager.setLookAndFeel(Your_Choice_of_Look_and_Feel); // To set the Look and Feel
SwingUtilities.updateComponentTreeUI(frame); // To refresh the JFrame and Components
See this for more details:
http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
Had to find that first link mentioned in an earlier post on the waybackmachine ...
Answer is to add a runtime argument or put it in the project.properties file. The line to add (for e.g. Metal) is
run.args.extra=--laf javax.swing.plaf.metal.MetalLookAndFeel

<ProjectName.ProjectUI sucks as a name for my Netbeans java OS X app

What property in Netbeans to I need to change to set the name of my java swing app in the OS X menubar and dock? I found info.plist, but changing #PROJECTNAMEASIDENTIFIEER# in multiple keys here had no effect.
Thanks,
hating netbeans.
The answer depends on how you run your application. If you run it from the command line, use '-Xdock:name=appname' in the JVM arguments. See the section "More tinkering with the menu bar" in the article linked to by Dan Dyer.
If you are making a bundled, double-clickable application, however, you just need to set the standard CFBundle-related keys in your application's Info.plist (see the documentation on Info.plist keys for more details).
Check:
nbproject/project.properties
nbproject/project.xml
in project.xml look for the name element...
But... Why not just select the main project and right click and do rename?
This is not NetBeans-specific, but this article has some useful tips about tweaking your Swing apps so that they fit in on OS X.

Categories