سؤال

أحاول إضافة أرقام خط إلى JTextarea وأواجه بعض الصعوبات. تظهر أرقام الخط ، لكنها لا تمرير بشكل صحيح.

لدي قائمة مرتبطة بفئة مخصصة تخزن سطرًا من البيانات (رسالة السجل) ورقم سطر مرتبط به وكذلك الرؤية حول ما إذا كان ينبغي عرضه في منطقة النص أم لا. إذن ما فعلته هو إنشاء اثنين من jtextareas ... واحدة لتخزين السجلات ، والآخر لتخزين أرقام الخط.

يعمل التصميم وأرقام الخطوط المملوءة بشكل صحيح مع السجلات. المشكلة هي عندما أحاول التمرير لأعلى أو لأسفل. تعدل السجلات بشكل صحيح أثناء التمرير ، لكن أرقام الخط لا. لا شيء يظهر وراء أرقام الأسطر الـ 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) { ... }
}

أيه أفكار؟

شكرًا،

هل كانت مفيدة؟

المحلول

هل حاولت وضع كلا حقائلي النص في موقع Viewport؟ ربما على لوحة تعطي أرقام الخط جزءًا من العرض المتاح؟

نصائح أخرى

يمكن إضافة compenent المستخدمة لعرض أرقام الأسطر باستخدام:

scrollPane.setRowHeaderView(...);

خيار آخر هو استخدام JTable لعرض كلا العمدين. إن استخدام منطقتين نصين ليس هو الحل الأفضل حقًا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top