JEditorPane is a class of The Package Swing which, supports styled document . With the help of JEditorPane you can use custom text formats.
JEditorPane can be used as Following program:
[box type="shadow" ]
[/box]
JEditorPane can be used as Following program:
[box type="shadow" ]
package javaapplication2;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class NewJFrame2 extends javax.swing.JFrame {
public NewJFrame2() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jEditorPane1.setEditable(false);
jScrollPane1.setViewportView(jEditorPane1);
HTMLEditorKit kit = new HTMLEditorKit();//allows user to embed hmtl code
jEditorPane1.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body {color:#000;background-color:#e0ffff; font-family:times; margin: 4px; }");
styleSheet.addRule("h1 {color: blue;}");
styleSheet.addRule("h2 {color: #ff0000;}");
styleSheet.addRule("pre {font : 10px monaco; color : black; background-color : #fafafa; }");
String htmlString = "<html>\n"
+ "<body>\n"
+ "<h1>Welcome!</h1>\n"
+ "<h2>This is an H2 header</h2>\n"
+ "<p>This is some sample text</p>\n"
+ "</body>\n";
Document doc = kit.createDefaultDocument();
jEditorPane1.setDocument(doc);
jEditorPane1.setText(htmlString);
jButton1.setBackground(new java.awt.Color(255, 51, 51));
jButton1.setFont(new java.awt.Font("Comic Sans MS", 3, 24)); // NOI18N
jButton1.setForeground(new java.awt.Color(255, 255, 255));
jButton1.setText("Close");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(4, 4, 4))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
[/box]
Hello , you are good writer, i love www.texoid.org
ReplyDelete