Wonderful bright scene of Grundlsee lake in austrian Alps. Amazing nature landscape in sunny day. Great view on park and lake. Awesome summer scene. Grundlsee. Austria. Europe

Create powerful Word Processor using this Java code

Start
				
					import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class WordProcessor
{
    JFrame frame;
    JPanel panel;
    JTextPane textPane;
    JScrollPane scrollPane;
    JPopupMenu menu;
    JMenuBar menuBar;
    JButton btnBold, btnItalic, btnUnderline, btnLeft, btnCenter, btnRight, btnFont, btnColor;
    JFileChooser fileChooser;
    File file;
    String fontName;
    int fontSize;
    Color fontColor;

    public static void main(String[] args)
    {
        new WordProcessor();
    }

    public WordProcessor()
    {
        // Create the frame
        frame = new JFrame("Word Processor");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create the main panel
        panel = new JPanel(new BorderLayout());
        frame.add(panel);

        // Create the textPane
        textPane = new JTextPane();
        textPane.setBackground(Color.WHITE);
        textPane.setSelectionColor(Color.BLUE);
        textPane.setSelectedTextColor(Color.WHITE);

        // Create the scrollpane
        scrollPane = new JScrollPane(textPane);
        panel.add(scrollPane, BorderLayout.CENTER);

        // Create the popup menu
        menu = new JPopupMenu();
        menu.add(new JMenuItem("Cut")).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textPane.cut();
            }
        });
        menu.add(new JMenuItem("Copy")).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textPane.copy();
            }
        });
        menu.add(new JMenuItem("Paste")).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textPane.paste();
            }
        });
        textPane.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    menu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        // Create the menubar
        menuBar = new JMenuBar();
        menuBar.add(new JMenu("File")).add(new JMenuItem("New")).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textPane.setText("");
            }
        });
        menuBar.add(new JMenuItem("Open")).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fileChooser = new JFileChooser();
                int returnVal = fileChooser.showOpenDialog(frame);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        file = fileChooser.getSelectedFile();
                        FileInputStream fis = new FileInputStream(file);
                        byte[] data = new byte[(int) file.length()];
                        fis.read(data);
                        fis.close();
                        String str = new String(data, "UTF-8");
                        textPane.setText(str);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        menuBar.add(new JMenuItem("Save")).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (file == null) {
                    fileChooser = new JFileChooser();
                    int returnVal = fileChooser.showSaveDialog(frame);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        file = fileChooser.getSelectedFile();
                    }
                }
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    String str = textPane.getText();
                    fos.write(str.getBytes());
                    fos.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        frame.setJMenuBar(menuBar);

        // Create the toolbar
        JToolBar toolBar = new JToolBar();
        btnBold = new JButton("B");
        btnBold.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setBold(attr, !StyleConstants.isBold(attr));
                doc.setCharacterAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        btnItalic = new JButton("I");
        btnItalic.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setItalic(attr, !StyleConstants.isItalic(attr));
                doc.setCharacterAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        btnUnderline = new JButton("U");
        btnUnderline.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setUnderline(attr, !StyleConstants.isUnderline(attr));
                doc.setCharacterAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        btnLeft = new JButton("Left");
        btnLeft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setAlignment(attr, StyleConstants.ALIGN_LEFT);
                doc.setParagraphAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        btnCenter = new JButton("Center");
        btnCenter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
                doc.setParagraphAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        btnRight = new JButton("Right");
        btnRight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setAlignment(attr, StyleConstants.ALIGN_RIGHT);
                doc.setParagraphAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        btnFont = new JButton("Font");
        btnFont.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FontDialog fontDialog = new FontDialog(frame, fontName, fontSize);
                fontDialog.setVisible(true);
                fontName = fontDialog.getFontName();
                fontSize = fontDialog.getFontSize();
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setFontFamily(attr, fontName);
                StyleConstants.setFontSize(attr, fontSize);
                doc.setCharacterAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        btnColor = new JButton("Color");
        btnColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ColorDialog colorDialog = new ColorDialog(frame, fontColor);
                colorDialog.setVisible(true);
                fontColor = colorDialog.getColor();
                StyledDocument doc = (StyledDocument) textPane.getDocument();
                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setForeground(attr, fontColor);
                doc.setCharacterAttributes(textPane.getSelectionStart(), textPane.getSelectionEnd() - textPane.getSelectionStart(), attr, true);
            }
        });
        toolBar.add(btnBold);
        toolBar.add(btnItalic);
        toolBar.add(btnUnderline);
        toolBar.add(btnLeft);
        toolBar.add(btnCenter);
        toolBar.add(btnRight);
        toolBar.add(btnFont);
        toolBar.add(btnColor);
        panel.add(toolBar, BorderLayout.NORTH);

        // Show the frame
        frame.setVisible(true);
    }
}
				
			
Previous Story

C++ code to create a Solitaire game

Next Story

Do you remember COBOL? I grew up with this language