views:

98

answers:

2

ChatGUI

im using 2 JEditorPane to transfer text from one to another.

once i have transfered the data i do the following:

JEditorPane.setText(null);

JEditorPane.setCaretPosition(0);

but as you can see from the attached image the return action makes the prompt appear a row down. how can i fix this?

EDIT: does the following seem correct to you? if so then why is caret not positioning itself to chracter 0 position?

    private class MyKeyAdapter extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent ke) {

        int kc = ke.getKeyCode();

        if (kc == ke.VK_ENTER) {

            System.out.println(editorPaneHistory.getText());

            System.out.println(editorPaneHomeText.getText());

            editorPaneHistory.setText(editorPaneHomeText.getText());

            //JEditorPane - editorPaneHistory
            //JEditorPane - editorPaneHomeText

            editorPaneHomeText.setText(null);

            editorPaneHomeText.setCaretPosition(0);

        }
    }
}
+1  A: 

After your code runs, the JEditorPane is reacting to the enter key in the usual way, by inserting a newline. Try calling ke.consume() to "consume" the event so that the JEditorPane itself doesn't handle it.

Etaoin
this looks great! many thanks.
iEisenhower
could provide a small example? in my attempts when i put ke.consume(); the "enter" stops responding.
iEisenhower
not sure if this is working or. will update tomorrow.
iEisenhower
forgot to mention. once i positioned .consume() correctly it, it performs flawlessly! thanks.
iEisenhower
A: 

Don't use a KeyListener. You should be using a custom Action. This way you can replace the default Action. Read up on Key Bindings.

camickr