Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JTextPane questions.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default JTextPane questions.

    I am working on building a text editor using a jtext pane, and I have run into some problems. I am fairly inexperienced when it comes to using jtextpanes, and I have read the tutorials on them, sadly, the tutorials don't exactly cover what I want and I am at a loss.

    To start, I would like to point out that I made a post on Java-forums.com, which has went about a week without a response and fell off the interesting pages, here is the link
    Understanding Text pane use - Java Forums

    Rather than being annoying there and bumping it repeatedly, I have decided to come here looking for help.

    My problem is updating the fonts and font sizes, and I am becoming quite frustrated with my attempts, and I'm beginning to lose interest. Having know idea really how to start can get quite frustrating. I seem to have gotten the caretListener which updates the carets font working. The problem is that it has a delay. After I make a change(change font, select bold, etc) it takes 1 button press before it updates. I am wondering, how can I force this update to occur immediately?

    Here is the code I have for the caretListener
    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(e.getDot() == e.getMark()){
    				startSelect = -1;
    				endSelect = -1;
    			}
    			else if(e.getDot() > e.getMark()){
    				startSelect = e.getMark();
    				endSelect = e.getDot();
    			}
    			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();
    		}
    	};

    The part with the getMark and getDot was my attempt to remedy my next problem.

    How can I change the font of selected text? I thought that I could declare two int variables for the class to use when text is selected but it's not working as I would like. I tried creating a document listener to accomplish this but I don't believe it's anywhere near correct, here is the code for it.

    DocumentListener dl = new DocumentListener(){
    		public void insertUpdate(DocumentEvent e){
    			//don't need this listener to do anything
    		}
    		public void removeUpdate(DocumentEvent e){
    			//don't need this listener to do anything
    		}
    		public void changedUpdate(DocumentEvent e){
    			MutableAttributeSet att = text.getInputAttributes();
    			int size;
    			if((startSelect <= 0) && (endSelect <= 0)){
    				//don't do anything
    			}
    			else if(startSelect < endSelect){
    				if(boldToggle.isSelected()){
    					StyleConstants.setBold(att, true);
    				}
    				else{
    					StyleConstants.setBold(att, false);
    				}
    				if(italicToggle.isSelected()){
    					StyleConstants.setItalic(att, true);
    				}
    				else{
    					StyleConstants.setItalic(att, false);
    				}
    				try{
    					size = Integer.parseInt(sizeChoices.getSelectedItem().toString());
    				} catch(NumberFormatException nfe){
    					size = 12;
    					sizeChoices.setSelectedIndex(2);
    				}
    				StyleConstants.setFontSize(att, size);
    				StyleConstants.setFontFamily(att, fontChoices.getSelectedItem().toString());
    			}
    		}
    	};

    please give me a hand in helping me understand this. I have found nothing that really covers these topics, and I am at a loss for how to do this.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: JTextPane questions.

    I am editing my post because I figured a lot of the problems out. I am now using an action which finds the selected values of 2 toggle buttons and 2 combo boxes and when they are clicked, it sets the selected text to that character attribute.

    My two remaining problems are the following:

    1.) Forcing the caret listener to immediately update font changes and not take typing one character before updating.

    2.) Querying an attribute set. I want to be able to look at a selected text area,and find out the attributes of that area, size, font name, bold or italics, and use this to set the buttons and the combo boxes.

Similar Threads

  1. [SOLVED] JTextPane focus problem
    By LeonLanford in forum AWT / Java Swing
    Replies: 3
    Last Post: June 21st, 2010, 11:50 PM
  2. A few questions
    By adenverd in forum Java Theory & Questions
    Replies: 3
    Last Post: May 26th, 2010, 03:34 AM
  3. Java JTextPane Save
    By ikurtz in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: April 28th, 2010, 01:02 AM
  4. Need help with setting multiple font styles in jTextPane
    By jch02140 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 23rd, 2010, 06:16 AM
  5. Replies: 3
    Last Post: April 20th, 2009, 08:35 AM