Results 1 to 5 of 5
- 08-26-2011, 10:51 PM #1
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
- 08-27-2011, 01:23 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
with JTextArea you can only add numbers or other kind of marks at the beginning of lines.
simpliest way:
for determining number of lines -
getting number of columns -Java Code:jTextArea.getLineCount()
calculate position in text to insert line numbersJava Code:jTextArea.getColumns()
inserting text -
consider using JEditorPane or JTextPane insteadJava Code:jTextArea.insert(String lineNumber, int position)
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.
- 08-27-2011, 01:43 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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).
- 08-27-2011, 06:09 AM #4
will do! thanks for the input
- 08-30-2011, 12:01 AM #5
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:
i have 2 questions: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) { } } }
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)
i hope that made sense. if not, i can clarify.Java Code:>>blah >> blah
Similar Threads
-
Beginning with Android
By Dark in forum AndroidReplies: 10Last Post: 12-21-2011, 01:53 PM -
Beginning Swing Question
By Huskies in forum AWT / SwingReplies: 2Last Post: 08-17-2011, 06:59 PM -
Ideas for Beginning Projects
By Huskies in forum New To JavaReplies: 8Last Post: 08-17-2011, 08:24 AM -
beginning - IF
By sparkling in forum New To JavaReplies: 26Last Post: 06-05-2011, 04:17 PM -
adding jtextarea
By xhoneyskye in forum AWT / SwingReplies: 4Last Post: 01-26-2010, 03:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks