How to Mix XML into Your Java Programming - dummies

By Barry Burd

Sometimes, when you’re writing code in Java, you may want to mix in some XML. Despite Java’s object-oriented flavor, the code displays a window using a “do this, then do that” approach.

Here's how you show a picture:
Construct a frame
Construct an icon containing a certain image
Construct a label containing the icon
Add the icon to the frame
...
Pack the frame
Make the frame be visible

This “do this, then do that” approach is called procedural programming.

Now imagine you’re at the Louvre looking at the Mona Lisa. You don’t think “Da Vinci added a face, then he put a smile on the face, then he added a body, and then a background.” The painting doesn’t progress from one action to another. Instead, the painting simply is.

In the same way, a window in a GUI application doesn’t need a procedural progression. Instead, you can describe a window declaratively. You write code that says “Here’s how the window looks.” The Java Virtual Machine uses your description to decide (on its own) what to display and when.

image0.jpg

The following Swing code creates a grid. Don’t look at all the details in the code. Instead, notice all the verbs: “set the layout to a new GridLayout, add a label to the frame, set the font, pack the frame, and so on.” It’s all procedural.

import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
 public static void main(String[] args) {
 JFrame frame = new JFrame();
 frame.setLayout(new GridLayout(4, 2));
 JLabel labels[] =
  { new JLabel("Name"), new JLabel("Phone"),
  new JLabel("Alice"), new JLabel("555-1234"),
  new JLabel("Bob"), new JLabel("555-4321"),
  new JLabel("Carol"), new JLabel("555-3000") };
 frame.add(labels[0]);
 frame.add(labels[1]);
 JLabel boldLabel = new JLabel("Name");
 Font boldFont = boldLabel.getFont();
 Font plainFont = new Font(boldFont.getName(),
  Font.PLAIN, boldFont.getSize());
 for (int i = 2; i < 8; i++) {
  labels[i].setFont(plainFont);
  frame.add(labels[i]);
 }
 frame.pack();
 frame.setVisible(true);
 }
}

To save the world from its procedural fixation, JavaFX offers a declarative option. Using JavaFX, you can describe a scene as an outline using XML (eXtensible Markup Language) tags. Here’s a JavaFX version of the grid:

<GridPane gridLinesVisible="true"
 layoutX="100.0" layoutY="165.0">
 <children>
 <Label text="Name"
  GridPane.columnIndex="0" GridPane.rowIndex="0">
  <font>
  <Font name="System Bold" size="12.0" fx:id="x1" />
  </font>
 </Label>
 <Label font="$x1" text="Phone"
  GridPane.columnIndex="1" GridPane.rowIndex="0" />
 <Label text="Alice"
  GridPane.columnIndex="0" GridPane.rowIndex="1" />
 <Label text="555-1234"
  GridPane.columnIndex="1" GridPane.rowIndex="1" />
 <Label text="Bob"
  GridPane.columnIndex="0" GridPane.rowIndex="2" />
 <Label text="555-4321"
  GridPane.columnIndex="1" GridPane.rowIndex="2" />
 <Label text="Carol"
  GridPane.columnIndex="0" GridPane.rowIndex="3" />
 <Label text="555-3000"
  GridPane.columnIndex="1" GridPane.rowIndex="3" />
 </children>
</GridPane>

If you’re familiar with HTML (the language of web pages) you might recognize some of the tricks in the XML grid code. If not, don’t worry. Using a tool named Scene Builder, your computer writes the XML code on your behalf.