This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run this file

 
selvas kumars
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

public class Demo1 extends JFrame implements ActionListener {
public static JTextPane textpane= new JTextPane();
JPanel panel = new JPanel(new BorderLayout());
JTextPane textpane1 = new JTextPane();
public static StringBuffer buf= new StringBuffer("hello how are you");
String s1;
JButton button = new JButton("print1");
String news="";
Container c = getContentPane();
HTMLDocument htmlDoc = new HTMLDocument();
HTMLEditorKit htmlEditorKit;
JButton toolbar;
public Demo1(){
c.setLayout(new BorderLayout());

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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic