D
Daniel Forster
does someone know, why this simple program does not work???
each time it detects ".*", this should be replaced by a button.
insertcomponent doesn't work. i tried with invokeLater and invokeAndWait -
nothing worked.
import java.awt.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.text.rtf.*;
import javax.swing.*;
class DF extends DocumentFilter {
JTextPane tp;
static boolean exit=false;
public DF(JTextPane t) {
tp=t;
}
public void replace(DocumentFilter.FilterBypass fb,int offset,int
length,String text,AttributeSet attr) throws BadLocationException {
if (exit) return;
fb.replace(offset,length,text,attr);
String pt=fb.getDocument().getText(0,fb.getDocument().getLength());
for (int x=1;x<pt.length();x++)
if ((pt.charAt(x)=='*')&&(pt.charAt(x-1)=='.')) {
exit=true;
tp.select(x-1,x+1);
tp.insertComponent(new JButton("Click Me"));
exit=false;
}
}
}
public class Uni extends JFrame {
protected JTextPane m_monitor;
DefaultStyledDocument m_doc;
public Uni() {
m_monitor=new JTextPane();
RTFEditorKit m_kit=new RTFEditorKit();
m_monitor.setEditorKit(m_kit);
StyleContext m_context=new StyleContext();
m_doc=new DefaultStyledDocument(m_context);
m_monitor.setDocument(m_doc);
getContentPane().add(m_monitor,BorderLayout.CENTER);
((AbstractDocument)m_monitor.getDocument()).setDocumentFilter(new
DF(m_monitor));
m_monitor.replaceSelection(" .* - * - .* * * ");
setSize(600,400);
setVisible(true);
}
public static void main(String[] args) {
Uni u=new Uni();
}
}
each time it detects ".*", this should be replaced by a button.
insertcomponent doesn't work. i tried with invokeLater and invokeAndWait -
nothing worked.
import java.awt.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.text.rtf.*;
import javax.swing.*;
class DF extends DocumentFilter {
JTextPane tp;
static boolean exit=false;
public DF(JTextPane t) {
tp=t;
}
public void replace(DocumentFilter.FilterBypass fb,int offset,int
length,String text,AttributeSet attr) throws BadLocationException {
if (exit) return;
fb.replace(offset,length,text,attr);
String pt=fb.getDocument().getText(0,fb.getDocument().getLength());
for (int x=1;x<pt.length();x++)
if ((pt.charAt(x)=='*')&&(pt.charAt(x-1)=='.')) {
exit=true;
tp.select(x-1,x+1);
tp.insertComponent(new JButton("Click Me"));
exit=false;
}
}
}
public class Uni extends JFrame {
protected JTextPane m_monitor;
DefaultStyledDocument m_doc;
public Uni() {
m_monitor=new JTextPane();
RTFEditorKit m_kit=new RTFEditorKit();
m_monitor.setEditorKit(m_kit);
StyleContext m_context=new StyleContext();
m_doc=new DefaultStyledDocument(m_context);
m_monitor.setDocument(m_doc);
getContentPane().add(m_monitor,BorderLayout.CENTER);
((AbstractDocument)m_monitor.getDocument()).setDocumentFilter(new
DF(m_monitor));
m_monitor.replaceSelection(" .* - * - .* * * ");
setSize(600,400);
setVisible(true);
}
public static void main(String[] args) {
Uni u=new Uni();
}
}