Beginning Java for Xojo Programmers X

Alright we’e look at a lot of stuff but haven’t really dived into making a UI.

So lets take a stab at that.

Make a new directory - I named mine X - UI Hello World !
I opened VS code and opened that directory - and created a new Java file

Recall that in Java we have to declare which items we want to import so we can use them.
In this example we want to use java.swing - one of the UI toolkits that java has available.

We dont need to import the whole thing for this example - JUST JOptionPane.

import javax.swing.JOptionPane;

The JOptionPane is a component which provides standard methods to pop up a standard dialog box for a value or informs the user of something. Much like a Xojo MsgBox.

We need to declare the class itself, main entry point for our class - HelloWorldUI
And it needs a main entry point - recall we discussed the requirements of this in the last instalment. It must be private static void main(String[] args)

And to show a Hello World message its one line

        JOptionPane.showMessageDialog(null, "Hello World!");

OK so whats the first parameter ?
Well the signature for this method is

public static void showMessageDialog(Component parentComponent, Object message)                               throws HeadlessException

the “parentComponent” is akin to what Window the dialog should show within
By passing null we tell java it doesnt matter.

So our entire “GUI” app looks like

import javax.swing.JOptionPane;

class HelloWorldUI {

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Hello World!");
    }

}

However, compiling and running this directly from VS Code isnt working (I’ll have to sort out why) but it works fine from the command line

Thats it.

Now its not sophisticated

We’'ll do more in a bit :slight_smile:

References :
Java examples explained - https://nfmy.wordpress.com/

https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

2 Likes