JavaFX Tricks: LookAndFeel

Yes I know, it’s been awhile since I’ve blogged. Over the last few months I’ve been working on some swing apps, installing ubuntu on a server at my house, and testing open source network monitoring software.

During my time away, I’ve also been working with JavaFX…I’ll blog more on this later. For now I want to leave you with a FX tip that I couldn’t find on-line. To change the look and feel of your FX script from the system default to one of the other Java look and feels do the following:

import javafx.ui.*;

Frame {
      lookAndFeel: "javax.swing.plaf.metal.MetalLookAndFeel"
      title: "Metal Look and Feel"
      height: 400
      width: 600
      content: Label {
                text: "Metal Look And Feel"
              }
      visible: true
};

As you can see from the example, the lookAndFeel attribute string needs to be set to the java look and feel class that you want to use.

Advertisements

Exporting JTable data to Excel Revised

Someone alerted me to a problem with my JTable exporting example. It seems with all the changes I’ve been making to my blog, I lost some of the code at the end of the example. It’s been restored and can be found here: Exporting JTable data to Excel.

I discuss JTables further in my post: Making a JTable Non Editable.

Making a JTable Non Editable

There are times when you want to display a JTable in your Swing app, but do not want to the user to be able to edit the cells. Here’s a simple way to accomplish this task via the DefaultTableModel:

  DefaultTableModel model = new DefaultTableModel(6,7){

 public boolean isCellEditable(int row, int column)
 {
     return false;
 }
  };

In the example above, I’m creating a DefaultTableModel model that has 6 rows and 7 columns. I’m overriding the isCellEditable() method, the method used by other methods and classes to know whether or not a JTable cell can be edited, and returning a boolean value of false in all cases. I could have also returned false for only selected rows and columns by adding conditional logic .

Using the Preferences API

Like many Java developers, I tend to find something that works for me and use it for years without researching better approaches. For example, I’ve used the Properties class for years to load and save configuration information in my Java programs. The Properties class works fine, but does have some drawbacks.

Last week I stumbled across another API, the Preferences class, that’s been available since 1.4. It can be used the same way as the Properties class, to load and save configuration stuff. The big difference between it and Properties is that the Preference class allows you to store data in a consistent format, it allows you to take advantage of centralized repositories such as the Windows registry, and provides more flexibility with regards to storing the preferences in user folders.

Without going into a lot of detail, I’ve created a simple swing application that demonstrates the basics of the Preferences class. All the program TestPrefs does is save an loads the most recent window size and location.

Looking at the code below, I’ve highlighted the Preferences api calls for you. As you can see, the first thing to do is to decide where you want the preferences file to be located. In this case, I’ve chosen user root, which means that I’ve decided to store the preferences file under the user tree rather than the system tree. Next, I’ve told the program to store the file in the root folder of TestPrefs class. You can also specify a folder with something like node = root.node(“/com/prefs/…”).

Now that the class knows where to locate the preferences file, how do we write information to it? Looking at the windowClosing() method, you’ll see node.putInt(..). The putInt() method has two parameters: name and value. For example, if you wanted to save a frame’s width, you might create a name value/pair like this: node.putInt(“width”, jFrame.getWidth()).

Next, we want to read information from the preferences file and use it to position our window on the next execution of the program. looking at the getJFrame() method, you can see the jFrame.setSize() method. This time I use node.getInt(). Like putInt(), getInt() has a name/value pair. This time it works a little differently. In this case, the value parameter acts as a default value.The name parameter tries to find the associated value within the preferences file. If it can’t, then the value parameter is used.

There are other methods associated with the Preferences api, and I encourage you to explore these. Hopefully, I’ve whetted your appetite enough to get started.

___________________

package test;

import java.awt.BorderLayout;
import javax.swing.SwingUtilities;
import java.util.prefs.Preferences;

import javax.swing.JPanel;
import javax.swing.JFrame;

public class TestPrefs {

private JFrame jFrame = null;

private JPanel jContentPane = null;

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public static final int DEFAULT_LEFT = 0;
public static final int DEFAULT_TOP = 0;

Preferences root = Preferences.userRoot();
 final Preferences node = Preferences.userNodeForPackage(this.getClass());

/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(node.getInt("width", DEFAULT_WIDTH),node.getInt("height", DEFAULT_HEIGHT));
jFrame.setLocation(node.getInt("left",DEFAULT_LEFT), node.getInt("top",DEFAULT_TOP));
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("Test Preferences API");
jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
node.putInt("left", jFrame.getX());
     node.putInt("top", jFrame.getY());
     node.putInt("width",jFrame.getWidth());
     node.putInt("height", jFrame.getHeight());
}
});
}
return jFrame;
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
}
return jContentPane;
}



/**
* Launches this application
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestPrefs application = new TestPrefs();
application.getJFrame().setVisible(true);
}
});
}

}

Java Windowing Toolkit Poll

%d bloggers like this: