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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:37:23+00:00 2026-06-14T12:37:23+00:00

Some time ago I wrote a little image viewer/processing program with Java, a mini-Photoshop,

  • 0

Some time ago I wrote a little image viewer/processing program with Java, a mini-Photoshop, if you will.

I wanted there to be a drop-down menu where I could select which one of the images I have opened would be “on the table”, ie. shown and methods applied to. I wanted the name of the image to be the name of the JMenuItem shown in the menu. I also wanted a new button to appear when I add a new image.

I wondered this for some time and finally produced this solution, a new class that handles the creation of the new button when an image is added. The code is as follows:

import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class ImageList{

    private ArrayList<JMenuItem> list;
    private ImageHandler main;
    private ImageLevel img;

    public ImageList() {}

    public void setHandler(ImageHandler hand) {
        main = hand;
        img = main.getImg1();
    }

    public void add(Buffer addi) {
        final String added = addi.getName();
        JMenuItem uusi = new JMenuItem(added);

        main.getMenu5().add(uusi);
        uusi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                img.setBuffer(added);
                main.getScr().updateUI();
            }
        });
   }
}

This works as it should. For this site I translated the original Finnish names to English, wonder why I wrote them in Finnish originally…I suck at naming things.

Method add is supposed to be called multiple times while the program is running.

What I cannot understand really is the inner class implementation of the interface ActionListener, namely its compilation and how it works.

If I have two buttons on my interface and I want them to do different things, I need two inner classes, one for each, each having its own inner implementation of the interface ActionListener. But in my code there is one class that seems to do the work of many, one complied .class-file for it, but the final result works as if there were many.

Can someone educate me on this issue? Is this code here one class and new buttons are instances of it? Are they new classes? Should there be a new .class-file for each new button? etc…

  • 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-14T12:37:25+00:00Added an answer on June 14, 2026 at 12:37 pm

    Often, an inner class is instantiated in a code which is only called once (e.g. when you extend JPanel and add ActionListeners to JButtons in the constructor). Here, you instantiate an inner class in a method which you call several times (if I understand your description correctly). Each time you call add(), a new instance of the inner class will be created. As with named classes, there is only one class, but there may be many instances.

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

Sidebar

Related Questions

I wrote a program some time ago (Mac OS X, C++, SDL, FMOD) and
Some time ago I wrote a little piece of code to ask about on
Some time ago, I wrote some code to decide which method of updating mutable
Some time ago I created this LAMP based web. At the time I wrote
I've got a function I wrote quite some time ago that works fine, but
Some time ago (~4-5months ago) I attented a lecture about Java EE and at
Some time ago, i wrote some bash scripts for my school. I thought it
Some time ago i Wrote a service with a timer that make an action
So I'm working with some older code I wrote some time ago. I was
I was tidying up some code I wrote a long time ago, putting it

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.