|
Author |
Topic: The java button question (again) (Read 1366 times) |
|
st33d
|
The java button question (again)
« on: Dec 14th, 2004, 9:48pm » |
|
Searched for this and found an unanswered question. I've gotten this far: Code: Button sub = new Button("submit"); void setup(){ add(sub); //sub.addMouseListener(this); <-- allowed but I've no idea how to activate it //sub.addActionListener(this); <-- doesn't work funnily } void loop(){ println("argh!"); } void subFunc(){ println("button pressed"); } |
| The Java examples I look at keep structuring the stuff like I'm supposed to call a class instead of a method. Which doesn't make sense to my linear mind. Am I supposed to stick my method in the class constructor? I ask simply because I'm creating an applet with textfield and a submit button and I think it would be a bit heavy handed to write an overRect fuction and load in text and draw the submit button when there's one to hand (that I can't figure out unfortunately). How do I call the subFunc method with the sub (button) object?
|
I could murder a pint.
|
|
|
toxi
|
Re: The java button question (again)
« Reply #1 on: Dec 15th, 2004, 1:06pm » |
|
what you came accross here, is one of the core design features of java: Interfaces are java's clean answer to multiple inheritance and ensure code stability. in order to add your sketch as "ActionListener" to the button, it'll need to "implement" certain functions which are defined by the ActionListener interface. as you don't have direct access to the generated java version of your sketch, and the ActionListner interface is not implemented by default, the only way around is to create a helper class: Code:Button sub = new Button("submit"); void setup(){ add(sub); sub.addActionListener(new ButtonListener()); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { println(e.getActionCommand()); } } |
| Processing as tool itself is not promoting any of these Java and OOP specific features directly and that is part of its strong appeal. it really wasn't meant to work as AWT (java's GUI toolkit) container (although it can of course). so if you want to go down that route further, you'll soon find yourself dealing with a lot more with Java problems. for me the beauty of Processing is being able to explore new interface approaches which can't be done as easily with the standard java toolkit and *not* to build standard java applications.
|
http://toxi.co.uk/
|
|
|
st33d
|
Re: The java button question (again)
« Reply #2 on: Dec 15th, 2004, 1:20pm » |
|
Thanks a lot. I understand the value of making my own GUI, but sometimes you just want something quick and dirty whilst you're testing code. I don't think I'll have any great need for Java gubbinz but it's nice that one can reach in and grab some useful stuff at times (like String methods - invaluable!).
|
I could murder a pint.
|
|
|
|