Java JTextArea line numbers

I am trying to add line numbers to JTextArea and I am having some difficulties. The line numbers will appear, but they are not scrolling as expected.

I have a linked list of a custom class that stores a data line (log message) and its associated line number, as well as the visibility of whether it should be shown in the text area or not. So what I did was create two JTextAreas ... one to hold the logs and the other to hold the line numbers.

The layout works and the line numbers are correctly populated by the logs. The problem is when I'm trying to scroll up or down. The logs adjust correctly when scrolling, but the line numbers are not displayed. Nothing appears behind the starting 28 line numbers that are shown originally. The space is simple.

My code is below:

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) { ... }
}

      

Any ideas?

Thank,

+2


source to share


2 answers


Have you tried putting both textboxes in the viewPort? Maybe in a panel that gives line numbers in terms of the available width?



+4


source


The component used to display line numbers can be added with:

scrollPane.setRowHeaderView(...);

      



Another option is to use JTable to display both columns. Using two text areas is really not the best solution.

+3


source







All Articles