Re: Problem with DocumentEvent.GetChange
On Dec 5 2008, 5:18 pm, Andrew Thompson <andrewtho...@gmail.com>
wrote:
On Dec 5, 4:57 pm, mick.heyw...@gmail.com wrote:
...Any ideas?
Post an SSCCE* that has been run through the TWC**.
* <http://pscode.org/sscce.html>
** Linked from *.
Cheers,
No worries.
--
Andrew Thompsonhttp://pscode.org/
Fair call, example follows. I'm sure it could be shorter, but this
compiles and should show what the problem is.
import java.util.ArrayList;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import javax.swing.*;
public class MainJFrame extends JFrame {
public MainJFrame() {
initComponents();
AddListener();
}
@SuppressWarnings("unchecked")
private void initComponents() {
EditScrollPane = new javax.swing.JScrollPane();
EditPane = new javax.swing.JTextPane();
LogScrollPane = new javax.swing.JScrollPane();
LogTextArea = new javax.swing.JTextArea();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
EditScrollPane.setViewportView(EditPane);
LogTextArea.setColumns(20);
LogTextArea.setRows(5);
LogScrollPane.setViewportView(LogTextArea);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(
GroupLayout.Alignment.TRAILING)
.addComponent(LogScrollPane,
GroupLayout.Alignment.LEADING,
GroupLayout.DEFAULT_SIZE,
380, Short.MAX_VALUE)
.addComponent(EditScrollPane,
GroupLayout.Alignment.LEADING,
GroupLayout.DEFAULT_SIZE,
380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(EditScrollPane,
GroupLayout.PREFERRED_SIZE,
232,
GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(LogScrollPane,
GroupLayout.PREFERRED_SIZE,
105,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
pack();
}
private void AddListener() {
try {
Document doc = EditPane.getDocument();
doc.addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
logChange(e);
}
public void insertUpdate(DocumentEvent e) {
logChange(e);
}
public void removeUpdate(DocumentEvent e) {
logChange(e);
}
private void logChange(DocumentEvent e) {
StringBuffer s = new StringBuffer();
s.append(e.getType().toString() + ":");
if (e.getDocument()
instanceof DefaultStyledDocument) {
try {
DefaultStyledDocument doc =
(DefaultStyledDocument)
e.getDocument();
s.append(doc.getText(
e.getOffset(),
e.getLength()));
ArrayList<Element> els =
new ArrayList<Element>();
GetAllElements(doc, els);
DocumentEvent.ElementChange chg
= null;
for (Element elem : els) {
chg = e.getChange(elem);
if (chg != null) {
s.append("Found One!!"
+ elem.getName());
break;
}
}
} catch (Exception ex) {
LogTextArea.append(
ex.getMessage());
}
}
LogTextArea.append(s.toString());
}
});
} catch (Exception e) {
LogTextArea.append(e.getMessage());
}
}
private void GetAllElements(Document doc,
ArrayList<Element> array) {
Element[] roots = doc.getRootElements();
for (Element el : roots) {
array.add(el);
GetAllElements(el, array);
}
}
private void GetAllElements(Element el,
ArrayList<Element> array) {
for (int i = 0; i < el.getElementCount(); i++) {
array.add(el.getElement(i));
GetAllElements(el.getElement(i), array);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainJFrame().setVisible(true);
}
});
}
private JTextPane EditPane;
private JScrollPane EditScrollPane;
private JScrollPane LogScrollPane;
private JTextArea LogTextArea;
}