Exporting and sharing

IA tooling, not an exam topic. These are JavaFX and standard-library patterns to adapt for your IA. Nothing here is examinable, and your IA must be your own work.

This page is about getting data out of your app: writing a CSV that a spreadsheet can open, opening the user’s mail client pre-filled, and opening a web page.

New here? Read Start here: how every recipe fits together first; it explains the two places you edit and the onAction link every recipe relies on.


Export your data to a CSV file

What this does: writes your records as comma-separated rows to a file that spreadsheets open directly.

When you would use it in your IA: giving the client a report they can open in a spreadsheet, or exporting a table so the data can be used elsewhere. CSV needs no extra library, which is why it is preferred over true spreadsheet files.

In SceneBuilder (the layout): nothing to add beyond a button to trigger it. Add a Button and set its onAction to handleExport.

<Button text="Export CSV" onAction="#handleExport" />

In the controller (the .java file):

Imports:

import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.List;

Method:

private void exportCsv(File file, List<Item> items) {
    try (PrintWriter out = new PrintWriter(file)) {
        // your IA logic here: the header and one line per record, from your own fields
        out.println("Name,Quantity");                       // header row
        for (Item item : items) {
            out.println(item.getName() + "," + item.getQuantity());
        }
    } catch (IOException e) {
        // tell the user the export failed
    }
}

Where your own logic goes: the header line and the row line, marked // your IA logic here, which name your own columns and read your own getters. The file writing and try/catch are reusable wiring.

Make it your own:

  • Change the header line and the row line to match your own columns and getters.
  • Pair this with a save dialog so the user chooses where the file goes: get the File from Let the user choose a file with a "*.csv" filter.

Watch out for:

  • The method named in onAction (handleExport) must exist in the controller, or the screen will not load.
  • The one real gotcha with CSV is a value that itself contains a comma, for example a name like "Smith, John". It would split into two columns and shift the row. The simplest fixes are to wrap such values in double quotes ("Smith, John"), or to choose a delimiter the data will never contain, such as a semicolon or a tab.
  • A stray line break inside a value causes the same kind of problem. Keep exported values to single lines.

Mix with: Let the user choose a file, Display rows in a scrollable table.


Email from a button

What this does: opens the user’s own mail program with a new message already filled in, ready for them to review and send.

When you would use it in your IA: a “Contact support” or “Email this booking” button, where the user sends the message from their own account rather than your app sending it for them.

In SceneBuilder (the layout): nothing to add beyond a button to trigger it. Add a Button and set its onAction to handleEmail.

<Button text="Email support" onAction="#handleEmail" />

In the controller (the .java file):

Imports:

import java.awt.Desktop;
import java.net.URI;

Method:

private void emailSupport() {
    if (!Desktop.isDesktopSupported()) {
        return;                                  // not available on this system
    }
    try {
        // your IA logic here: the recipient, subject, and body for your app
        String to = "support@example.com";
        String subject = "Question about my booking";
        String body = "Hello,";

        URI mailto = new URI("mailto:" + to
            + "?subject=" + subject.replace(" ", "%20")
            + "&body=" + body.replace(" ", "%20"));
        Desktop.getDesktop().mail(mailto);
    } catch (Exception e) {
        // handle the error
    }
}

Where your own logic goes: the to, subject, and body strings, marked // your IA logic here. Building the mailto URI and the Desktop guard are reusable wiring.

Make it your own:

  • Change the to, subject, and body strings.
  • Spaces and other special characters need to be encoded in a URL, which is why the example replaces spaces with %20. For longer bodies, java.net.URLEncoder.encode(text, "UTF-8") does this more thoroughly.

Watch out for:

  • This opens the user’s mail client pre-filled and needs no password or server. That is the point: it is the safe approach for a school IA.
  • Do not try to send mail directly from your app with hardcoded login details (the JavaMail or SMTP approach). That needs a mail server and stored credentials in your code, which is poor practice and a security risk. The mail-client approach above avoids both.
  • Desktop lives in java.awt, but here it is only used to hand the request to the operating system, not to build any Swing window. The interface stays pure JavaFX.

Mix with: Open a web page from your app, Tell the user something.


Open a web page from your app

What this does: opens a web address in the user’s default browser.

When you would use it in your IA: a Help link to online documentation, an About link to a project page, or a link to a resource the app refers to.

In SceneBuilder (the layout): nothing to add beyond a button to trigger it. Add a Button and set its onAction to handleHelp.

<Button text="Help" onAction="#handleHelp" />

In the controller (the .java file):

Imports:

import java.awt.Desktop;
import java.net.URI;

Method:

private void openHelpPage() {
    if (!Desktop.isDesktopSupported()) {
        return;
    }
    try {
        // your IA logic here: the address you want to open
        Desktop.getDesktop().browse(new URI("https://example.com/help"));
    } catch (Exception e) {
        // handle the error
    }
}

Where your own logic goes: the address inside new URI(...), marked // your IA logic here. The Desktop guard and try/catch are reusable wiring.

Make it your own:

  • Change the address inside new URI(...) to the page you want to open.
  • The same Desktop.isDesktopSupported() guard applies as for email.

Watch out for:

  • The method named in onAction (handleHelp) must exist in the controller, or the screen will not load.
  • The address must be a full URL including https://, or new URI(...) may not open it as a web page.
  • On some systems (for example a headless server, or a locked-down lab machine) Desktop is not supported, which is exactly what the guard check protects against.

Mix with: Email from a button, Add actions to the menu bar.


© EduCS.me — A resource hub for Computer Science education

This site uses Just the Docs, a documentation theme for Jekyll.