wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes run this file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "run this file" Watch "run this file" New topic
Author

run this file

selvas kumars
Ranch Hand

Joined: Jan 06, 2001
Posts: 115
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

Joined: Sep 24, 2001
Posts: 21
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).]
 
 
subject: run this file