Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

360
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda