Results 1 to 5 of 5
  1. #1
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default Adding something to beginning of JTextArea

    Hey guys, i have a questions...

    1: Is there a way to add something to the beginning of each line in a JTextArea? I am making a notepad like application, and i would like to add something like in notepad++, where it displays what line you are on. I'd like it to work just like notepad++ where it doesn't display the number of rows not used yet.

    If that doesn't make sense i can try and clarify. Thanks in advance!

    -Huskies

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    275
    Rep Power
    4

    Default

    with JTextArea you can only add numbers or other kind of marks at the beginning of lines.
    simpliest way:

    for determining number of lines -
    Java Code:
    jTextArea.getLineCount()
    getting number of columns -
    Java Code:
    jTextArea.getColumns()
    calculate position in text to insert line numbers

    inserting text -
    Java Code:
    jTextArea.insert(String lineNumber, int position)
    consider using JEditorPane or JTextPane instead
    take a look:
    How to Use Editor Panes and Text Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Last edited by milovan; 08-27-2011 at 01:39 AM.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    With some googling you can probably find examples of what you are looking for. Like Milovan said, you will need to switch to JTextPane(which adds more power to the editor).

  4. #4
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    will do! thanks for the input

  5. #5
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    Having played around with the JTextPane, i am still confused. in the following code, i want to put the symbol ">>" in front of every line that is typed. I am trying to use a key listener, which probably isn't the best way, but i figured it was a good time to learn. here is my code:

    Java Code:
    package textpanetest;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class TextPaneTest {
    
        JTextPane text;
        
        public static void main(String[] args) {
            TextPaneTest test = new TextPaneTest();
            test.makeWindow();
        }
        
        private void makeWindow(){
            JFrame frame = new JFrame("Text Pane v1.0");
            frame.setSize(600,300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            
            text = new JTextPane();
            text.setFont(new Font("Lucida Console", Font.BOLD, 18));
            
            text.setText(">>");
            
            text.addKeyListener(new EnterKeyPressed());
            JScrollPane scroll = new JScrollPane(text);
            
            frame.getContentPane().add(BorderLayout.CENTER, scroll);
            frame.setVisible(true);
        }
        
        private void enter(String s, AttributeSet att){
            Document d = text.getDocument();
            int position = text.getCaretPosition();
            try{d.insertString(position,s,att);}
            catch(BadLocationException ble){}
        }
        
        private class EnterKeyPressed implements KeyListener{
    
            @Override
            public void keyTyped(KeyEvent e) {
            }
    
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if(keyCode == KeyEvent.VK_ENTER){
                    enter("\n>>",null);
                }
            }
    
            @Override
            public void keyReleased(KeyEvent e) {
            }
        
        }
    }
    i have 2 questions:

    1: when i run the application, the cursor automatically starts behind the first ">>". How can i get it to go in front of it

    2: when i click enter, this is what the text looks like (i know its a code box, but pretend its the textPane)

    Java Code:
    >>blah
    >>
    blah
    i hope that made sense. if not, i can clarify.

Similar Threads

  1. Beginning with Android
    By Dark in forum Android
    Replies: 10
    Last Post: 12-21-2011, 01:53 PM
  2. Beginning Swing Question
    By Huskies in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2011, 06:59 PM
  3. Ideas for Beginning Projects
    By Huskies in forum New To Java
    Replies: 8
    Last Post: 08-17-2011, 08:24 AM
  4. beginning - IF
    By sparkling in forum New To Java
    Replies: 26
    Last Post: 06-05-2011, 04:17 PM
  5. adding jtextarea
    By xhoneyskye in forum AWT / Swing
    Replies: 4
    Last Post: 01-26-2010, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •