さて、私は同様のプロジェクトに取り組みました、そしてこれが私が思いついたものです。行番号に関しては、実際のテキストペインに接続されたスクロールペインを使用しました。スクロールペインは、次のコードで数字を変更していました。
public class LineNumberingTextArea extends JTextArea
{
private JTextPane textArea;
/**
* This is the contructor that creates the LinNumbering TextArea.
*
* @param textArea The textArea that we will be modifying to add the
* line numbers to it.
*/
public LineNumberingTextArea(JTextPane textArea)
{
this.textArea = textArea;
setBackground(Color.BLACK);
textArea.setFont(new Font("Consolas", Font.BOLD, 14));
setEditable(false);
}
/**
* This method will update the line numbers.
*/
public void updateLineNumbers()
{
String lineNumbersText = getLineNumbersText();
setText(lineNumbersText);
}
/**
* This method will set the line numbers to show up on the JTextPane.
*
* @return This method will return a String which will be added to the
* the lineNumbering area in the JTextPane.
*/
private String getLineNumbersText()
{
int counter = 0;
int caretPosition = textArea.getDocument().getLength();
Element root = textArea.getDocument().getDefaultRootElement();
StringBuilder lineNumbersTextBuilder = new StringBuilder();
lineNumbersTextBuilder.append("1").append(System.lineSeparator());
for (int elementIndex = 2; elementIndex < root.getElementIndex(caretPosition) +2;
elementIndex++)
{
lineNumbersTextBuilder.append(elementIndex).append(System.lineSeparator());
}
return lineNumbersTextBuilder.toString();
}
}
構文の強調表示は簡単な作業ではありませんが、私が始めたのは、特定の言語のすべてのキーワードを含むいくつかのテキストファイルに基づいて文字列を検索できるようにすることでした。基本的に、ファイルの拡張子に基づいて、関数は正しいファイルを見つけ、そのファイル内でテキスト領域に含まれている単語を探します。