Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Zwischenablage Zugriffsfehler

Empfohlene Antworten

Veröffentlicht

Hallo erstmal,

hab hier ein kleines Problem bei welchem ich einfach nicht weiterkomme. Ich habe eine Klasse, welche das Interface "ClipboardOwner" implementiert. Dieser soll ein JTextfield übergeben werden und sobald sich die Zwischenablage ändert soll der String in das JTextfield eingetragen werden. So weit so gut....Funktioniert auch AUSSER wenn man versucht aus einem anderen Java Programm zu kopieren z.B. eclipse oder auch selbstgeschriebene. Hat vlt. jemand ne Ahnung woran das liegen könnte? Ansonsten funktioniert es Wunderbar(ausser das sich das Textfeld auch aktualisiert wenn es selbst aktiv ist)

Hier mal mein Code:

import java.awt.Toolkit; 

import java.awt.datatransfer.*; 


import javax.swing.JTextField;

import java.io.IOException;


public class TextFieldClipBoardControl  implements ClipboardOwner  {


	private JTextField toControl;

	private Clipboard systemClipboard;

	private Transferable transferData;


	public TextFieldClipBoardControl() { 

		this.systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

		this.transferData = this.systemClipboard.getContents(this); 

		this.systemClipboard.setContents(this.transferData, this);

	}

	public void lostOwnership(Clipboard clipboard, Transferable contents) {

		this.transferData = clipboard.getContents(this); 

		for (DataFlavor dataFlavor : contents.getTransferDataFlavors()) 

		{ 

			try {

				Object content = this.transferData.getTransferData(dataFlavor); 

				if (content instanceof String) 

				{ 

					this.toControl.setText(String.valueOf(content)); 

					break; 

				}

			}

			catch (Exception e){


			}

		} 

		this.systemClipboard.setContents(this.transferData, this);

	}

	public void control(JTextField tf) {

		this.toControl = tf;

		for ( DataFlavor dataFlavor : this.transferData.getTransferDataFlavors() ) 

		{ 

			try {

				Object content = this.transferData.getTransferData( dataFlavor ); 

				if (content instanceof String) 

				{ 

					this.toControl.setText(String.valueOf(content)); 

					break; 

				}

			}

			catch (IOException e){


			}

			catch (UnsupportedFlavorException e) {


			}

		} 

	}

	public void put(String pPut){

		StringSelection contents = new StringSelection(pPut);

		this.systemClipboard.setContents(contents, this);


	}


}

Und die Fehlermeldung :

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: cannot open system clipboard

at sun.awt.windows.WClipboard.openClipboard(Native Method)...

Danke schonmal im vorraus für die Mühe

Grüße

Okey hab jetzt schonmal ein bisschen was gefixt. Hab das Interface direkt in das Frame implementiert und ich hatte die API falsch verstanden. Ich dachte bei lostOwnership steht das contents für den neuen Inhalt dabei war dies der alte. Das Problem, dass ich im selben Fenster kein strg+c machen kann bleibt. Soll ich das Interface direkt in das JTextfield implementieren??

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.