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

  • Home
  • SEARCH
  • 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 500405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:05:01+00:00 2026-05-13T06:05:01+00:00

I am making a user interface which shows graphs and manipulates graphs. The class

  • 0

I am making a user interface which shows graphs and manipulates graphs. The class extends JFrame implements ActionListener. The ActionListener then calls different classes to manipulate graphs depending on the action. This worked while the class had few ActionListeners; however, now the class is becoming unmanageable.

I know that in the interest of encapsulation, it would be best to have the ActionListener within the user interface class because it needs to access non-static components of the interface. However, it seems like there is a conflict between encapsulation and readability.

What I am proposing is breaking the class into one class for the interface and a second for the ActionListener and accessing the interface components statically. What I want to know is does this follow basic design conventions? And, if this is an acceptable approach would you place the main class in the user-interface class or the ActionListener class?

  • 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-13T06:05:02+00:00Added an answer on May 13, 2026 at 6:05 am

    Not a duplicate question… but my answer should help with your question.

    Short summery, my preference would be to have the JFrame class not implement ActionListener and then have a number of named inner classes withing the JFrame that do implement the ActionListener.

    I would place the main in a class unto itself… and call it Main.

    Here is some sample code for the way I like to do it:

    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    
    public class Main
    {
        private Main()
        {
        }
    
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI()
        {
            final FooFrame frame;
    
            frame = new FooFrame();
            frame.setupGUI();
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    

    and then the GUI:

    import java.awt.FlowLayout;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    
    public class FooFrame
        extends JFrame
    {
        private final JButton incrementBtn;
        private final JButton decrementBtn;
        private int value;
    
        {
            incrementBtn = new JButton("++");
            decrementBtn = new JButton("--");
        }
    
        private class IncrementListener
            implements ActionListener
        {
    
            public void actionPerformed(final ActionEvent evt)
            {
                increment();
            }
    
        }
    
        private class DecrementListener
            implements ActionListener
        {
    
            public void actionPerformed(final ActionEvent evt)
            {
                decrement();
            }
    
        }
    
        public void setupGUI()
        {
            final LayoutManager layout;
    
            layout = new FlowLayout();
            setLayout(layout);
            setupListeners();
            addComponents();
        }
    
        private void setupListeners()
        {
            incrementBtn.addActionListener(new IncrementListener());
            decrementBtn.addActionListener(new DecrementListener());
        }
    
        private void addComponents()
        {
            add(incrementBtn);
            add(decrementBtn);
        }
    
        private void increment()
        {
            value++;
            System.out.println("value = " + value);
        }
    
        private void decrement()
        {
            value--;
            System.out.println("value = " + value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have a look at BindingFlags. Try to set at least… May 15, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer Solution is: describe 'tasks/_form' do attr_accessor :output_buffer before do task… May 15, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer Although this is Duff's Device, this version is not about… May 15, 2026 at 6:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.