开发者

Removing new line function when enter button is pressed Java

I have a text area that I would like to become blank when the enter button is pressed. I know this would normally be done with a setText method. However when I do thi开发者_C百科s, the text is removed but the new line function created by the return key being pressed. My question is, is the anyway of stopping this default action from happening?

thanks


Are you listening for the ENTER key on the text area and then clearing it? The following works for me:

final JTextArea ta = new JTextArea();
ta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            ta.setText("");
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }
});


Hi

I have a text area that I would like to become blank when the enter button is pressed.

This, I understand. Here's how you can do that:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main {
    public static void main(String[]args) {
        final JFrame frame = new JFrame();
        final JTextArea area = new JTextArea();
        area.setPreferredSize(new Dimension(200, 200));
        area.addKeyListener(new KeyAdapter(){
            @Override
            public void keyReleased(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                    area.setText("");
                }
            }
        });
        frame.add(area);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

I know this would normally be done with a setText method. However when I do this, the text is removed but the new line function created by the return key being pressed. My question is, is the anyway of stopping this default action from happening?

That, I don't understand.


The problem is probably that you are not consuming the keystroke event, and although the text area is cleared, the normal processing of the keystroke ends up inserting a newline.

Rather than trapping the keystroke event (which isn't necessarily portable) I would recommend using a DocumentFilter. There is a tutorial here that shows you how to write one. Implement the filter so that when there is a 'newline' in the insert or replace string, replace the entire contents of the document with "".

However this approach can't tell the difference between a newline typed at the keyboard and one pasted into the text area.


Before you clear the text you need to remove the new line code the return button left. You do that with the consume() method.

So to clear your text:

yourkeyevent.consume();
yourTextObject.setText("");


Instead, you can also use:

yourTextarea.setText(null);
yourTextarea.setCaretPosition(-1);


I solved your problem overriding the code in the method "public void keyTyped(KeyEvent e)" instead of "public void keyPressed(KeyEvent e)" and it works. Here the code:

package versione1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class WhiteEnter {
    final JFrame frame = new JFrame();
    private JTextArea area = new JTextArea();


    public static void main(String[]args) {

        WhiteEnter prova = new WhiteEnter();
        prova.run();
    }

    public void run(){
        area.setPreferredSize(new Dimension(200, 200));
        area.addKeyListener(new PressEnterKeyListener());

        frame.add(area);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public class PressEnterKeyListener implements KeyListener{

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }

        @Override
        public void keyTyped(KeyEvent e) {

            if (e.getKeyChar() == (KeyEvent.VK_ENTER)){
                try{
                    area.setText(null);
                    area.setCaretPosition(0);
                } catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜