Results 1 to 2 of 2
Thread: Understanding Text pane use
- 03-25-2011, 07:43 AM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Understanding Text pane use
I am attempting to allow the fonts to be changed when using a text pane and I am having some trouble understanding it.
I have read the sun tutorials, and I have read a few examples of how to change fonts with the text pane, many of these examples modify pre written font to be one type of font.
I am just having a hard time figuring out where to start(I know threads like these are frownedupon) but I am kind of going in a thought circle of what I am supposed to do.
I am confused, do I modify the document? Im assuming caret listeners are needed. Is there a way to set the caret to produce a certain font from the caret until changed? Im assuming to edit the selected text you need to use a caret listener for the mark and dot. Also, I want all this to happen when I make changes to a drop box. I haven't really given it too many attempts(mainly because I am unsure where to start).
Please help me out, and feel free to let me know what details I left out.Last edited by sunde887; 03-25-2011 at 08:45 AM.
- 03-25-2011, 09:38 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I have partially solved it, I have one question, however. I would also like some feedback.
I was able to get the text to update from the cursor forward with the following code
fontChoices, and sizeChoices are JCombo boxes with fonts and sizes.Java Code:CaretListener cl = new CaretListener(){ public void caretUpdate(CaretEvent e){ MutableAttributeSet att = text.getInputAttributes(); int size = 12; try{ size = Integer.parseInt(sizeChoices.getSelectedItem().toString()); } catch(NumberFormatException nfe){ size = 12; sizeChoices.setSelectedIndex(2); } if(boldToggle.isSelected()){ StyleConstants.setBold(att, true); StyleConstants.setFontSize(att, size); StyleConstants.setFontFamily(att, fontChoices.getSelectedItem().toString()); } else{ StyleConstants.setBold(att, false); StyleConstants.setFontSize(att, size); StyleConstants.setFontFamily(att, fontChoices.getSelectedItem().toString()); } if(italicToggle.isSelected()){ StyleConstants.setItalic(att, true); StyleConstants.setFontSize(att, size); StyleConstants.setFontFamily(att, fontChoices.getSelectedItem().toString()); } else{ StyleConstants.setItalic(att, false); StyleConstants.setFontSize(att, size); StyleConstants.setFontFamily(att, fontChoices.getSelectedItem().toString()); } text.grabFocus(); } };
boldToggle and italicToggle are JToggleButtons.
One of my questions is that when I change the font/size/bold/italic choices it takes a second to actually make the change. It will print out 2-3 letters of the old style before switching. Is there anyway to make this more immediate?
My other question is not being quite sure how to modify selected text. Is it done in the caretListener? Or am I supposed to handle that in that action listeners for the toggle buttons and combo boxes?
Also, is this code any good? Im sure it can improve a lot, but I am extremely unexperienced with JTextPanes and they seem kind of complex compared to the other text components I have used(mostly JTextArea)
Similar Threads
-
Tabbed pane
By sonal12 in forum AWT / SwingReplies: 13Last Post: 09-15-2010, 12:22 AM -
How to set the size of Drawing pane
By cassysumandak in forum New To JavaReplies: 2Last Post: 09-27-2009, 03:13 AM -
tabbed Pane .
By programmer_007 in forum AWT / SwingReplies: 2Last Post: 09-03-2009, 08:52 PM -
Drag out text editor pane
By p4ndaman in forum EclipseReplies: 3Last Post: 07-21-2008, 04:19 PM -
Tabbed pane using struts 2.x......?
By prabhurangan in forum Web FrameworksReplies: 1Last Post: 07-19-2008, 06:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks