How to add an ActionListener to customize the component

I created a component and want to add ActionListener

to customize the component.
I want to create a component like JButton

and show an actionPerformed

event in Events

Dialog in design view.

JBaseComponent

the 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) {

}
}

      

+3


source to share


3 answers


If you really can't use JButton

to do this, your next best option expands 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();
            }
        });
    }
}

      

+1


source


This thread can help you understand the purpose ActionListener

with samples of how to use it.



+1


source


Implement the interface ActionListener

and what you need to do is delegate the listener _button

.

+1


source







All Articles