JTextArea に行番号を追加しようとしていますが、いくつか問題があります。行番号は表示されますが、正しくスクロールされません。
データ行 (ログ メッセージ) とそれに関連付けられた行番号を格納するカスタム クラスのリンク リストと、テキスト領域に表示するかどうかの可視性があります。そこで私がしたことは、2 つの JTextAreas を作成することでした... 1 つはログを保存するため、もう 1 つは行番号を保存するためです。
レイアウトが機能し、行番号がログに正しく入力されます。問題は、上下にスクロールしようとしたときです。スクロールするとログは適切に調整されますが、行番号は調整されません。最初に表示される最初の 28 行番号を超えるものは表示されません。スペースはただの空白です。
私のコードは以下の通りです:
public class CustomLineNumbers extends JFrame implements ActionListener
{
...
private JTextArea logField;
private JTextArea lineField;
private List<Log> logs;
public CustomLineNumbers()
{
...
logs = new ArrayList<Log>();
logField = new JTextArea(28, 68);
logField.setMargin(new Insets(0, 5, 0, 0));
logField.setEditable(false);
logField.setLineWrap(true);
logField.setWrapStyleWord(true);
lineField = new JTextArea();
lineField.setPreferredSize(new Dimension(25, 0));
lineField.setBackground(this.getForeground());
lineField.setBorder(OUTER);
lineField.setEditable(false);
initLogs();
updateLogView();
updateLineView();
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(logField);
scrollPane.setRowHeaderView(lineField);
scrollPane.setVertical...Policy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
...
}
private void initLogs()
{
// inits the data in the list
}
public void updateLogView()
{
logField.setText(""); // reset log field to nothing
for (int i = 0; i < logs.size(); i++)
{
// Append only if the line is visible
if (logs.get(i).getVisibility())
{
// if this isn't the first line,
// add a line break before appending
if (i > 0)
logField.append("\n");
logField.append(logs.get(i).getLine());
}
}
}
public void updateLineView()
{
lineField.setText(""); // reset log field to nothing
for (int i = 0; i < logs.size(); i++)
{
// Append only if the line is visible
if (logs.get(i).getVisibility())
{
// if this isn't the first line,
// add a line break before appending
if (i > 0)
lineField.append("\n");
lineField.append("" + logs.get(i).getLineNumber());
}
}
}
...
/***** Main Execution *****/
public static void main(String[] args) { ... }
}
何か案は?
ありがとう、