Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

359
Views
How to create a paste button in Java that works in Chrome, using a native function calling Javascript?

Currently, my implementation works in IE but not in Chrome.

I have a button labelled "Paste Text" which when clicked, should paste the contents currently stored in the clipboard to a text box. Both the button and text box are created in the same Java class that the following functions are defined.

I have a function that returns a ClickHandler doing the paste action. It calls a native function getClipboardContents() to read the current clipboard data:

ClickHandler pasteClipboardToTextBox(final CustomTextBox textBox) {
    return new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            textBox.setValue(getClipboardContents());
        }
    };
}

The following function uses JS to get the clipboard data:

private native String getClipboardContents() /*-{
    return window.clipboardData.getData('Text');
}-*/;

I can add the ClickHandler to the paste button so when it's clicked, it'll paste the contents of clipboard into the passed text box object:

pasteButton.addClickHandler(pasteClipboardToTextBox(myTextBox));

I'm aware that this line:

window.clipboardData.getData('Text')

does not work in Chrome, so I'd need an alternative method of getting the clipboard data in a similar way, but that works in Chrome. I've heard that its possible to use

e.clipboardData.getData('text/plain')

but I'm unsure how to get this working in a native Java function.

Any help would be much appreciated!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!