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);
Related
This might sound like a ridiculous question, but I have to ask it because I have a working product which is doing this.
I have an applet running inside a browser. This applet is just not just any applet, but a fairly complex package application for CRM/ERP. I was told by a vendor company that they are able to monitor what a user does inside the applet, by replacing applet's main class at runtime before launch with their own. The term used was "endorsing".
I am a bit clueless now. How can you look inside an applet and listen on user clicks and keyboard events, even if you could somehow hack into it? I can tell you that this is a true story, because I have seen this vendor company's applicaiton and it just sits in the background and records all the contextual information (for instance, user filled which textbox in the applet, the name of the textbox and etc).
Are they any hacks at classloading level (I feel stupid asking this), or something else that I have not come across in java that would let you do something 'urban legendary' like this?
Java Applets are loaded using a HTML tags like this:
<applet archive="ApplicationSP1.jar,Application.jar" code="Main.class" name="myApp" width="800" height="600"></applet>
As you can see, the "archive" attribute supports several .jar files.
You could use this technique to load your own versions of the Java Classes of the application by putting them in the ApplicationSP1.jar file. They will be loaded before those classes stored in the second Application.jar.
Obviously, you would need to do some reverse engineering to understand which classes from the original application to override or wrap. Then you have to create new ones named exactly (same package and class name) as those you want to override.
Other option would be developing Aspects to capture events in the application and load these aspects using same technique of multiple .jar in the archive attribute of the HTML applet tag.
The solution for capturing Swing/AWT event can be found in
Want javax.swing hook that tells me WHICH component in the hierarchy is executing an action
It is difficult for overwriting Swing/AWT class used by applet which launching from browser.
They have to breaking the protection of Java security manager and get writing permission of JRE endorsed library folder.
For this case, Java Endorsed Standards Override Mechanism is hard to implement without manually operation of end user.
I have a .jar of a game that I would like to embed inside a JFrame of my own program. So when I run my program, it will also launch that .jar and it will be wrapped in my JFrame. I'm not sure if this is even possible, haven't found a way yet, so any guidance is great :)
I can't offer much more context because even after an hour or so of googling I'm still completely lost on where to start, if you need any other information feel free to ask and I'll get it asap.
A .jar file is not a swing widget; it's Java archive and can contain class files. What you want to do is add the jar to the classpath, and then load a JWidget class from there.
The only way I know how to do this by hacking the jar. Create a new project, add it to your class path. Look at the manifest and what class the main method is in.
Then create your own main method which calls MainClass.main(args);
Then do a sneaky Frames.getFrames() as described here Get Any/All Active JFrames in Java Application?. Once you have a handle the jframe you can 'steal' the components and add it to your own and presto!
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)
I am trying to create a program were the user drags a file into an area (currently a JTextArea, but can be another container) and it adds the absolute path of the file to an ArrayList. I am having trouble figuring out how to implement drag and drop of files.
so far I have tried reading some similar questions but they aren't really helping me.
(also this is targeting windows but linux/mac support is an option as well)
Have a look at oracles page about DnD. Basically you can drag everything into your program, should it be a file directly from a native browser or the JFileChooser. What you are dragging is only the path to the file. So you only have to set your JTextAreato accept drops and define how it has to "interpret" the object that was being dropped.
Here is a full blown example.
JFileChooser looks nothing like the native widget. I seem to remember reading some hack to get it look like the native widget but searching for it know i can't seem to find it again i came across posts that suggest using java.awt.FileChooser but that class does not seem to be in the distribution. How can i make JFileChooser make look like the native widget?
Take a look at this page. It goes through a lot of UI tweaks, but the third one is most relevant: "Using the JFileChooser to get Mac file and folder icons."
java.awt.FileDialog will be the classes the posts meant. It is, unsurprisingly, more limited that the Swing version. All Java SE implementations are required to have the FileDialog class (although headless configurations may not be able to do anything useful with it).
It may be worth a look at the Quaqua look and feel, it replaces the file chooser with a much better and more native version, including folder colouring etc.