miit0o
miit0o

Reputation: 15

How do I print the content of a TextArea?

So currently I'm trying to make a print feature for my Notepad application. I already have a kind of working Print feature, but it prints the full TextArea not only the string that is written into it.

I already tried to make it just print a string, but the PrintJob is not able to handle it, because it needs the actual TextArea, where the Text is written into.

My current Print Stuff:

public void doPrint() {
        String toPrint = writeArea.getText();
        printSetup(writeArea, Main.primaryStage);
    }

    private final Label jobStatus = new Label();

    private void printSetup(Node node, Stage owner)
    {
        // Create the PrinterJob
        PrinterJob job = PrinterJob.createPrinterJob();

        if (job == null)
        {
            return;
        }

        // Show the print setup dialog
        boolean proceed = job.showPrintDialog(owner);

        if (proceed)
        {
            print(job, node);
        }
    }

    private void print(PrinterJob job, Node node)
    {
        // Set the Job Status Message
        jobStatus.textProperty().bind(job.jobStatusProperty().asString());

        // Print the node
        boolean printed = job.printPage(node);

        if (printed)
        {
            job.endJob();
        }
    }

What I want to have: A print that only shows the String, just like any other notepad application does if you try to print something

What I currently get: The full textarea with frame.

Upvotes: 0

Views: 314

Answers (1)

Sunflame
Sunflame

Reputation: 3186

As I mentioned in the comment you can wrap it into a Text, but then the first line for some reason isn't displayed correctly.

The solution would be to use a Label instead like:

printSetup(new Label(toPrint), Main.primaryStage);

enter image description here

Upvotes: 1

Related Questions