Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

spacebelow-attribute  &  insert text in jtextpane with an own insertcomponent-method

Thread view: 
Peter Wieser - 07 May 2004 14:39 GMT
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();
 }
}
Andrew Thompson - 07 May 2004 13:58 GMT
> in the following program..

The text lines are too long and wrap, it won't compile.

C:\mo.java:44: Class SimpleFilter not found.
 protected SimpleFilter m_rtfFilter;
SimpleFilter is missing as well, ..it won't compile.

You have funny names..
public class mo extends JFrame {
..do you also have larry and curly?   ;-)

Class names in Java should start with a Capital,
and are rarely two letters, a better name might
be.. 'TextRenderExample' or 'TextRenderFrame'.

There are further tips for preparing an example, here..
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.