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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:35:13+00:00 2026-06-17T13:35:13+00:00

I created a component and want to add ActionListener to customize component. I want

  • 0

I created a component and want to add ActionListener to customize component.
I want to create a component like JButton and show actionPerformed event in Events Dialog on design view.

JBaseComponent class will be derived from JComponent.

Code:

public class JCButton extends JBaseComponent implements ActionListener,ItemSelectable,ChangeListener{

private JButton _button=new JButton();
private ActionEvent _actevent=new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"");

public JCButton() {
    super();
    _button.setPreferredSize(new Dimension(105, 25));
    _button.setFocusTraversalKeysEnabled(false);

     _button.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
    });

    _button.setTransferHandler(new TransferHandler("btn"));

    setColorFocus(false);

    _button.addActionListener(this);
    actionPerformed(_actevent);
    addActionListener(this);
   revalidate();
   repaint();
    add(_button);
}

public void setText(String txt){
    _button.setText(txt);
}

public void setIcon(Icon ico){
    _button.setIcon(ico);
}

public void addActionListener(ActionListener act){
    _button.addActionListener(act);
}

@Override
public void actionPerformed(ActionEvent e) {
}

@Override
public Object[] getSelectedObjects() {
    return null;
}

@Override
public void addItemListener(ItemListener l) {

}

@Override
public void removeItemListener(ItemListener l) {

}

@Override
public void stateChanged(ChangeEvent e) {

}
}
  • 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-17T13:35:14+00:00Added an answer on June 17, 2026 at 1:35 pm

    If you really can’t use a JButton for this, your next best option is extending AbstractButton, which includes the EventListenerList plumbing mentioned here. For example,

    image

    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.AbstractButton;
    import javax.swing.Action;
    import javax.swing.DefaultButtonModel;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.plaf.ButtonUI;
    
    /** @see https://stackoverflow.com/a/14429304/230513 */
    public class JCButtonTest {
    
        private void display() {
            JFrame f = new JFrame("JCButtonTest");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JCButton(new AbstractAction("JCButton") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(e);
                }
            }));
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        private static class JCButton extends AbstractButton {
    
            public static final int SIZE = 32;
    
            public JCButton(Action action) {
                this.setModel(new DefaultButtonModel());
                System.out.println(action.getValue(Action.NAME));
                this.init((String) action.getValue(Action.NAME), null);
                this.addActionListener(action);
            }
    
            @Override
            public void updateUI() {
                setUI((ButtonUI) UIManager.getUI(this));
            }
    
            @Override
            public String getUIClassID() {
                return "ButtonUI";
            }
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new JCButtonTest().display();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a simple COM component in VC++ 2008. I have created
I created a compound component Box which I want to add to the layout.
I have created a COM Component in C++. Now i want to register my
I want to create a component by giving XML source input directly using core
I want to create my own component which consists two other panels. One of
I have a UIView with UITableView component and I want to create a class
I want to create a template for a component that is used many times
How do you create your own custom component for vb.net 2008? I want it
I have a Symfony2 form that I want to add a file upload dialog
I want to write test case for GWT composite component i created, I had

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.