Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: create buttons in a JEditorPane

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default create buttons in a JEditorPane

    Hi, (sorry for my bad english)

    i am trying to develop an HTML editor with some "spezial" features.
    The generated HTML Code should only contains H1, H2, H3 and P tags.

    Every kind of Tag should get some special features in the editor.

    for example: if users marks text as H1, on the left side of the H1 Headline a input-box should appear where the user can add some keywords for this headline.
    if user writes an Paragrapch (p-tag), a button should appear where user can add some kind of media to this paragraph.

    these special funktions are not changing the html code. the informatons would be sepeately stored in database.
    but the input area should be directly in the editor.

    i tryed it by overriding the EditorKid and the View (paintChild) methods. But i'm only able to put out some text not other elements.


    public class HTMLEditor extends JFrame implements ActionListener {
     
        private HTMLDocument document;
        private JEditorPane textPane = new JEditorPane();
        /*
    	...
        */
        public HTMLEditor() {
          super("HTMLEditor");
          ReportEditorKit editorKit = new ReportEditorKit();
          textPane.setEditorKit(editorKit);
          /*
    	  ...
          */
        }
     
        /*
    	...
        */
     
        class ReportEditorKit extends HTMLEditorKit {
          @Override
          public ViewFactory getViewFactory() {
    	return new HTMLEditorKit.HTMLFactory() {
    	  public View create(Element elem) {
    	    Object o = elem.getAttributes().getAttribute(
    			StyleConstants.NameAttribute);
    	    if (o instanceof HTML.Tag) {
    	      HTML.Tag kind = (HTML.Tag) o;
     
    	      if (kind == HTML.Tag.P ) {
    		return new ParagraphView(elem);
    	      }
    	      if (kind == HTML.Tag.H2 ) {
    		return new BlockHeadlineView(elem);
    	      }
    	      /*
    		 ...
    	      */
    	    }  
    	    return super.create(elem);
    	  }
    	};
          }
        }
     
         /*
    	...
        */
     
    }
     
    /* 
      ReportView moves Content 120 Pxl to the right 
      to make way for extra elements
    */
    class ReportView extends javax.swing.text.html.ParagraphView {
     
      JButton test;
     
      public ReportView(Element e) {
          /*
    	...
        */
      }
     
      @Override
      protected void setInsets(short top, short left, short bottom,
        short right) {super.setInsets
        (top,(short)(left+120),
        bottom,right);
      }
     
      @Override
      public void paintChild(Graphics g, Rectangle r, int n) {
        super.paintChild(g, r, n);
      }
    }
     
    /*
      BlockHeadlineView (H2) 
      Outputs "Headline" in front of an H2 Headline
      And should display an Test-Button ... but don't work
    */
    class BlockHeadlineView extends ReportView {
     
      public BlockHeadlineView(Element e){
        super(e);
        test = new JButton("Test");
      }
      @Override
      public void paintChild(Graphics g, Rectangle r, int n) {
        super.paintChild(g, r, n);
     
        if(n == 0) {
    	int numberX = r.x - getLeftInset();
    	int numberY = r.y + r.height - 5;
    	g.drawString("Titel", numberX, numberY);	// this works
     
    	// with the following lines i tryed to display the test-button
    	// but nothing workes
     
    	// test.paint(g);
    	// getContainer().add(test);
    	// getContainer().validate();
    	// getContainer().repaint();
        }
      }
    }
     
    /*
     ...
    */


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: create buttons in a JEditorPane

    If I understand your problem correctly, you may wish to look into using an an OverlayLayout, in which you can place a component on top of the editor pane (set opaque to false so it appears invisible), and then add/remove components to this as needed

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: create buttons in a JEditorPane

    yes,

    i need something like a panel on the left side of the editor.
    while writing in editor in this panel should appear buttons or something else.

    for example: in most source-code editors there is a panel on the left side. while writing your code in these panels appear some marks when code is incorrect or this [+] and [-] Code Folds. these elements belongs to a part/line of the code but the dont realy manipulate the code.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: create buttons in a JEditorPane

    I'm still a bit unclear of the behavior you want, but you can add a panel to the inset of the editor. Google "line numbers in JEditorPane" and you will see how to add line numbers...adding other drawn elements is very similar by customizing the drawing. Interacting with that element in the inset is a bit more difficult, but you could have a separate text editor that changes based upon user mouse clicks in the sidebar of the editor pane.

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: create buttons in a JEditorPane

    Google "line numbers in JEditorPane" and you will see how to add line numbers
    my actual code is based on the "Add Line Numbering in the JEditorPane" example on this page: Add Line Numbering in the JEditorPane — Developer.com

    adding other drawn elements is very similar
    i don't know what i'm doning wrong. but all i tryed would not work. I can add linenumbers or some other String to each paragraph but if i try to put out a Button insetead of the String nothing happens.

    /* this will put out the String "Button" to each Paragraph - this works  */ 
    class MyParagraphView extends javax.swing.text.html.ParagraphView {
    ...
      public void paintChild(Graphics g, Rectangle r, int n) {  
        super.paintChild(g, r, n);
        g.drawString("Button", numberX, numberY);	// this works
      }
    }
     
    /* this will draw a Button instead of the Paragraph */
    class MyParagraphView extends ComponentView {
      protected Component createComponent() {
        JButton button = new JButton("Button");
          return button;
      }
    }

    My problem is to put out the button on the left side AND the HTML.-formated Text beside the button
    Last edited by seeker; December 5th, 2009 at 09:04 AM.

Similar Threads

  1. JRadio buttons
    By chronoz13 in forum AWT / Java Swing
    Replies: 0
    Last Post: November 29th, 2009, 01:08 AM
  2. Need more buttons!
    By GotWankel? in forum AWT / Java Swing
    Replies: 0
    Last Post: October 5th, 2009, 01:08 AM
  3. Convert contents of JTextArea / JEditorPane to PDF
    By rangarajank in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 30th, 2009, 02:38 PM
  4. how to create exe jar
    By ttsdinesh in forum Java Theory & Questions
    Replies: 1
    Last Post: September 27th, 2009, 08:21 AM
  5. [SOLVED] Using html Radio Buttons with Servlets
    By oss_2008 in forum Java Servlet
    Replies: 2
    Last Post: June 25th, 2009, 05:39 AM