Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9076701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:10:20+00:00 2026-06-16T19:10:20+00:00

Possible Duplicate: What components should I use for building a Java WYSIWYG HTML editor

  • 0

Possible Duplicate:
What components should I use for building a Java WYSIWYG HTML editor

I’m a total newbie in Java programming. I have to do text editor in Swing/AWT and I have one question about it. How can I edit one selected word, for example change its color? Which component and which functions should I use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:10:21+00:00Added an answer on June 16, 2026 at 7:10 pm

    For a beginner to Java Swing, try to keep this project simple. To show multiple colors and multiple sizes in the same document requires a lot of complex coding and rendering html.

    Try to just provide the basic copy,cut,paste features because they are easier to implement.

    To provide those features, a JTextArea is sufficient.

    Try this. It’s a pretty simple text editor

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Document extends JFrame implements ActionListener
    {
    private JTextArea ta;
    private int count;
    private JMenuBar menuBar;
    private JMenu fileM,editM,viewM;
    private JScrollPane scpane;
    private JMenuItem exitI,cutI,copyI,pasteI,selectI,saveI,loadI,statusI;
    private String pad;
    private JToolBar toolBar;
    public Document()
    {
        super("Document");
        setSize(600, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
    
        count = 0;
        pad = " ";
        ta = new JTextArea(); //textarea
        menuBar = new JMenuBar(); //menubar
        fileM = new JMenu("File"); //file menu
        editM = new JMenu("Edit"); //edit menu
        viewM = new JMenu("View"); //edit menu
        scpane = new JScrollPane(ta); //scrollpane  and add textarea to scrollpane
        exitI = new JMenuItem("Exit");
        cutI = new JMenuItem("Cut");
        copyI = new JMenuItem("Copy");
        pasteI = new JMenuItem("Paste");
        selectI = new JMenuItem("Select All"); //menuitems
        saveI = new JMenuItem("Save"); //menuitems
        loadI = new JMenuItem("Load"); //menuitems
        statusI = new JMenuItem("Status"); //menuitems
        toolBar = new JToolBar();
    
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
    
        setJMenuBar(menuBar);
        menuBar.add(fileM);
        menuBar.add(editM);
        menuBar.add(viewM);
    
        fileM.add(saveI);
        fileM.add(loadI);
        fileM.add(exitI);
    
        editM.add(cutI);
        editM.add(copyI);
        editM.add(pasteI);        
        editM.add(selectI);
    
        viewM.add(statusI);
    
        saveI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        loadI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
        cutI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
        copyI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
        pasteI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
        selectI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
    
        pane.add(scpane,BorderLayout.CENTER);
        pane.add(toolBar,BorderLayout.SOUTH);
    
        saveI.addActionListener(this);
        loadI.addActionListener(this);
        exitI.addActionListener(this);
        cutI.addActionListener(this);
        copyI.addActionListener(this);
        pasteI.addActionListener(this);
        selectI.addActionListener(this);
        statusI.addActionListener(this);
    
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e) 
    {
        JMenuItem choice = (JMenuItem) e.getSource();
        if (choice == saveI)
        {
            //not yet implmented
        }
        else if (choice == exitI)
            System.exit(0);
        else if (choice == cutI)
        {
            pad = ta.getSelectedText();
            ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
        }
        else if (choice == copyI)
            pad = ta.getSelectedText();
        else if (choice == pasteI)
            ta.insert(pad, ta.getCaretPosition());
        else if (choice == selectI)
            ta.selectAll();
        else if (e.getSource() == statusI)
        {
            //not yet implmented
        }
    }
    public static void main(String[] args) 
    {
        new Document();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Duplicating components at Run-Time I have a TMyControl ( Control1 ) with
Possible Duplicate: When should I use “using” blocks in C#? A lot of classes
Possible Duplicate: Smart Gwt components are not rendered in html div tag My problem
Possible Duplicate: Components Inside Textbox/RichTextbox in DotNet I have a textbox inside of my
Possible Duplicate: Use Zend Framework components without the actual framework? I just need the
Possible Duplicate: java - How would I dynamically add swing component to gui on
Possible Duplicate: What Web Application Framework for Delphi is recommended? We have a Delphi
Possible Duplicate: Adding multiple JProgressBar to TableColumn of JTable i have a jTable with
Possible Duplicate: When and why should I implement IComponent, IContainer, and ISite? Every time
Possible Duplicate: Python: Finding corresponding indices for an intersection of two lists I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.