simple program doesn't work (insertComponent)

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();
}
}
 
A

Alex Hunsley

Daniel said:
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.


comp.lang.java.help would be a more appropriate newsgroup.
Hint: don't populate, and especially don't show, GUI items in a constructor.

alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top