Maximize a JFrame

Hello everyone,

I’m writing a new post because I saw an awfull code to maximize a JFrame in Java. Maybe You’ve ever tried to maximize Your GUI at the startup of Your application. It’s something which is very basic but which can be done in several ways (and some awfull ways too…).

First of all, You could retrieve the size of Your screen, and set it to Your JFrame like this :

// Get the screen size
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
// Create a jframe, just for the example
JFrame myFrame = new JFrame();
// Set the size
myFrame.setPreferredSize(screen);
myFrame.setVisible(true);

Well this is bad… Why? Because imagine You created Your application on Linux, and then try to launch it on Windows (I test an application recently…). On Linux, fine. On Windows, the application was under the task bar… So a part of the application was hidden… And the developpers used a similar code. So, this is not really a good practice.
There is a much simplier and efficient way to do that. Just look at this :

JFrame myFrame = new JFrame();
myFrame.setVisible(true);
myFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

That’s all ! Much more simplier and better working. Notice that setting the state is after making the frame visible :)

JAI : how to solve vendorName == null exception

Hello everybody,

Have You ever used JAI (Java Advanced Imaging) ? If You used the class ImageIO in order to read and save images, the answer is yes.
Most of the time, You should not have problem when You deploy Your application with an executable JAR even if You’re using JAI. But, most of the time doesn’t mean never, so that’s a problem !
Recently I had to deploy an application as a JAR, and this application was using quantity of external library. And of course it used JAI.
Well, doing a JAR is not really a problem but …

In my case, the application was developped with Eclipse, and was running fine in it. But, when it comes to run it with a JAR, that was problematic…
I got an exception when a tried to load images, especially TIFF images. This exception was thrown by JAI and told me that the vendorName was null. Interessting : but which vendor ??

The answer is quiet simple : JAI needs sometimes to know the name of the vendor who developped the application, and the implementation of it… OK, you’ll tell me “Fine. It seems it’s my application, so I’m the vendor. But how do I specify that?”. And that’s the point.

When You deploy an application as an executable JAR, You probably know that You need a manifest file. And that manifest file is the key. A complete valid manifest file could be this :

Manifest-Version: 1.0
Main-Class: fr.free.thierrywasyl.myproject.launcher.Launch

But where did I specify the author? No where. But JAI will maybe need to know that, or You’ll get an exception about the vendor. So let’s add some lines, which are good to add (take it as a good practice) which are general:

Manifest-Version: 1.0
Implementation-Vendor: Sun Microsystems, Inc
Implementation-Title: Java Runtime Environment
Implementation-Version: 1.6.0
Main-Class: fr.free.thierrywasyl.myproject.launcher.Launch

So far so good. So are You a null vendor? Of course not, You get over the exception ;)
Enjoy.

Follow

Get every new post delivered to your Inbox.