Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
121 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...