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.