I am trying to develop a project using AWT And SWING concepts of Java.
In that I have an one menu item called "Viewline Numbers (i.e. we have chosen a JCheckBox
for that)". When I check the Check-box it is displaying line numbers in another Document. But, I want to display the line numbers in same using Document like as Editplus Editor.
Here is my code
private void ViewLineNumbersActionPerformed(java.awt.event.ActionEvent evt) {
lines = new JTextArea("");
lines.setBackground(Color.LIGHT_GRAY);
lines.setEditable(false);
lines.setSize(10,10);
tx.getDocument().addDocumentListener(new DocumentListener(){
public String getText(){
int caretPosition = tx.getDocument().getLength();
// System.out.println("caretPosition"+ caretPosition);
Element root = tx.getDocument().getDefaultRootElement();
// System.out.println("root"+ root);
String text = "1" + System.getProperty("line.separator");
int c=root.getElementIndex( caretPosition );
// System.out.println(c);
for(int i = 2; i < c + 2; i++){
text += i + System.getProperty("line.separator");
}
return text;
}
@Override
public void `enter code here`changedUpdate(DocumentEvent de) {
lines.setText(getText());
}
@Override
public void insertUpdate(DocumentEvent de) {
lines.setText(getText());
}
@Override
public void removeUpdate(DocumentEvent de) {
lines.setText(getText());
}
});
sp.getViewport().add(tx);
// sp.setViewportView(tx);
sp.setRowHeaderView(lines);
}
See Question&Answers more detail:os