Here's the code (just copy the code from the lines that have more than 1
(P.S. the code doesn't un-bold the text -- just bolds it)
public class TextSamplerDemo extends JPanel
implements ActionListener {
private String newline = "\n";
protected static final String textFieldString = "JTextField";
private JTextPane textPane;
public TextSamplerDemo() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); JToolBar
toolBar = buildToolbar();
add(toolBar);
//Create a text pane.
textPane = createTextPane();
JScrollPane paneScrollPane = new JScrollPane(textPane);
paneScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
paneScrollPane.setPreferredSize(new Dimension(250, 155));
paneScrollPane.setMinimumSize(new Dimension(10, 10));
add(textPane);
}
}
private JToolBar buildToolbar() {
JToolBar toolBar = new JToolBar();
toolBar.setRollover( true );
toolBar.setFloatable( false );
JButton boldButton = new JButton("Bold");
boldButton.setToolTipText( "Set selected text to bold" );
boldButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
StyledDocument doc = textPane.getStyledDocument(); int
start = textPane.getSelectionStart(); int end =
textPane.getSelectionEnd();
doc.setCharacterAttributes(start, end-start,
doc.getStyle("bold"),true);
}
});
toolBar.add( boldButton );
return toolBar;
}
}