Skip to content

Dialogues

William Wood edited this page Mar 10, 2019 · 8 revisions

Dialogues

One of the most basic means of (or tool to use when) creating a GUI is that of dialogue boxes. These are pop-up windows that offer a "one-shot" means of interacting with the user. The GUI class within JISA provides several methods for easy dialogues.

Contents

  1. Alerts
  2. Confirmation
  3. Choices
  4. Inputs
  5. File Save and Open
  6. VISA Instrument Browser

Alerts

The most basic dialogues are those that simply deliver a message to the user for them to acknowledge (by pressing "OK"). These are called "Alerts" and come in three varieties:

  • Information
  • Warning
  • Error

Each time you want to display one of these you need to provide 3 pieces of information:

  1. Title Text - The text to show in the title-bar of the window
  2. Header Text - The text to show at the top of the window content
  3. Message Text - The message to show in the window

To make a dialogue appear, use one of the following:

// Information
GUI.infoAlert("Title Text", "Header Text", "Message Text");

// Warning
GUI.warningAlert("Title Text", "Header Text", "Message Text");

// Error
GUI.errorAlert("Title Text", "Header Text", "Message Text");

Information

Warning

Error

These methods will not return until "OK" has been pressed, meaning that your code will halt at the line where you call one of these dialogues until the user has acknowledged the message.

If needed, you can change the width of the dialogue by adding your custom width as a fourth argument:

// Info dialogue that is 500 pixels wide
GUI.infoAlert("Title", "Header", "Message", 500);

(this applies for all three types).

Confirmation

The next type of dialogue is the one that gives the user a yes/no or ok/cancel choice. This is called a confirmation window and is called much like the alerts, except that this time it will return a boolean indicating the user's choice:

boolean choice = GUI.confirmWindow("Title", "Header", "Message");

which will result in:

Again, this will not return until the user has pressed either "OK" or "Cancel". Pressing "OK" will return true and pressing "Cancel" will return false. For example, you could use this to confirm exiting the program:

boolean result = GUI.confirmWindow(
    "Confirm Quit", 
    "Quit?", 
    "Do you really want to quit?"
);

if (result) {
    System.out.println("Goodbye!");
    System.exit(0);
} else {
    System.out.println("Quit aborted.");
}

Choices

You can offer the user a choice between several options by use of the choice window:

int choice = GUI.choiceWindow(
    "Title", 
    "Header", 
    "Message", 
    "Choice 0", 
    "Choice 1",
    etc...
);

This will create a dialogue with buttons to represent each option. Clicking a button will close the dialogue and return an integer representing the button that was pressed (starting at 0 for the first button).

For example:

int choice = GUI.choiceWindow(
    "Choice",
    "Select an option",
    "What do you want to do?",
    "Sweep Voltage",
    "Sweep Current",
    "Quit"
);

switch (choice) {

    case 0:
        // Code to sweep voltage
        break;

    case 1:
        // Code to sweep current
        break;

    case 2:
        System.exit(0);

}

This results in:

Clone this wiki locally