-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineNumberPanel.java
35 lines (29 loc) · 977 Bytes
/
LineNumberPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import javax.swing.*;
import java.awt.*;
public class LineNumberPanel extends JPanel {
private JTextArea textArea;
public LineNumberPanel(JTextArea textArea) {
this.textArea = textArea;
setBackground(Color.LIGHT_GRAY);
setPreferredSize(new Dimension(50, textArea.getHeight()));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawLineNumbers(g);
}
private void drawLineNumbers(Graphics g) {
int lineHeight = textArea.getFontMetrics(textArea.getFont()).getHeight();
int numberOfLines = textArea.getLineCount();
g.setColor(Color.BLACK);
// Drawing line numbers
for (int i = 0; i < numberOfLines; i++) {
String lineNumber = String.valueOf(i + 1);
g.drawString(lineNumber, 5, (i + 1) * lineHeight);
}
}
public void updateLineNumbers() {
revalidate();
repaint();
}
}