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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:17:29+00:00 2026-05-28T17:17:29+00:00

I have a Swing application in which I have multiple windows with different goals

  • 0

I have a Swing application in which I have multiple windows with different goals but they all have 2 things in common:

  • They use a specific object which is business logic related
  • They have to update some widgets with the specific object’s stuff

So, in order to avoid redundancy of code, I wanted to share some of those routines.
Maybe an example can be more clear:

public class WindowA {
     private JLabel labelA;
     // ...
     private void updateLabelInACertainManner() {
          labelA.setText(this.specificObject.getText());
     }
}

public class WindowB {
     private JLabel labelB;
     // ...
     private void updateLabelInACertainManner() {
          labelA.setText(this.specificObject.getText());
     }
}

How can I share updateLabelInACertainManner(), knowing that specificObject references the same object in both classes ?

I was thinking about inheriting both WindowA and WindowB from a WindowRoot which contains the method but how to do so having labelA and labelB are not the same object, not necessarily created in same way?

  • 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-28T17:17:30+00:00Added an answer on May 28, 2026 at 5:17 pm

    You could pass the label whose text should be changed as a parameter to the method in the superclass:

    public class SuperWindow {
        protected void updateLabel(JLabel label) {
           label.setText("foo");
        }
    }
    
    public class WindowA extends SuperWindow {
        private JLabel labelA;
        //...
    
        private void somethingHappened() {
          updateLabel(labelA);
        }
    }
    

    Or you could write a getter for the label in the specific subclasses:

    public class SuperWindow {
       protected abstract JLabel getLabel();
    
       protected void updateLabel() {
         getLabel().setText("foo");
       }
    }
    
    public class WindowA extends SuperWindow {
       private JLabel labelA;
       //...
    
       @Override
       protected JLabel getLabel() {
          return labelA;
       }
    
       private void somethingHappened() {
          updateLabel();
       }
    }
    

    And the same for specificObject.

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

Sidebar

Related Questions

I have a small image processing application which does multiple things at once using
We have a swing application which displays a lot of rectangles. We use Rectangle2D.double
I have made a swings application but there is one problem in that which
I have developed a Java Swing application, which uses the SwingWorker class to perform
I have created a swing application as bellow which shows main tasks in tabs
I have an application which uses Hibernate for database transactions, I have mapped all
I have a statusbar in a Swing application that continuously prints status. All objects
I have Swing application which manipulate shapes. In my toolbar I have a zoom
I have swing application in which there is MS excel like functionality. User can
I have a swing application which stores a list of objects. When the users

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.