I have created a java application in which JTextpane is used as an editor. Now my problem is that when I select a block of text and press tab key, it automatically deletes the selected text contents. I don't expect these behaviour for my tool's editor. I need to add indendation of selected text to my editor just like in notepad++. How can I resolve this problem? Can any one please help me.. I am not able to resolve it.. Please help.. Thanks in advance...

Dani AI

Generated

Building on suggestions from and , the simplest, most predictable fix is to override the Tab key binding for your JTextPane and implement "indent" / "unindent" actions that operate on whole lines instead of letting the default insert replace the selection. The approach below enumerates paragraph elements (lines), inserts or removes a leading tab/spaces for each selected line, and restores a sensible selection afterward.

// bind once after creating the JTextPane
textPane.getInputMap().put(KeyStroke.getKeyStroke("TAB"), "indent");
textPane.getActionMap().put("indent", new AbstractAction() {
    @Override public void actionPerformed(ActionEvent e) {
        try {
            JTextPane tp = (JTextPane) e.getSource();
            Document doc = tp.getDocument();
            int start = tp.getSelectionStart(), end = tp.getSelectionEnd();
            Element root = doc.getDefaultRootElement();
            if (start == end) { // no selection -> insert a tab
                tp.replaceSelection("\t");
                return;
            }
            int startLine = root.getElementIndex(start);
            int endLine = root.getElementIndex(Math.max(0, end - 1));
            // insert a tab at the start of each affected line
            for (int i = startLine; i <= endLine; i++) {
                Element line = root.getElement(i);
                doc.insertString(line.getStartOffset(), "\t", null);
            }
            // recompute offsets and select the same visual lines
            root = doc.getDefaultRootElement();
            tp.setSelectionStart(root.getElement(startLine).getStartOffset());
            tp.setSelectionEnd(root.getElement(endLine).getEndOffset());
        } catch (BadLocationException ex) {
            Toolkit.getDefaultToolkit().beep();
        }
    }
});
// bind Shift+Tab similarly to remove a leading tab or up to N leading spaces per line

Notes and tips: handle Shift+Tab by scanning each line prefix and removing one tab or up to N spaces; recompute the element offsets after edits before adjusting selection; perform these edits on the EDT (Actions already run there); use an UndoManager and wrap multiple insert/remove operations in a CompoundEdit if you want the whole indent/unindent to be a single undo step. Test edge cases (selection at document end, mixed tabs/spaces) and decide whether to insert tabs or a configurable number of spaces to match the project's style.

Recommended Answers

All 2 Replies

I don't know about notepad++, but the behaviour you describe (type a tab and it replaces the currently-selected text) is standard behaviour all the way from notepad to Word.
If you want non-standard behaviour, implement a DocumentFilter (see the API doc and/or Google) for your JTextPane

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.