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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:37:04+00:00 2026-06-10T05:37:04+00:00

I was making a simple text editor where you can set font style,font size,

  • 0

I was making a simple text editor where you can set font style,font size, clear all etc. To set font size I added JComboBox and implemented ItemListener. Here is my MainWindow class:

import javax.swing.*;

public class MainWindow extends JFrame{
Editor e = new Editor();

public MainWindow(){
    super(".:My Text Editor:.");
    getContentPane().add(e);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            new MainWindow();
        }
    });

}

}

Here is my Editor class:

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Editor extends JPanel{
JPanel optionPanel = new JPanel();
JTextArea editArea = new JTextArea();
JButton boldBtn = new JButton("Bold");
JButton italicBtn = new JButton("Italic");
JButton plainBtn = new JButton("Plain");
JButton clearBtn = new JButton("Clear all");
String [] fontSizes = {"10","11","12","13","14","15","16","17","18","19","20"};
int fontSize;
JComboBox combo = new JComboBox(fontSizes);

public Editor(){
    createUI();
    addEvents();
}

public void createUI(){
    optionPanel.add(boldBtn);
    optionPanel.add(italicBtn);
    optionPanel.add(plainBtn);
    optionPanel.add(combo);
    optionPanel.add(clearBtn);

    setLayout(new BorderLayout());
    add(optionPanel,BorderLayout.NORTH);
    add(new JScrollPane(editArea),BorderLayout.CENTER);
    setPreferredSize(new Dimension(640,480));
}

public void addEvents(){

    boldBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setFont(new Font("Sans Serif",Font.BOLD,fontSize));
        }
    });

    italicBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setFont(new Font("Sans Serif",Font.ITALIC,fontSize));

        }
    });

    plainBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
        }
    });

    combo.addItemListener(new ItemListener(){

        public void itemStateChanged(ItemEvent e){
            int ind = combo.getSelectedIndex();
            System.out.println(ind); 
            fontSize = Integer.parseInt(fontSizes[ind]);
            editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
        }
    });

    clearBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setText("");
        }
    });
}

}

Now, weird thing what happened is when I put System.out.println(ind); line just to see what index the getSelectedIndex() method returns me. Depending on which item I click, it returns me this:

1
1
0
0
2
2
3
3

Why is this happening? Shouldn’t return me just 1 0 2 3? Thanks in advance.

  • 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-10T05:37:05+00:00Added an answer on June 10, 2026 at 5:37 am

    JCombobox fire itemStateChanged twice for SELECTED and DESELECTED that you differentiate with ItemEvent.getStateChanged(). So wrap your code in an if like this:

    public void itemStateChanged( ItemEvent event ) {
        if( event.getStateChanged() == ItemEvent.SELECTED ) {
            // code here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a simple text and script editor with code highlighting. For that
In C# i am making a simple text editor with line numbers. I want
When making a simple WYSIWYG editor with JavaScript for a textarea I can check
I'm making a fairly simple text editor, and I have a question about my
Im making a simple programm for android, what can add, read and write text
I am making a simple text editor in wxpython. I would like it to
I'm making a very simple text editor and I'd like to color the background
I'm making a simple text editor, and I have a JTextPane and changed the
I am new to C++ and am making a simple text RPG, anyway, The
I am making a simple chat bot in Python. It has a text file

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.