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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:24:34+00:00 2026-06-06T21:24:34+00:00

I have two classes: JDialog and JFrame (see below). Now I want them to

  • 0

I have two classes: JDialog and JFrame (see below). Now I want them to get some extra functionality, but I want the two classes both to extend my code, without writing the code twice. Is there a way to do that?

java.awt.Window » java.awt.Dialog » javax.swing.JDialog  
java.awt.Window » java.awt.Frame » javax.swing.JFrame

(PS: Actually, I want to “rewrite” the source code of java.awt.Window, adding extra functionality. I know that’s not possible, but that would be a “solution”, because if I do so, both JDialog and JFrame would extend my brand new methods.)

Example:
Suppose I want to add the method moveWindowToMagicPositionOnScreen(), moving the window to a user-friendly position on the screen—I know, it’s just an example. I want the method to apply to both JDialog and JFrame. Using inheritance, I must write two classes, for example MagicJDialog and MagicJFrame, each implementing the method moveWindowToMagicPositionOnScreen() in exactly the same manner. Well, that’s pretty redundant. So I don’t want to have the code written twice. In some way, I want one class to use the code of the other 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-06-06T21:24:36+00:00Added an answer on June 6, 2026 at 9:24 pm

    The interface / delegate / composition ideas are probably the “cleanest”. But an acceptable and arguably simpler alternative is to make a utility class with a static method.

    public static moveWindowToMagicPositionOnScreen(Window window) {
      // calculate new x and y
      window.setLocation(x, y);  // or perhaps setBounds()...
    }
    

    In practice, I’d probably put the calculation logic into another method, computeMagicPositionOnScreen(). If it makes sense (it may not) this static utility class could actually be a “real” (non-static) class, say MagicPositionCalculator, that might have multiple instances, say one per each screen, one for really large screens, one for power-users, etc…

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

Sidebar

Related Questions

I have two classes in a many-to-many relationship, but only one of them have
I have two classes below: public class Module { public int Id { get;
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, Item and SoldItem . SoldItem inherits from Item adding some
I have two classes Order and Items I want a method like this class
I have two classes Teacher and Student . I am trying to get teacher
I have two classes: class Player { public string Id { set; get; }
I have two classes, Progression and FibonacciProgression, however FibonacciProgression print out some unexpected results
I have two classes, a server and some clients. The server class make a
I have two classes and want to build a collection that contains a collection.

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.