Look And Feel Matters

Download Podcast

It’s been a long while since I planned to blog on working with third-party pluggable look feels for Swing Applications. As it turned out, today seems to be the day. Anyway one of the scintillating capabilities with the latest addition of Swing components has to be there ability to adapt to any custom look and feel as opposed to AWT where they can only take the native look and feel of the Operating System and few other PLAF’s (Pluggable Look And Feels) that come bundled with JDK makes look all the applications in the same old boring fashion (Which is not to my liking). Because they say “Look And Feel matters“. Here I’ll be using a theme PLAF called Synthetica which is currently at its v2.7.2. You can download a free evaluation copy upon registration on

http://www.javasoft.de/jsf/public/products/synthetica;jsessionid=46B0017CCD5DD3D

27B2E036DFC9587D6

Since Synthetica provides series of themes first I would suggest you to download the core of all which is the Synthetica Standard Look And Feel. Then you can proceed to download any other additional theme(s) as you wish. Here of course I have used the “Synthetica White Vision Look And Feel” so in that case you might also require to download this particular theme as well to have a feel of my Tiny App.

Pre-Requisites

  • SyntheticaStandard Look And Feel
  • SyntheticaWhite Vision Look And Feel
  • Java 1.5 or later (Since it’s a requirement of Synthetica 2.x.x)
  • Eclipse – IDE (Since I’m used to eclipse and probably the favourite of mine at that, I’ll be going ahead with it)

Getting to the business end. Open up your eclipse IDE and select File -> New -> Project -> Java Project. Then name it with whatever name you like and that’s it you have just created an empty project.

Now you can create a new class and name it App (in my case) and then you can right click on your project root to create a new package and name it lib and within that another package called synthetica (Here I’m just following the conventions. Best Practices..!) And just copy and paste your jar files (Synthetica) that you have downloaded.

Then right click on the root of your project in the workspace and select Build Path -> Configure Build Path -> go to the Libraries tab and -> Click on Add External Jar files and just point to both your jar files that now reside in the synthetica package inside the lib package.

That’s it your Environment is now perfectly setup..!

If you have the environment setup properly when you type de. In your App.java, a popup menu should appear showing all the available packages and classes within it (Just for a Verification..!). If you get it well you are all most there.

Below I give you the code which consists a Menu, MenuItem Button and a Table. So it’s just requires copy n paste job here onwards. Now you can Run the App to say your oohs and Ahhs..!

Screenshot of App

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//Importing the all importatnt Look And Feel classes.
//import de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel;
import de.javasoft.plaf.synthetica.SyntheticaWhiteVisionLookAndFeel;

public class App extends JFrame implements ActionListener{

public JMenuBar menuBar;
public JMenu menu;
public JMenu helpMenu;
public JMenuItem item1;
public JMenuItem aboutItem;

//Buttoon Instance
public JButton myButton1;

Object rowData[][] = { { “Dat 1”, “Dat 2”, “Dat 3”},
{ “Dat 4”, “Dat 5”, “Dat 6”} };
Object columnNames[] = { “Column 1”, “Column 2”, “Column 3”};

public App(){

/**
* Note: If you are to call super explicitely make sure
* That it is the first statement
*/
super(“Look And Feel Observation”);
this.setSize(400,300);

menuBar = new JMenuBar();
menu = new JMenu(“File”);
item1 = new JMenuItem(“Exit”);
menu.add(item1);

helpMenu = new JMenu(“Help”);
aboutItem = new JMenuItem(“About”);
helpMenu.add(aboutItem);

menuBar.add(menu);
menuBar.add(helpMenu);


//Sets the MenuBar
this.setJMenuBar(menuBar);

//No need for a Layout Manger
this.getContentPane().setLayout(null);

myButton1 = new JButton(“Click Here”);

//Both item1 and myButton1 implments ActionListener
item1.addActionListener(this);
myButton1.addActionListener(this);

//uses set bounds to place the components on the ContentPane
myButton1.setBounds(20, 20, 100, 100);
this.getContentPane().add(myButton1);
this.getContentPane().setBackground(Color.orange);

try {
UIManager.setLookAndFeel(new SyntheticaWhiteVisionLookAndFeel());
SwingUtilities.updateComponentTreeUI(this);

}
catch (Exception e) {
e.printStackTrace();
}

drawTable();
this.setDefaultCloseOperation(1);
setVisible(true);

}

/**
* Populating the table
* in a pretty static fashion
*/
public void drawTable(){
JTable table = new JTable(rowData, columnNames);
table.setBounds(140, 20, 200, 100);
getContentPane().add(table);
}

/**
* Implmenting the actionPerformed method
* in ActionListener Interface
*/
public void actionPerformed(ActionEvent evt){

if(evt.getActionCommand() == “Click Here”){
JOptionPane.showMessageDialog(this,”Help Protect Green.”, “About”, 1);
System.out.println(“You have just clicked on the button”);
}
if(evt.getActionCommand() == “Exit”){
System.exit(0);
}

if(evt.getActionCommand() == “About”){
System.out.println(“About”); }

}

public static void main(String [] argc){

//Creates an instance of App.
new App();

}

}

Two statement tht I would like note in this class will be the following..

UIManager.setLookAndFeel(new SyntheticaWhiteVisionLookAndFeel());

SwingUtilities.updateComponentTreeUI(this);

Advertisements