insertComponent in jtextpane and spacebelow-attribute

From: Peter Wieser (p.troxler_at_hispeed.ch)
Date: 05/07/04

  • Next message: Andrew Thompson: "Re: insertComponent in jtextpane and spacebelow-attribute"
    Date: Fri, 7 May 2004 14:45:42 +0100
    
    

    in the following program i try to set the spacebelow attribute - but it
    doesn´t work; why?

    and i want to insert the string "test" with an own component (see code). i
    want to use insertComponent(new MyComponent()); and not insertString,
    because i have to do some extra drawing (circle around the text).
    how can i do, so that the inserted text has exactly the same attributes like
    the text at the insertion position and the same baseline? the original text
    has a lot of different styles.

    thanks

    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import java.lang.String;
    import java.lang.StringBuffer;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.rtf.*;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.Style;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.JTextPane;

    class MyComponent extends JComponent {
      public Dimension getPreferredSize() {
        //return new Dimension(a,h);
        return new Dimension(40,30);
      }
      public MyComponent() {
      }
      public Dimension getMaximumSize() {
        return getPreferredSize();
      }
      public void paint(Graphics g) {

        Dimension d=getSize();
        g.drawString("test",0,10);
      }
    }

    public class mo extends JFrame {
      protected JTextPane pane;
      protected DefaultStyledDocument styledDoc;
      protected StyleContext context;
      protected RTFEditorKit kit;
      protected JFileChooser m_chooser;
      protected SimpleFilter m_rtfFilter;
      protected JToolBar m_toolBar;
      private StringBuffer m;
      int i;
      float sb;

    public mo() {
        pane=new JTextPane();
        kit=new RTFEditorKit();
        pane.setEditorKit(kit);
        context=new StyleContext();
        styledDoc=new DefaultStyledDocument(context);
        pane.setDocument(styledDoc);
        pane.setPreferredSize(new Dimension(300, 800));
        JScrollPane ps=new
    JScrollPane(pane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZO
    NTAL_SCROLLBAR_NEVER);
        getContentPane().add(ps,BorderLayout.CENTER);
        Document doc=pane.getDocument();
        File fChoosen=new
    File("c://j2sdk1.4.2_03//Eigenes//Universe//trigokap10b.rtf");
        try {
          InputStream in=new FileInputStream(fChoosen);
          styledDoc=new DefaultStyledDocument(context);
          kit.read(in,styledDoc,0);
          pane.setDocument(styledDoc);
          in.close();
        }
        catch (Exception ex) {
          System.out.println("Problems encountered: Note that RTF support is
    still under development.");
        }
        AttributeSet atr=pane.getParagraphAttributes();
        float sb=StyleConstants.getSpaceBelow(atr);
                    SimpleAttributeSet attr=new SimpleAttributeSet();
                    StyleConstants.setSpaceBelow(attr,sb*1.5f);
        pane.selectAll();
        pane.setParagraphAttributes(attr,true);
        for (int u=2;u<8;u++) {
          pane.moveCaretPosition(u*2);
          pane.select(u*2,u*2);
          pane.insertComponent(new MyComponent());
        }
        pack();
        setVisible(true);
      }
      public void actionPerformed(ActionEvent e) {

      }
      public static void main(String argv[]) {
        new mo();
      }
    }


  • Next message: Andrew Thompson: "Re: insertComponent in jtextpane and spacebelow-attribute"

    Relevant Pages