N
N
nano_e_t_42016-11-11 18:25:56
Java
nano_e_t_4, 2016-11-11 18:25:56

How to set a class as an argument?

Hello everyone
Tell me, who knows how to submit a class as an argument? Probably not quite correctly expressed the question, the example below explains:
I have a method that changes the listener for buttons:

void changeListeners(ArrayList<JButton> buttons, Object className) {
        ActionListener[] listeners;
        for (JButton btn:buttons) {
            listeners = btn.getActionListeners();
            for (ActionListener l:listeners) {
                btn.removeActionListener(l);
            }
            btn.addActionListener(new className.getClass()); //знаю что такая конструкция может все сломать
        }
    }


but since the new listeners are different, then, accordingly, you need to give the argument to the class that will be the listener.
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2016-11-11
@petermzg

so easy to find

this.getClass().getClassLoader().loadClass("package.classname").newInstance();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question