JTextArea line numbers of menu item

最后都变了- 提交于 2019-12-13 07:56:37

问题


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);
  }

回答1:


But, I want to display the line numbers in same using Document like as Editplus Editor.

I doubt very much that the line numbers are part of the Document. They may appear to be displayed as part of the same component, but I'm sure that when you copy/paste text you don't get the line numbers included.

Assuming my above statement is correct you can try using the Text Component Line Number. Since this a component displayed in the row header of the scroll pane you should be able to toggle the visibility of the component based on your checkbox.



来源:https://stackoverflow.com/questions/22859721/jtextarea-line-numbers-of-menu-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!