Reputation:
I have two different scripts, both written in Java. The first one is used to check what key the user has pressed(logging). That script then turns the key into a string and returns
in a function. The second script is then a regular jFrame, which has a label. This jFrame calls the first scripts function to return/get the key logged string. The label in the jFrame is then set to change to that string every 1 second using a timer. There is just the issue that it doesn't seem like the second script (jFrame) is actually getting the key string, as the label isn't changing when I run the script. The label just stays blank, with no new information. Whenever I run it I have also set it to print the string to check if it gets it, which it doesn't as it wont print it. The timer function is working I am sure, as I am printing a line every second in the timer which tells me timer is working
. I am not completely sure what the reason is to this problem, I am aware of some difficulties when using static variables and the GUI declaration system. But I am not sure how to work around it, to make my code work. I don't get any errors either, which makes it even more difficult.
The first script is the following, it is the one which logs the keystrokes.
import java.io.FileNotFoundException;
import java.io.IOException;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class lauH1 implements NativeKeyListener {
public static String stringCarry = "";
public static void main(String[] args) throws FileNotFoundException, IOException {
checkBut(); //call keycheck function
int x = 0;
int y = 4;
PopUpJframe f = new PopUpJframe();
while (x<y){
if (f.isShowing()){
} else {
f.setVisible(true);
}
}
}
static void checkBut(){
try {
GlobalScreen.registerNativeHook();
} catch(Exception e) {
e.printStackTrace();
}
GlobalScreen.getInstance().addNativeKeyListener(new lauH1());
}
@Override
public void nativeKeyPressed(NativeKeyEvent e) {
stringCarry = NativeKeyEvent.getKeyText(e.getKeyCode());
System.out.println(stringCarry);
}
@Override
public void nativeKeyReleased(NativeKeyEvent e) {
}
@Override
public void nativeKeyTyped(NativeKeyEvent e) {
}
public static String getString() {
return stringCarry;
}
}
The second script is the one below, this is the jFrame which is supposed to get the logged string.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Timer;
public class lauH2 extends javax.swing.JFrame{
String stringFK = lauH1.getString(); //this is where I get the string
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel2.setText(stringFK);
System.out.println("Timer works");
System.out.println(stringFK);
}
});
public lauH2() {
initComponents();
setResizable(false);
timer.start();
}
/**
* 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")
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new lauH2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
Upvotes: 0
Views: 38
Reputation: 324108
String stringFK = lauH1.getString(); //this is where I get the string
Well that only gets the String once when the class is created.
You need to invoke the getString()
method in the ActionListener
of the Timer
.
public class lauH1 implements NativeKeyListener
Class names should start with an upper case character! Show me a class in the API that doesn't start with an upper case character. Follow Java naming conventions which are easy to learn just by following examples.
I am aware of some difficulties when using static variables
Yes, you should not be using a static variable to access another class. If you want one class to have access to methods of another class then pass a parameter to the class.
For example:
LauH1 l1 = new LauH1();
LauH2 l2 = new LauH2(l1);
Then in the constructor of the LauH2 class you save the Lauh1 variable as an instance variable in the class and now any method of the LauH2 class can access the LauH1 class and its methods.
while (x<y)
{
if (f.isShowing()){
} else {
f.setVisible(true);
}
}
What is the point of the above code? The values of x/y never change so you just created an infinite loop.
public class lauH2 extends javax.swing.JFrame{
An application should only have a single JFrame. If you need a second window then use a JDialog as the child window to the main frame.
Upvotes: 1