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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:41:13+00:00 2026-05-16T22:41:13+00:00

I have code similar to the following: public class myButton extends JButton() { public

  • 0

I have code similar to the following:

public class myButton extends JButton()
{
    public int data;
    public myButton(){
        super("asdf");
        data = 2;
    }
}

public class myPanel extends MouseListener()
{
    myButton myButtonVar1;
    myButton myButtonVar2;
    public myPanel()
    {
        myButtonVar1 = new myButton();
        myPanel.add(myButtonVar1);
        myButtonVar1.addMouseListener(this);
        myButtonVar2 = new myButton();
        myPanel.add(myButtonVar2);
        myButtonVar2.addMouseListener(this);
    }

    //MouseListener Methods are here
    void mouseClicked(MouseEvent e)
    {
        //say this changes the myButton that was clicked data based off
        //the other myButton's data
        doSomething((myButton)(e.getSource()).data);
    }
}

And then I add that panel to a JFrame via setContentPane.

This works alright. The handler has to have access to all of the buttons, because it needs to know myButtonVar1.data and myButtonVar2.data

This setup doesn’t sit right with me, but the main problem is that I have to have other buttons in the frame as well, which access the myButtons.

So how could I clean this up such that I could add something a “resetButton” that would reset all of the myButtons contained in myPanel. The route that stands out to me would be to use instanceof to see if the source is a resetButton or myButton, but that seems to be strongly discouraged in everything i’ve seen.

I hope I’m not too far off here. My goal is to write good code rather than stick with what I have, so let me know if I have done something fundamentally wrong already and should backtrack.

  • 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-16T22:41:13+00:00Added an answer on May 16, 2026 at 10:41 pm

    Classes should start with an uppercase letter (MyButton instead of myButton). This is a convention.

    Fields are usually private, and you only have a getter (myButton.data).

    instanceof is seldom required. Instead, you could define a base class with a “press” method. Both classes could then implement it.

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

Sidebar

Related Questions

I have code similar to this in my application: class A { public: int
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have ASP.Net code similar to the following (this is inside a FIELDSET): <ol>
We have similar code to the following in one of our projects. Can anyone
I have a HTML file that has code similar to the following. <table> <tr>
I've got code similar to the following... <p><label>Do you have buffet facilities?</label> <asp:RadioButtonList ID=blnBuffetMealFacilities:chk
I have the following JQuery code which does similar functionality like Stackoverflow where the
I have code similar to this filtering entries in an Array of Objects: var
There is probably is simple fix for this but I currently have code similar
In particular, would it be possible to have code similar to this c++ code

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.