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

    Default 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 09:45 AM.

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

    Default

    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
    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();
    		}
    	};
    fontChoices, and sizeChoices are JCombo boxes with fonts and sizes.
    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

  1. Tabbed pane
    By sonal12 in forum AWT / Swing
    Replies: 13
    Last Post: 09-15-2010, 01:22 AM
  2. How to set the size of Drawing pane
    By cassysumandak in forum New To Java
    Replies: 2
    Last Post: 09-27-2009, 04:13 AM
  3. tabbed Pane .
    By programmer_007 in forum AWT / Swing
    Replies: 2
    Last Post: 09-03-2009, 09:52 PM
  4. Drag out text editor pane
    By p4ndaman in forum Eclipse
    Replies: 3
    Last Post: 07-21-2008, 05:19 PM
  5. Tabbed pane using struts 2.x......?
    By prabhurangan in forum Web Frameworks
    Replies: 1
    Last Post: 07-19-2008, 07:48 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
  •