Hi swing gurus, Here is the sample file having textpane set to html editor kit.I am going through each attributeset of the character and changing the atribute still I am not able to make the necessary changes in UI..sometimes it works and sometimes not...(my version is jdk1.3.0).. My reuirement is like taking out html document from the textpane by calling textPane.getText()(I will get o/p in html format). I tried out giving StyledEditorKit.BoldAction in action listener..it will work well for the selected text and the time i make a getText() on textpane i won't have the latest html doc seen in UI.
Run the sample by changing this textpane.setText("hello how are you"); to textpane.setText("hello how are you"); Selection any portion and try to make all changes and see... Give me a solution to solve the problem...since i will set an html doc into a textpane and make change and get it back again a latest html doc seen in UI...How to proceed on this??? Try out our mind on this ..... Thanks & Regards, Silva.
htmlEditorKit = new HTMLEditorKit(); textpane.setEditorKit(htmlEditorKit);
button.addActionListener(this); bold = new JButton("Bold"); bold.addActionListener(this); italic = new JButton("italic"); underline = new JButton("underline"); italic.addActionListener(this); underline.addActionListener(this); panel.add("Center",button); JPanel pan = new JPanel(new GridLayout(1,3)); pan.add(bold); pan.add(italic); pan.add(underline); c.add("North",pan); c.add("Center",textpane); c.add("South",panel); textpane.setPreferredSize(new Dimension(300,400)); textpane.setText("hello how are you"); pack(); show(); } public void actionPerformed(ActionEvent e){ int startpostion = 0; int mark = textpane.getCaret().getMark(); int dot = textpane.getCaret().getDot(); int end = 0; if(mark<dot){> startpostion =mark; end = dot; }else { startpostion = dot; end =mark; } if(e.getSource()==button) System.out.println(textpane.getText());
if(e.getSource()==bold){ Element element = textpane.getStyledDocument().getCharacterElement(startpostion); AttributeSet attrs = element.getAttributes(); SimpleAttributeSet simpleAttr = new SimpleAttributeSet(); simpleAttr.addAttributes(attrs); boolean firstCharBold = false; if(StyleConstants.isBold(attrs)){ firstCharBold = false; }else { firstCharBold = true;