2

I have written a simple scala swing application, with a text field and a button. Whenever user clicks on the button, the content gets copied to the clipboard. But, as soon as the application is closed, the clipboard content is lost. Why ? Is there a way to get around this ?

This is my source code :

package com.loloof64.scalatest

import java.awt.Dimension
import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.StringSelection
import java.awt.datatransfer.Transferable
import scala.swing.BoxPanel
import scala.swing.Button
import scala.swing.MainFrame
import scala.swing.SimpleSwingApplication
import scala.swing.TextField
import scala.swing.Orientation
import scala.swing.event.ButtonClicked

object ClipboardCopyTest extends SimpleSwingApplication {

  def writeToClipboard(str:String) {
      val clipboard:Clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
      val transferData:Transferable = new StringSelection(str)
      clipboard.setContents(transferData, null)
  }

  override def top = new MainFrame {
    val textfield = new TextField(60)
    val button = new Button("Copy to clipboard") 
    contents = new BoxPanel(Orientation.Vertical){
      contents += (textfield, button)
      listenTo(button)
      reactions += {
        case _:ButtonClicked => writeToClipboard(textfield.text)
      }
    }
    val prefSize = new Dimension(300,200)
    override def size = prefSize
  }

}

helps are welcome

i am under ubuntu 13.10 64 bits and I have scala 2.10.3

loloof64
  • 5,252
  • 12
  • 41
  • 78

1 Answers1

4

I would look at this answer.

In short its a known problem with Ubuntu.

From their site on the subject.

"The problem happens because Xorg takes a conservative approach to copying. It copies only a reference to the original data when the user performs a select or copy. It doesn't go and retrieve the actual data from the source program until the user requests a paste. It saves a lot of unneeded transfer of data this way, at the expense of having no way of retrieving data from a closed program that hasn't saved its clipboard somewhere else. "

Community
  • 1
  • 1
Halfwarr
  • 7,853
  • 6
  • 33
  • 51
  • thanks, problem is solved for me meanwhile, as it is just the OS special case. Anyway, I still can warn Ubuntu or derivatives os users :) – loloof64 Feb 07 '14 at 17:11