Twinkie
Banned
- Messages
- 1,389
- Reaction score
- 12
- Points
- 0
How can I repaint elements in a panel? I was told all modifications to an element have to be done before being added to a panel. How can you repaint an element after it has been added to panel, then added to a window? Example code bellow.
Outcome: The window appears without errors but the text typed does not appear in the window.
It is acknowledged that this is not the most efficient way to do this, I am just practicing. Apparently it was worth it
Code:
package keylogger;
import javax.swing.*;
import java.awt.*;
public class Main {
static JLabel text;
static JFrame window;
public static void main(String[] args) {
window = new JFrame("Keylogger");
window.setSize(400, 100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new FlowLayout(FlowLayout.CENTER));
text = new JLabel();
window.add(new JScrollPane(text));
window.addKeyListener(new Writter());
window.setVisible(true);
}
}
Code:
package keylogger;
import java.awt.event.*;
public class Writter extends KeyAdapter {
public void keyTyped(KeyEvent event) {
Main.text.setText(Main.text.getText() + event.getKeyChar());
Main.window.repaint();
}
}
It is acknowledged that this is not the most efficient way to do this, I am just practicing. Apparently it was worth it
Last edited: