simple program doesn't work (insertComponent)

Discussion in 'Java' started by Daniel Forster, Jun 24, 2004.

  1. 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();
    }
    }
    Daniel Forster, Jun 24, 2004
    #1
    1. Advertising

  2. Daniel Forster

    Alex Hunsley Guest

    Daniel Forster wrote:
    > 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
    Alex Hunsley, Jun 24, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Owen
    Replies:
    1
    Views:
    436
    Natty Gur
    Jul 24, 2003
  2. Replies:
    7
    Views:
    6,465
    Vlad Iliescu
    Apr 20, 2006
  3. Jack Klein
    Replies:
    0
    Views:
    445
    Jack Klein
    Aug 4, 2003
  4. MZ
    Replies:
    7
    Views:
    814
    Ed Mullen
    Mar 17, 2008
  5. Tilman
    Replies:
    0
    Views:
    403
    Tilman
    Mar 19, 2008
Loading...

Share This Page