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 7952935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:00:47+00:00 2026-06-04T03:00:47+00:00

Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue;

  • 0

Please have a look at the following code

import java.awt.Color;  
import java.awt.Dimension;  
import java.awt.EventQueue;  
import java.awt.FlowLayout;  
import java.util.ArrayList;  
import java.util.Scanner;  
import javax.swing.*;  
import javax.swing.event.DocumentEvent;  
import javax.swing.event.DocumentListener;  
import javax.swing.text.BadLocationException;  
import javax.swing.text.Style;  
import javax.swing.text.StyleConstants;  
import javax.swing.text.StyledDocument;  

public class Form extends JFrame  
{  
    private JTextPane textPane;  
    private JLabel results;  
    private JPanel center,south;  
    private FlowLayout flow;  
    private ArrayList array;  
    private Color color;  
    private StyledDocument doc;  
    private Style style, fontSize;  



    public Form()  
    {  
        textPane = new JTextPane();  
        textPane.setMinimumSize(new Dimension(100,100));  

        doc = textPane.getStyledDocument();  
        doc.addDocumentListener(new TextActions());  


        results = new JLabel("Number of letters: ");  

        array = new ArrayList();   
        array.add("public");
        array.add("static");
        array.add("void");
        array.add("private");
        array.add("protected");

        color = new Color(185,224,247);  

        //Adding styles  
        style = doc.addStyle("blue", null);    
        StyleConstants.setForeground(style, color);    


         fontSize = doc.addStyle("fontSize", null);
         StyleConstants.setFontSize(fontSize, 25);


         //Setting the font Size
         doc.setCharacterAttributes(0, doc.getLength(), fontSize, false);

        center = new JPanel();  
        flow = new FlowLayout();  

        center.setLayout(flow);  
        center.add(textPane);  

        south = new JPanel();  
        south.setLayout(new FlowLayout());  
        south.add(results);  

        getContentPane().add(textPane,"Center");  
        getContentPane().add(south,"South");  



    }  

    private class TextActions implements DocumentListener   
    {  
        @Override  
        public void insertUpdate(DocumentEvent e)   
        {  
            try {  
                highlighat();  
            } catch (BadLocationException ex) {  
               ex.printStackTrace();  
            } 
        }  

        @Override  
        public void removeUpdate(DocumentEvent e)  
        {  
            try {  
                highlighat();  
            } catch (BadLocationException ex) {  
               ex.printStackTrace();  
            }  
        }  

        @Override  
        public void changedUpdate(DocumentEvent e)  
        {  

        }  

    }  

      private void highlighat()  throws BadLocationException  
     {    

        EventQueue.invokeLater(new Runnable()    
        {    
            public void run()    
            {    
                String text = "";  
                String content = null;  
                try {  
                    content = doc.getText(0, doc.getLength()).toLowerCase();  
                } catch (BadLocationException ex) {  
                    ex.printStackTrace();  
                }  
            int last=0;  

            for(int i=0;i<array.size();i++)  
            {  
               text = array.get(i).toString();  

               if(content.contains(text))  
               {  
                    while((last=content.indexOf(text,last))!=-1)  
                    {                     

                       int end = last+text.length();  

                       doc.setCharacterAttributes(last, end, textPane.getStyle("blue"), true);    

                       last++;  

                     }  

               }  
            }  
            }    
        }  
     );  
    }  

    public static void main(String[]args)  
    {  
        try  
        {  
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
        }  
        catch(Exception e)  
        {  

        }  
        Form f = new Form();  
        f.setVisible(true);  
        f.setSize(800,600);  

        f.validate();  
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }  
}  

In there, I am also trying to set the font size to 25, but as you can see, it is not working. “textPane.setFont()” also didn’t work. How can I set the font size correctly? Please help..

  • 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-04T03:00:48+00:00Added an answer on June 4, 2026 at 3:00 am

    Sure, you can create a font object and use it to set the font of your text pane.
    Instantiate it like this:

        Font f = new Font(Font.SANS_SERIF, 3, 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout;
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code. import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*;
Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
am new here. i have a slight problem; PLease look at the following code
Please have a look at the code below: import string from collections import defaultdict
Please have a look at the following code, which I have run in VB6
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];

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.