I am making a simple text editor in Java so that I can write programs. I am stuck on making an ActionListener
that is activated whenever a space is entered.
So, when you are typing into the text area and you press space, it will read the last word you typed.
Here are the essential parts of my code.
I have made an action listener and all of the other stuff, now I just need to make it read spaces.
JTextArea textarea = new JTextArea("@ECHO off" + "\n");
Font textareaFont = new Font("Arial", Font.BOLD, 15);
action event = new action();
textarea.setFont(textareaFont);
textarea.setForeground(Color.BLUE);
textarea.setLineWrap(true);
textframe.getContentPane().add(new JScrollPane(textarea));
textframe.setSize(525,715);
textframe.setLocationRelativeTo(null);
textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textframe.setVisible(true);
}
private class action implements ActionListener {
public void actionPerformed(ActionEvent event) {
}
}
Sorry if I am not being clear.
I think you would want a KeyListener, not an ActionListener.
Perhaps something like this:
textarea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_SPACE) {
//do your stuff here
}
}
}