Friday, 31 March 2017

TextPane Example

import java.awt.Container;
import java.awt.EventQueue;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class TextPaneEx extends JFrame {

    private JTextPane textPane;

    public TextPaneEx() {

        initUI();
    }

    private void initUI() {

        textPane = new JTextPane();
        JScrollPane spane = new JScrollPane(textPane);

        textPane.setContentType("text/html");
        textPane.setEditable(true);

        loadFile();

        createLayout(spane);
      
        setTitle("JTextPane");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
  
    private void createLayout(JComponent... arg) {
      
        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);      
      
        gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);      

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createParallelGroup()
                .addComponent(arg[0])
        );

        pack();
    }      

    private void loadFile() {

        try {
            String cd = System.getProperty("user.dir") + "/";
            textPane.setPage("File:///" + cd + "test.html");
        } catch (IOException ex) {
            Logger.getLogger(this.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            TextPaneEx ex = new TextPaneEx();
            ex.setVisible(true);
        });
    }
}

No comments: