IDEで作業しているときに見られるように、タブをjeditorpaneのスペースに変換する方法があるかどうか疑問に思っています。タブサイズを設定したくない。私はすでにそれを簡単に行うことができます。
スペース内のタブを同等のものに置き換えたい。たとえば、タブの長さが5スペースの場合、すべてのタブを作成するたびにすぐに5スペースに置き換えます。
何か案は?
IDEで作業しているときに見られるように、タブをjeditorpaneのスペースに変換する方法があるかどうか疑問に思っています。タブサイズを設定したくない。私はすでにそれを簡単に行うことができます。
スペース内のタブを同等のものに置き換えたい。たとえば、タブの長さが5スペースの場合、すべてのタブを作成するたびにすぐに5スペースに置き換えます。
何か案は?
テキストが に挿入されるときにタブをスペースに置き換えるDocumentFilter
には、 に a を追加します。AbstractDocument
Document
詳細については、テキスト コンポーネントの機能に関する Swing チュートリアルのセクションを参照してください。
簡単な例:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class TabToSpaceFilter extends DocumentFilter
{
@Override
public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributes)
throws BadLocationException
{
replace(fb, offset, 0, text, attributes);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributes)
throws BadLocationException
{
// In case someone tries to clear the Document by using setText(null)
if (text == null)
text = "";
super.replace(fb, offset, length, text.replace("\t", " "), attributes);
}
private static void createAndShowGUI()
{
JTextArea textArea = new JTextArea(5, 20);
AbstractDocument doc = (AbstractDocument) textArea.getDocument();
doc.setDocumentFilter( new TabToSpaceFilter() );
JFrame frame = new JFrame("Integer Filter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout( new java.awt.GridBagLayout() );
frame.add( new JScrollPane(textArea) );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
EventQueue.invokeLater( () -> createAndShowGUI() );
}
}