import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
javax.swing.text.*;
public
class
JTextPaneExample {
public
static
void
main(String[] args)
{
SwingUtilities.invokeLater(() -> {
JFrame frame =
new
JFrame(
"JTextPane Example"
);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setSize(
400
,
300
);
JTextPane textPane =
new
JTextPane();
StyledDocument doc
= textPane.getStyledDocument();
Style style = doc.addStyle(
"customStyle"
,
null
);
StyleConstants.setForeground(style, Color.BLUE);
StyleConstants.setBold(style,
true
);
textPane.setStyledDocument(doc);
try
{
doc.insertString(
0
,
"This is a "
, style);
doc.insertString(doc.getLength(),
"button: "
,
null
);
}
catch
(BadLocationException e) {
e.printStackTrace();
}
JButton button =
new
JButton(
"Click Me!"
);
button.addActionListener(
new
ActionListener() {
@Override
public
void
actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(
frame,
"Button clicked!"
);
}
});
textPane.insertComponent(button);
frame.add(
new
JScrollPane(textPane));
frame.setVisible(
true
);
});
}
}