菜单项的JTextArea行号 [英] JTextArea line numbers of menu item

查看:114
本文介绍了菜单项的JTextArea行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java的AWT和SWING概念开发项目.

I am trying to develop a project using AWT And SWING concepts of Java.

因为我有一个菜单项,叫做视线编号(即我们为此选择了JCheckBox)".当我选中复选框时,它在另一个文档中显示行号.但是,我想使用Document像Editplus Editor一样显示行号.

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.

这是我的代码

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

推荐答案

但是,我想使用Document像Editplus Editor一样显示行号.

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.

这篇关于菜单项的JTextArea行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆