Programmatically setting Look And Feel, post startup - java

I know I can change the L&F before startup using my app.conf file, but I would prefer to avoid that since Netbeans fails to have individual app.conf files for each individual RCP application, and I have a few that will NOT use thte target Look And Feel.
I can do this:
LookAndFeel hifi = new HiFiLookAndFeel();
UIManager.setLookAndFeel(hifi);
SwingUtilities.updateComponentTreeUI(this);
But that only updates the current component, not the toolbars, windows etc. Is there another way to solve my dilemma WITHOUT having to swap my app.conf files on each build?
I woul like to set the look and feel and have the entire application change, but NOT modify my app.conf file.

I have found it.
Generate a new ModuleInstall class, under the New->Other->Module Development->Installer/Activator menu.
It will register that class to be run during startup (specifically the Restored() method).
Put your LookAndFeel altering code in that method and you are good to go.
NOTE: This was done with Netbeans 7.4

Instead of using SwingUtilities.updateComponentTreeUI(this); Replace this with a reference to the top-level component you want to update (such as the JFrame of your application). Repeat as necessary for any other top-level components in your application (i.e. if you have multiple JFrames in use)

Related

Changing the look and feel of the JFileChooser

I am using a net beans form to create an applet. The applet relies on a JFileChooser. If I write the program as an application instead of an applet, the file chooser looks different. Why does the same code produce different looking file choosers when written as an applet or an application? Also, how can I change the look and feel of my file chooser from the applet to look like the file chooser from the application?
Application file chooser:
Applet file chooser:
When you invoke UIManager.setLookAndFeel(…) in your application, existing components are not automatically updated as there is no global registry of all existing component. Hence, these components will look different than the components created afterwards. You may invoke updateUI() on a component to update it to the current look and feel. There is also the utility method SwingUtilities.updateComponentTreeUI(…) which will call updateUI() on an entire component tree, recursively.
But generally, it’s better to set the desired look and feel as early as possible, preferably before creating any component, to avoid the necessity to update existing components.
You can use UIManager.setLookAndFeel()
You should be able to do something like this:
SwingUtilities.updateComponentTreeUI(JFileChooser);

How do I dynamically add and remove css for the whole JavaFX application?

I'm using JDK8 build 87 and would like to dynamically add and remove css stylesheets such that they can be used by my whole JavaFX application.
At the moment I'm setting the default styleSheet using this command:
Application.setUserAgentStylesheet(Application.STYLESHEET_MODENA);
and then when I want to add an additional css style sheet I do this:
com.sun.javafx.css.StyleManager.getInstance.addUserAgentStylesheet(styleSheet);
This works but I have two problems. Firstly, it is using a private API and secondly there doesn't seem to be a way to remove it once I have finished with it (I'm using OSGI so it is common for modules to come and go).
There was talk about moving StyleManager to public API at the start of 2012 but I'm not sure anything has happened about that.
Does anyone know of a public method to add styleSheets such that they apply to the whole JavaFX application? Also how would one remove them?
(I don't have the privileges to create the new javafx-8 tag)
According to Global Stylesheet for your GUI application:
// load default global stylesheet
Application.setUserAgentStylesheet(null);
// add custom global stylesheet
StyleManager.getInstance().addUserAgentStylesheet(AQUA_CSS_NAME);
However as Boomah points out, StyleManager.getInstance().addUserAgentStylesheet is not part of the JavaFX API, so this method is really not recommended that it be used directly from user code. Additionally, it only works for adding a global stylesheet and not for removing a such a stylesheet once the stylesheet has been added.
The more adventurous could create a patch to add Boomah's suggested feature by modifying the StyleManager code to support removal of global stylesheets and modifying Application class source code to provide a public API for the new feature which makes use of the updated StyleManager, then submit the patch to openjfx-dev for inclusion in the JavaFX platform.
In the meantime you can manually set your user stylesheet on each of your application's scenes - kind of pain, but there you are . . .

How to build JAR files dynamically

I'm making a program that needs to be able to let Clients change a setting, and using what I'm calling a "Builder", create a .jar that replaces some constants in a class with their settings.
In other words, I have a GUI that has a few textfields so that when they press the JButton labeled Build, it creates a new Runnable Jar that in a Constants class whose settings are changed with what was in the textfields.
Is this possible? I've heard about ANT Scripts, but I'm not really sure if that's what I'm looking for here.
Thanks
have you considered using a .properties files or something similar instead? You can use ant scripts for what you are describing (check out http://ant.apache.org/manual/Tasks/replaceregexp.html, you could use this task in your build.xml to dynamically change the .java files but it seems a little kludgy) but it might not be the best solution.
Check this page: http://www.mkyong.com/java/java-properties-file-examples/ which has some detail about saving to/loading from a properties file. You could set up your constants class to load it's state variables from this file, and set up the Build JButton to create that properties file.
I'm trying to think of a use case where you would want to modify the class source itself rather than use a properties file, but to be honest I can't. So I suppose you may have some special circumstance where this is not a tenable solution for you, but 99% of the time this is how I would suggest you go about it.

How to correctly make app structure to use resources?

can anyone tell me how to make directory structure of desktop app, to proper use of resources? Here is example of my app structure (using maven). It worked until I tryied to change packages structure, I only renames folders.
New structure:
src/main/java/com/example/appname/app/App.java //main class with Application, just runs gui
src/main/java/com/example/appname/gui/GuiFrame.java //JFrame
now I have resources this way, but it doesn't work:
src/main/resources/com/example/appname/app/resources/App.properties
src/main/resources/com/example/appname/gui/resources/GuiFrame.properties
after clean and build, netbeans makes me:
target/classes/com/example/appname/app/App.class
target/classes/com/example/appname/app/resources/App.properties
target/classes/com/example/appname/gui/GuiFrame.class
target/classes/com/example/appname/gui/resources/GuiFrame.properties
But when I run it, on Swing controls I don't see any text, which is inside .properties file, they are empty.
may I set something somewhere? Thank you for answers.
Maybe after your refactoring you need to change the way you load resources, i.e. from getResourceAsStream("/App.properties") to getResourceAsStream("App.properties")?

Properties Table in Java

I need a table that displays properties and allows their values to be changed. Similar to the Netbeans properties windows for the GUI editor. Does anyone know of any existing classes or libraries. I'd hate to reinvent the wheel on this one.
Edit:
Something like this which allows separators into different groups, JCombos, and JButtons to all be used.
Thanks
I would recommend JTable, and a gridbag layout manager.
The table in NetBeans is PropertySheetView (or similar) and it is part of the NetBeans Platform (PropertySheetView JavaDoc). This class should also be usable in a standalone Swing application by including the necessary NetBeans modules as jar files in the classpath (Found this with a bit of googling).
JTable can be used to display a Grid with values and allow the user to edit the value from columns you enable to it.
You could leverage the JTable class; which should provide the base you need.
JIDE Soft has a number of components, some of them are OpenSource take a look at there to see if you fine the one you need.
For future reference
I'm successfully using l2prof-common's PropertySheetPanel. The documentation is not that great and the API is not that clean, but it works, and the view is nice
The official site is
http://www.l2fprod.com/common/
But the download link is broken; I compiled the propertysheet JAR myself but you can get the whole library from http://www.astrogrid.org/maven/l2fprod/jars/
The library doubles as a demo if you execute the jar and there's also the code in it
I found this other question useful Have com.l2fprod.common.propertysheet.PropertySheetPanel To Display Composited Class
I would recommend taking a look at the SwingX JXTreeTable. I have used it in an application to create a very similar view.
An example screenshot I found on the web (here) to illustrate this

Categories

Resources