Hallo,
ich möchte folgendes programmieren:
Wenn sich der inhalt der Zwischenablage (Clipboard) ändert und ein String ist, soll der Text eines Labels durch den Inhalt der Zwischenablage ersetzt werden.
Mein Problem:
Ich habe bereits eine Klasse, die eine Konsolenausgbe macht, wenn sich der Inhalt der Zwischenablage (ClipboardListener.java) ändert und ich habe eine JFrame form Klasse (jForm). Jedoch kann ich von der ClipboardListener Klasse nicht auf die Labels in meiner Gui zugreifen und ich kann von der jForm klasse nicht auf die ClipbordListener Klasse zugreifen. Wie schaffe ich eine Kommunikation zwischen den Klassen?
ClipBoardListener
jForm
ich möchte folgendes programmieren:
Wenn sich der inhalt der Zwischenablage (Clipboard) ändert und ein String ist, soll der Text eines Labels durch den Inhalt der Zwischenablage ersetzt werden.
Mein Problem:
Ich habe bereits eine Klasse, die eine Konsolenausgbe macht, wenn sich der Inhalt der Zwischenablage (ClipboardListener.java) ändert und ich habe eine JFrame form Klasse (jForm). Jedoch kann ich von der ClipboardListener Klasse nicht auf die Labels in meiner Gui zugreifen und ich kann von der jForm klasse nicht auf die ClipbordListener Klasse zugreifen. Wie schaffe ich eine Kommunikation zwischen den Klassen?
ClipBoardListener
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jvok;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import java.awt.datatransfer.*;
/**
*
* @author matthias
*/
public class ClipBoardListener extends Thread implements ClipboardOwner{
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
@Override
public void run() {
Transferable trans = sysClip.getContents(this);
TakeOwnership(trans);
}
@Override
public void lostOwnership(Clipboard c, Transferable t) {
try {
ClipBoardListener.sleep(250); //waiting e.g for loading huge elements like word's etc.
} catch(Exception e) {
System.out.println("Exception: " + e);
}
Transferable contents = sysClip.getContents(this);
try {
process_clipboard(contents, c);
} catch (Exception ex) {
Logger.getLogger(ClipBoardListener.class.getName()).log(Level.SEVERE, null, ex);
}
TakeOwnership(contents);
}
void TakeOwnership(Transferable t) {
sysClip.setContents(t, this);
}
public void process_clipboard(Transferable t, Clipboard c) { //your implementation
String tempText;
Transferable trans = t;
try {
if (trans != null?trans.isDataFlavorSupported(DataFlavor.stringFlavor):false) {
tempText = (String) trans.getTransferData(DataFlavor.stringFlavor);
System.out.println(tempText);
}
} catch (Exception e) {
}
}
/*
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
*/
}
jForm
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jvok;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
/**
*
* @author matthias
*/
public class jForm extends javax.swing.JFrame {
/**
* Creates new form jForm
*/
ArrayList<String> cliphistory = new ArrayList<>();
public jForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel1.setText("Deutsch");
jTextField2.setText("jTextField1");
jLabel2.setText("Englisch");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(211, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(63, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(78, 78, 78))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(jForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(jForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(jForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(jForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new jForm().setVisible(true);
}
});
new ClipBoardListener().start();
}
public void process_clipboard(Transferable t, Clipboard c) {
String tempText;
Transferable trans = t;
try {
if (trans != null?trans.isDataFlavorSupported(DataFlavor.stringFlavor):false) {
tempText = (String) trans.getTransferData(DataFlavor.stringFlavor);
System.out.println(tempText+"_____");
}
} catch (Exception e) {
}
}
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UNDEFINED){
System.out.println("Kein Unicode-Character gedr\u00FCckt!");
}
}
public void keyPressed(KeyEvent e) {
System.out.println("Tastenposition: " + e.getKeyLocation());
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE){
System.out.println("Programmabbruch!");
System.exit(0);
}
System.out.println("Taste: " + e.getKeyChar() + ", Code: " + e.getKeyCode());
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}