Results 1 to 17 of 17
Thread: User Interface
- 10-22-2008, 01:00 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 7
- Rep Power
- 0
-
If you look up the API for the addActionListener (I think this can be found in the AbstractButton API), you'll see that it requires that you pass an object into it that implements the ActionListener interface, and this can be any object that implements this interface. In your current situation, the current object, the one whose code you are in, happens to implement this interface (look up at the top of the class and you'll see this). So when you pass "this" to this method, you are passing the current object into the button's action listener list.
Whenever this button is pressed, it iterates through its actionlistener list and calls the actionPerformed() method of each ActionListener object in this list. Since the current object is in that list (and in fact may be the only object in that list), whenever the button is pressed, the current object's actionPerformed() method will be called.
Is that clear as Mudd? Please reply if this doesn't make sense to you and we can try to break this down.
edit: a very simple example:
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class ActionListenerExample implements ActionListener { JButton myButton = new JButton("My Button"); public ActionListenerExample() { myButton.addActionListener(this); // now when "myButton" is pressed, "this"'s actionPerformed method will be called } // all classes that implement ActionListener *MUST* have this method public void actionPerformed(ActionEvent e) { System.out.println("Hello from the actionlistener!"); } public JButton getMyButton() { return myButton; } private static void createAndShowUI() { JFrame frame = new JFrame("example"); frame.getContentPane().add(new ActionListenerExample().getMyButton()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }Last edited by Fubarable; 10-22-2008 at 02:11 PM.
- 10-22-2008, 04:59 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 135
- Rep Power
- 0
Incidentally, the old addActionListener(this) routine often indicates a poor design: the "telephone switchboard" all-purpose action listener. Doubtless there's a single object handling all UI events, and constantly querying those events to see which widget spewed them out and acting accordingly. Quickly becomes a maintenance nightmare. A far better approach is to add an individual action listener to each widget, typically as an anonymous inner class
- 10-22-2008, 05:29 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 7
- Rep Power
- 0
"this"is to call another constructor in the same class for calling
- 10-22-2008, 05:41 PM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 135
- Rep Power
- 0
- 10-22-2008, 05:51 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 7
- Rep Power
- 0
sorry im new in java buti m trying
- 10-22-2008, 05:51 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 7
- Rep Power
- 0
im now learining java how long it will take to devlop a useful program if im everyday studying
- 10-22-2008, 06:05 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's depends on you lol, depends on your skills/ability to learn new things in short period of time. To build a useful application you should have a useful design. So the answer is in your hand.
Do you familiar with any programming language? It's really helpful to you. And object oriented programming too.
- 10-23-2008, 10:43 AM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 135
- Rep Power
- 0
- 10-23-2008, 11:29 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I agreed with you georgemc. Better to start work on with a complete design, a real one. So you can, you have to learn a lot. It's really valid.
- 10-23-2008, 12:01 PM #11
Senior Member
- Join Date
- Sep 2008
- Posts
- 135
- Rep Power
- 0
I needed to learn another language (not Java) quickly once, for a project I'd been dumped on in protest. I have a lot of CDs, and when friends came round at the weekend, we'd be constantly up and down changing CDs all the time. So I wrote a jukebox that held all my CDs as MP3s, and let us just scroll through all the CDs in my collection and choose random songs to put on a playlist, etc etc, via a numeric keypad.
Point is, because it was something I was actually interested in, I learnt a lot quicker than if I just plodded along doing generic exercises somebody else had set me, plus, I had something useful at the end of it
- 10-23-2008, 12:14 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Nice, lots of people worry about Hello World!
- 10-23-2008, 05:36 PM #13
Member
- Join Date
- Oct 2008
- Posts
- 7
- Rep Power
- 0
whts Book prefered for study?
which book do u prefer to learn java on it u recomment im reading now th ebook for study guide is it useful or not
- 10-23-2008, 07:28 PM #14
Senior Member
- Join Date
- Sep 2008
- Posts
- 135
- Rep Power
- 0
- 10-24-2008, 05:59 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I suggest you the Suns' tutorial. It's really helpful to me at the time I start work on Swing/AWT in Java.
Here is the the link. Start from the first page.
- 12-09-2008, 11:22 AM #16
Member
- Join Date
- Oct 2008
- Posts
- 7
- Rep Power
- 0
For Loop Question
1 for i:=1 to n do
2 for j:=1 to n/3 do
3 x:=x+1
How many times third line execute?
This is actually Psedocode.
- 12-09-2008, 02:37 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
J2ME User Interface Developer
By mobileapps in forum Jobs OfferedReplies: 0Last Post: 10-03-2008, 12:43 PM -
user interface development using JSP
By pradeep1_mca@yahoo.com in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 06-02-2008, 01:48 PM -
Help user interface
By carl in forum New To JavaReplies: 1Last Post: 07-31-2007, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks