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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:55:51+00:00 2026-05-14T02:55:51+00:00

When developing Swing applications, I’ve typically defined a delegate interface for each UI component

  • 0

When developing Swing applications, I’ve typically defined a delegate interface for each UI component for action callbacks. For example, if there is a class, MyDialog, which contains a button, MyButton, then the ActionListener for MyButton will call MyDialog.Delegate.OnMyButtonClick(Event e). The UI component then becomes “dumb” and requires a controller to handle events as well as update the component itself.

I thought that by using the Swing Application Framework’s @Actions, I could get around creating delegate interfaces and implementations by defining @Action methods in implementation classes, letting the ApplicationContext figure out what to call. Apparently, that is not the case, as I don’t see any clear way of adding those classes into the ApplicationContext, nor do I see any examples out there of doing such a thing.

Has anyone managed to use SAF in this manner so that there is a clean separation between UI and UI action code?

  • 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-14T02:55:51+00:00Added an answer on May 14, 2026 at 2:55 am

    I’ve discovered a good way to keep the UI separate from the behavior using @Actions.

    First, create a UI Component, say a JPanel with a button and then give it a public method that can be used to set the action of the Button:

    class CustomJPanel extends JPanel {
       private JButton myButton;
       public CustomJPanel() {
          initializeComponents();
       }
       public void initializeComponents() {
          myButton = new JButton();
       }
       public void setButtonAction(javax.swing.Action action)
       {
          myButton.setAction(action);
       }
    }
    

    Next, create an Action class that will provide the logic for that button:

    class CustomJPanelActions {
       @Action
       public void doSomething()
       {
           JOptionPane.showMessageDialog(null,"You pressed me!");
       }
    }
    

    Finally, setup the application controller and during setup, assign the appropriate action to the appropriate UI:

    class MyApp extends SingleFrameApplication {
       private JFrame mainFrame;
       private JLabel label;
    
       @Override
       protected void startup() {
          getMainFrame().setTitle("BasicSingleFrameApp");
          CustomJPanel panel = new CustomJPanel();
          panel.setButtonAction(getContext().getActionMap(new CustomJPanelActions()).get("doSomething");
          show(panel);
       }
    
       public static void main(String[] args) {
          Application.launch(BasicFrameworkApp.class, args);
       }
    }
    

    In this way, the UI is logically separated from the control (i.e. Action) and can be tested on its own. The controller can make any decisions it needs to in order to determine what Action set to use and which specific action to assign to the UI controls. That is, one can create a Test Action Set and a Live Action Set, etc.

    This method of using SAF has worked rather well for me.

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

Sidebar

Related Questions

I'm developing a utility class to handle Actions from Java Swing components; I would
I have been studying swing for developing gui applications since a few days. The
I have a server application and two Swing-based client applications. We're developing them in
I'm developing a console Java application(Not swing) using java.awt.Desktop class, which will launch the
I'm developing Swing application, and everything works fine usually. But I have an GUI
I'm developing a small swing application, and I'm not sure if I should use
Developing a website and just trying to get back into the swing of (clever)
So suppose I'm developing a chess-like program using Java's Swing. I added a MouseListener
I am developing a small project using Java Swing. I am using jdk 7
This problem is solved. I'm am developing a Java Swing based projected, and the

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.