Results 1 to 5 of 5
  1. #1
    peterme is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Unhappy Format editing in HTML view with JEditorPane

    Hi
    I wonder if anybody can help me out in this: my code is getting more and more complex, but I'm just not getting there:

    I want to make a text-editor, which can do some simple text-formatting too. For this I am using a JEditorPane in HTML-mode.

    By selecting a part of the text, and then clicking a 'Bold'-button, I want to make the selected part of the text bold. (Like in MS Word)
    The program should simply surround the selected text with <b>...</b> tags, to make it bold.

    This sounds really easy, but it is a pain in the neck.

    Problem is, that I select the text in HTML-formatted view (which is the only thing I want to see really), but the tags need to be set in the HTML-code in plain-text-view.

    The methods getSelectionStart and getSelectionEnd refer only to the HTML-view.

    Now I need to get the right corresponding Start- and End-points for the HTML-code in plain-text-view, where the tags need to be put.

    For this I iterate through the HTML-code, but it gets quite complicated, and I just donít get it right, as it keeps making mistakes, if I play around a bit.

    I just feel there should be an easier answer to this.

    Can anybody help?
    Cheers, Peter

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.text.BadLocationException;
    
    public class MyPane1 extends JFrame implements ActionListener {
    
        private JEditorPane editorPane;
        private JPanel control;
        private JScrollPane editorScrollPane;
        private JButton bold, italic;
        private JButton code, clear;
    
        public MyPane1() {
            editorPane = new JEditorPane();
            editorPane.setContentType("text/html");
            editorPane.setEditable(true);
            editorPane.setText("<h1>Hello</h1><i>this is italic</i><br>this is a <a href='123'>referenz</a><br>the end");
            editorScrollPane = new JScrollPane(editorPane);
    
            control = new JPanel();
            bold = new JButton("<html><b>B");
            bold.addActionListener(this);
            control.add(bold);
            italic = new JButton("<html><i>I");
            italic.addActionListener(this);
            control.add(italic);
            code = new JButton("code");
            code.addActionListener(this);
            control.add(code);
            clear = new JButton("clear");
            clear.addActionListener(this);
            control.add(clear);
    
            setSize(300, 300);
            setLayout(new BorderLayout());
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            add(editorScrollPane, BorderLayout.CENTER);
            add(control, BorderLayout.SOUTH);
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(bold)) {
                editing("b");
            }
            if (e.getSource().equals(italic)) {
                editing("i");
            }
            if (e.getSource().equals(clear)) {
                editorPane.setText(null);
            }
            if (e.getSource().equals(code)) {
                String t = editorPane.getText();
                if (editorPane.getContentType().equals("text/plain")) {
                    editorPane.setContentType("text/html");
                } else {
                    editorPane.setContentType("text/plain");
                }
                editorPane.setText(t);
            }
        }
    
        public void editing(String what) {
            int codeSelectStart = 0;
            int codeSelectEnd = 0;
            String text = editorPane.getText();
            int selectStart = editorPane.getSelectionStart();
            int selectEnd = editorPane.getSelectionEnd();
            int max = text.length();
            String tag = "";
            Boolean tagged = false;
            int ii = 0;
            for (int i = 0; i < max; i++) {
                char ch = text.charAt(i);
                if (ch == '<' && !tagged) {
                    tagged = true;
                    tag = "";
                } else {
                    if (ch == '>' && tagged) {
                        tagged = false;
                        if (tag.equals("br") || tag.equals("/h1")) { // several other tags would qualify, but are not used in this example
                            ii++;
                            if (selectStart == ii) {
                                codeSelectStart = i + 1;
                            }
                            if (selectEnd == ii) {
                                codeSelectEnd = i + 1;
                            }
                        }
                    } else {
                        if (tagged) {
                            tag = tag + ch;
                        } else {
                            try {
                                if (editorPane.getText(ii + 1, 1).equals(" ") || Character.getNumericValue(ch) > -1) {
                                    ii++;
                                    if (ii == selectStart) {
                                        codeSelectStart = i;
                                    }
                                    if (ii == selectEnd - 1) {
                                        codeSelectEnd = i + 1;
                                    }
                                    System.out.print("(" + ii + ")" + ch);
                                }
                            } catch (BadLocationException ex) {
                                Logger.getLogger(MyPane1.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                }
            }
            text = text.substring(0, codeSelectStart) + "<" + what + ">" + text.substring(codeSelectStart, codeSelectEnd) + "</" + what + ">" + text.substring(codeSelectEnd, max);
            editorPane.setText(text);
        }
    
        public static void main(String[] args) {
            new MyPane1();
        }
    }

  2. #2
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    I am struggling with the exact same problem, if anyone have a solution, it would be greatly appreciated.

    private void buttonBold_actionPerformed(ActionEvent e) {
    try {
    kit.insertHTML((HTMLDocument)textPaneEmail.getDocu ment(), textPaneEmail.getSelectionStart(), "<b>", 0, 0, null);
    kit.insertHTML((HTMLDocument)textPaneEmail.getDocu ment(), textPaneEmail.getSelectionEnd(), "</b>", 0, 0, null);
    } catch (BadLocationException f) {
    f.printStackTrace();
    } catch (IOException f) {
    f.printStackTrace();
    }
    }

    this is the method that is supposed to make the selected text bold, however it's not working, any advice
    Last edited by oomrichie; 02-20-2011 at 09:24 AM.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    With JTextPane you can give styles to selected text and it is a subclass of JEditorpane meaning you can keep the html formatting and other JEditorPane tid bits.

    here is a run of making text bold...

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.text.Style;
    import javax.swing.text.StyleConstants;
    
    
    public class Test extends JFrame implements ActionListener {
    
        private JPanel control;
        private JScrollPane editorScrollPane;
        private JButton bold, italic;
        private JButton code, clear;
        private JTextPane p;
        private Style bolder;
    
        public Test() {
        	p=new JTextPane();
            
        	bolder=p.addStyle("bold",null);
        	StyleConstants.setBold(bolder,true);
    
            p.setContentType("text/html");
            p.setEditable(true);
            p.setText("<h1>Hello</h1><i>this is italic</i><br>this is a <a href='123'>referenz</a><br>the end");
            editorScrollPane = new JScrollPane(p);
    
            control = new JPanel();
            bold = new JButton("<html><b>B");
            bold.addActionListener(this);
            control.add(bold);
            italic = new JButton("<html><i>I");
            italic.addActionListener(this);
            control.add(italic);
            code = new JButton("code");
            code.addActionListener(this);
            control.add(code);
            clear = new JButton("clear");
            clear.addActionListener(this);
            control.add(clear);
    
            setSize(300, 300);
            setLayout(new BorderLayout());
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            add(editorScrollPane, BorderLayout.CENTER);
            add(control, BorderLayout.SOUTH);
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(bold)) {
            	makeBold();
            }
            
        }
        public void makeBold(){
        	//System.out.println("here");
          int len=p.getSelectedText().length();
          p.getStyledDocument().setCharacterAttributes(p.getSelectionStart(),len,bolder,true);
        }
        
        public static void main(String[] args) {
            new Test();
        }
    }

  4. #4
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    Thanks The code works perfectly, but I have one more question. How do I remove the bold from text (un bold).

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Inside the makeBold() method check before hand if the selected text already contains the attribute bolder, if it does then we know the text is bold and can add a new style to that sets bold to false and apply it to the selected text:

    I will start you off
    Java Code:
    public void makeBold(){
    
    	    	if(p.getSelectedText()!=null){
    
    	    		System.out.println(p.getCharacterAttributes());
    
    	    	        int len=p.getSelectedText().length();
                            // perform check to see if this piece of text has the attribute bolder
    		    	if(p.getCharacterAttributes().containsAttributes(bolder)){
                                     // create a new Style to make text non bold
    		    		 // add the new Style to to the seledted piece of text
    		    	 }
                            // Text was not bold so make it so
    		    	else{
    			    	 p.getStyledDocument().setCharacterAttributes(p.getSelectionStart(),len,bolder,false);
    			    	 p.setCaretPosition(p.getSelectionStart());
    		    	}
    	    	}
    	    }

Similar Threads

  1. How enter this mode-gui/design-view/code-view
    By lse123 in forum NetBeans
    Replies: 0
    Last Post: 02-28-2010, 10:09 PM
  2. Replies: 1
    Last Post: 01-22-2010, 06:23 PM
  3. JEditorPane, HTML 4.0, CSS, custom Tags
    By AndreB in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2009, 11:45 AM
  4. jeditorpane help parsing html
    By asifsolkar in forum Advanced Java
    Replies: 4
    Last Post: 12-14-2007, 06:23 AM
  5. How to view applet from html page.
    By jwzumwalt in forum Java Applets
    Replies: 2
    Last Post: 11-24-2007, 05:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •