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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:26:39+00:00 2026-06-09T22:26:39+00:00

I’m making a custom button that is made up of two other buttons. It

  • 0

I’m making a custom button that is made up of two other buttons. It has different behavior from a normal button (hence why it’s custom) and looks like this

My Button Working http://i1078.photobucket.com/albums/w491/evesforeva/Screenshot2012-07-28at21932AM.png

I would like to change the way icons are set in my button so that they apply to the button on the right (the one with the square). Currently, I am overriding AbstractButton.getIcon and AbstractButton.setIcon so that they apply to the right button, rather than the whole button. So my code looks like this

/* (non-Javadoc)
 * @see javax.swing.AbstractButton#getIcon()
 */
@Override
public Icon getIcon() {
    return popupButton.getIcon();  // popupButton = the right button
}

/* (non-Javadoc)
 * @see javax.swing.AbstractButton#setIcon(javax.swing.Icon)
 */
@Override
public void setIcon(Icon icon) {
    Icon oldValue = getIcon();
    firePropertyChange(ICON_CHANGED_PROPERTY, oldValue, icon);
    popupButton.setIcon(icon);
}

When I try to set my button icons, the button ends up looking like this

My Button Broken http://i1078.photobucket.com/albums/w491/evesforeva/Screenshot2012-08-19at42508PM.png

where the icon is duplicated in the middle of the whole button. I tried removing firePropertyChange, but that didn’t work.

Also here is the code I used to produce the two screenshots

public class MyButtonDemo implements Runnable {

public static class DemoPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    public DemoPanel() {
        MyButton myButton = new MyButton("My Button");
        Icon icon = new ImageIcon(new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB));
        myButton.setIcon(icon);

        add(myButton);
        add(new JButton("Normal Button"));
    }

}

/* (non-Javadoc)
 * @see java.lang.Runnable#run()
 */
public void run() {
    LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
    try {
        UIManager.setLookAndFeel(lafs[2].getClassName());
    } catch (Exception e) { }

    JFrame frame = new JFrame();
    frame.setContentPane(new DemoPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new MyButtonDemo());
}

}

What is making my button display the icon for the whole button instead of just the right button? And how do I work around it?

  • 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-09T22:26:40+00:00Added an answer on June 9, 2026 at 10:26 pm

    As to the why: you are overriding a method of the containing button such that the containing button thinks it has an icon – consequently, its uidelegate paints it where it deems appropriate 🙂

    As to how-to prevent that: depends a bit on how much work you want to put into it.

    • The clean one is to implement a custom ui-delegate and implement that for all LAFs you want to support. You might want to have a look at SwingX, its plaf module takes off a bit of the pain as demonstrated in a recent thread.
    • The simpler one is to not re-use the containing button’s api: implement the setIcon to do nothing and add new methods for setting the popup icon.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into

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.