JTextArea Tab Tuşu Kullanımı

20-10-14

JTextArea'da Tab tuşuna basıldığında seçili satırların 4 birim ileri yönde hareket ettirilmesini ve Shift + Tab tuşuna basıldığında tersinil işlemin yapılmasını sağlamak için aşağıdaki kodları kullanabiliriz:

import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class Test extends JFrame {
    private JTextArea txtContent = new JTextArea(30, 150);
    
    public Test() {
        add(txtContent);
        txtContent.setDocument(new TabDocument());
        addKeyListener();
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    private static class TabDocument extends DefaultStyledDocument {
        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            str = str.replaceAll("\t", "");
            super.insertString(offs, str, a);
        }
    }
    private void addKeyListener() {
        txtContent.addKeyListener(new KeyAdapter() {
            private boolean isSelectedText = false;
            private int selectionStart = 0;
            
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_TAB  && e.isShiftDown()) {
                    selectionStart = txtContent.getSelectionStart();
                    new TabBackSpace(txtContent).actionPerformed(null);
                    isSelectedText = true;
                } else if (e.getKeyCode() == KeyEvent.VK_TAB) {
                    
                    if (txtContent.getSelectedText() != null) {
                        selectionStart = txtContent.getSelectionStart();
                        new TabSpace(txtContent).actionPerformed(null);
                        isSelectedText = true;
                        
                    } else {
                        txtContent.insert("    ", txtContent.getCaretPosition());
                    }
                }
            }
            
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_TAB && isSelectedText) {
                    int caretPosition = txtContent.getCaretPosition();
                    txtContent.setSelectionStart(selectionStart);
                    txtContent.setSelectionEnd(caretPosition);
                }
            }
        });
    }
    public static void main(String[] args) {
        new Test();
    }
}

Not: 36. satırdaki e.isShiftDown() metodu, Shift tuşuna basıldığı zaman true değerini alır.

TabSpace sınıfı

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

/**
 * TabSpace
 */
public class TabSpace extends AbstractAction {
    private JTextArea txtContent;

    public TabSpace(JTextArea txtContent) {
        this.txtContent = txtContent;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String selectedText = txtContent.getSelectedText();
        int selectionStart = txtContent.getSelectionStart();
        int selectionEnd = txtContent.getSelectionEnd();
        String[] lines = selectedText.split("\\n");
        String result = "";
        for (String line : lines) {
            result += "    " + line + "\n";
        }

        result=result.replaceAll("\\n$","");
        txtContent.replaceRange(result, selectionStart, selectionEnd);

    }
}


TabBack Sınıfı

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

/**
 * TabBackSpace
 */
public class TabBackSpace extends AbstractAction {
    private JTextArea txtContent;

    public TabBackSpace(JTextArea txtContent) {
        this.txtContent = txtContent;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String selectedText = txtContent.getSelectedText();
        int selectionStart = txtContent.getSelectionStart();
        int selectionEnd = txtContent.getSelectionEnd();
        String[] lines = selectedText.split("\\n");
        String result = "";
        for (String line : lines) {
            result += line.replaceAll("^(\\s{4})", "") + "\n";
        }
        txtContent.replaceRange(result.replaceAll("(\\n)$",""), selectionStart, selectionEnd);

    }
}

© 2019 Tüm Hakları Saklıdır. Codesenior.COM