when i run this file and make a portion as bold, it reflects in UI.But when i try to unbold i am not able to do so.. what is the problem in this code? Thanks selva. import javax.swing.*; import java.awt.event.*; import java.awt.*; import javax.swing.text.StyledDocument; import javax.swing.text.*; import java.util.*; import javax.swing.event.*; import javax.swing.event.*; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.HTMLEditorKit; import java.io.*; import javax.swing.text.html.HTML;
htmlEditorKit = new HTMLEditorKit(); textpane.setEditorKit(htmlEditorKit);
button.addActionListener(this); //FormatToolBar toolbar = new FormatToolBar(); toolbar = new JButton("Bold"); toolbar.addActionListener(this); //toolbar.setTextPane(textpane); panel.add("Center",button); c.add("North",toolbar); 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){ if(e.getSource()==button) System.out.println(textpane.getText());
if(e.getSource()==toolbar){ 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; } System.out.println("the start position is "+startpostion); System.out.println("the mark position is "+mark); System.out.println("the end position is "+end); Element element = textpane.getStyledDocument().getCharacterElement(startpostion); AttributeSet attrs = element.getAttributes(); SimpleAttributeSet simpleAttr = new SimpleAttributeSet(); simpleAttr.addAttributes(attrs); if(StyleConstants.isBold(attrs)){ System.out.println("yes i am bold "); StyleConstants.setBold(simpleAttr,false); simpleAttr.removeAttribute(HTML.Tag.B); }else { System.out.println("yes i am not bold "); StyleConstants.setBold(simpleAttr,true); } textpane.getStyledDocument().setCharacterAttributes(startpostion,end-startpostion, simpleAttr, true); } }
public static void main(String args[]){ new Demo1();
} }
Tim Blommerde
Greenhorn
Joined: Sep 24, 2001
Posts: 21
posted
0
Dear Selvas Kumars, After copying your source and changing the line:
Into the following, which I guess it should be:
I was able to compile your application and run it. And it worked just fine. I could mark a piece of text, press the toolbar button and it became bold, while marking an already bold part and pressing the button, made the text normal again. So, for as far as I'm concerned, your application works fine. If your version still bugs, please tell us a bit more about which JDK you're using and maybe some other things that might be helpful. Greetings, Tim [This message has been edited by Tim Blommerde (edited September 24, 2001).]