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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:40:10+00:00 2026-05-14T06:40:10+00:00

Before you guys ask, yes I’ve searched online for the answer, but everything I

  • 0

Before you guys ask, yes I’ve searched online for the answer, but everything I found just leaves a lil confuse and nothing is recent so asking in those forums won’t really help.

My problems is this:

I have an array that holds my name for a menu.

String[] fontColor = new String[] {"Red", "Blue", "Green"};

for (int i = 0; i < fontColors.length; i++) {
     JMenuItem fontC = new JMenuItem(fontColors[i]);
     fontC.addActionListener(new fontColorAction());
     changeFontColor.add(fontC);
}

Then I have an array that holds my color change in a class called fontColorAction

in that class I have another array that does the same thing as my string array except all thats in the statement is textarea.setForeground(colorArr[i]);

that will set the setForeground() in order, but now how do I successfully attact the action listner in my class to my menuItems?

my class looks like this

private class fontColorAction implements ActionListener {
     Color[] colorArr - new Color[] {"Color.RED","Color.BLUE","Color.GREEN"};
     public void actionPerformed(ActionEvent e){
          for(i = 0; i < collorArr.length; i++){
               textarea.setForeground(colorArr[i]);
          }
     }
}
  • 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-05-14T06:40:11+00:00Added an answer on May 14, 2026 at 6:40 am

    You can add a color attribute to your action:

    private class FontColorAction implements ActionListener {
         private Color color;
         public FontColorAction (Color color){
              this.color = color;
         }
    
         public void actionPerformed(ActionEvent e){
              textarea.setForeground(color);
         }
    }
    

    And initialize the actions like this:

     String[] fontColor = new String[] {"Red", "Blue", "Green"};
     Color[] colorArr - new Color[] {"Color.RED","Color.BLUE","Color.GREEN"};
    
    for (int i = 0; i < fontColors.length; i++) {
         JMenuItem fontC = new JMenuItem(fontColors[i]);
         fontC.addActionListener(new fontColorAction(colorArr [i));
         changeFontColor.add(fontC);
    }
    

    Also, by convention java classes have their first letter in upper case 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, before you guys go nuts -- this is just a small site, temporary
Ok guys, I did ask before on how to create a way to run
Guys I searched around like hell but nothing could help me so I think
I've not done any thread programming before, so i wanted to ask you guys
Hi guys i have posted a similar post before, but that is for another,
Ok guys, three questions here. They are all pretty noobish, but I just want
Hey guys I have an admin page that checks if you are admin before
(Before I start, yes I have asked a similar question before; unfortunately due to
Before I ask my question, I want to mention that I am only today
My question is very simple. I will ask it first in case the answer

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.