Skip to main content

Styled Text Using JEditorPane

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" ]
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]

Comments

  1. Hello , you are good writer, i love www.texoid.org

    ReplyDelete

Post a Comment

Popular posts from this blog

Facebook Login In PHP

As we all know Time is Money. When there's a long form on your site for registration you could sometimes lose your visitors. To make this registration step a success, adding a small social login button to your site may cut down lot of burden on visitors side and will help you in getting more attention on your data rather than wasting time on the lengthy registration process. By doing so visitor will be glad to see that you care for their precious time.   What do you think will there be anyone in this small world who is not having a Facebook account? According to recent stats 1 in every 13 person in this world is using facebook.

USB Boot Procedure

Simple step-by-step solution to create a bootable usb in cmd: 1)   Take a pen drive of more than 4gb . 2)   Now the most important step is to open cmd from start->search program and files->type cmd . 3)   Now in the new window, type diskpart and hit enter. 4)   Type the command list disk . Now, note the disk which have size identical to that of your pen drive. eg. disk 1. 5)   Type the command select disk 1 . 6)   Then type clean and hit enter. 7)   Just type create partition primary and press enter. 8)  Then run the command list partition . 9)  Type select partition 1 and hit enter. 10) Format it using the command format fs=ntfs . 11) Type the commands active , assign and exit in sequence. Now just copy all the files from Boot-able disc of  windows 7 to the pen drive and your pen drive is ready to be used as a boot-able device.