0

I'm trying to understand how Apache Pivot builds a GUI from it's definition in a BXML file. I want to make sure that I understand which steps are involved and what is done automatically during these steps. For this, I tried translating a BXML definition for a very simple GUI to pure Java. But it seems that layouting is not done the same way (or at all) when following the steps that are described in the BXML Primer.

This is the BXML file and the accompanying class to load the file:

example.bxml:

<?xml version="1.0" encoding="UTF-8"?>

<Frame xmlns="org.apache.pivot.wtk">
    <TextArea text="Hello World" />
</Frame>

WithBxml.java:

public final class WithBxml extends Application.Adapter {
    @Override
    public void startup(Display display, Map<String, String> properties) throws Exception {
        Frame frame = (Frame) new BXMLSerializer().readObject(WithBxml.class, "example.bxml");

        frame.open(display);
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(WithBxml.class, args);
    }
}

This creates the following GUI, which is expected:

enter image description here

I'm trying to use the following code to recreate the same GUI. But the TextArea is not visible, as can be seen in the following screenshot.

WithoutBxml.java:

public final class WithoutBxml extends Application.Adapter {
    @Override
    public void startup(Display display, Map<String, String> properties) throws Exception {
        TextArea textArea = new TextArea();
        textArea.setText("Hello World");

        Frame frame = new Frame();
        frame.add(textArea);
        frame.open(display);
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(WithoutBxml.class, args);
    }
}

enter image description here

What do I need to change in the class WithoutBxml to get the same result as with the BXML file?

angelatlarge
  • 4,086
  • 2
  • 19
  • 36
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90

1 Answers1

0

Instead of invoking frame.add(textArea);, I had to use frame.setContent(textArea); to add the TextArea to the Frame. This method is called when loading the BXML file because the Window class is annotated with @DefaultProperty("content"):

@DefaultProperty("content")
public class Window extends Container {
    ...
}

Because of this, BXMLSerializer is calling setContent() for child elements of the <Window> element in the BXML file.

Feuermurmel
  • 9,490
  • 10
  • 60
  • 90