0% found this document useful (0 votes)
10 views3 pages

Java ActionListener Button Example

The document shows an example of using an ActionListener in Java to add interactivity to a button. When the button is clicked, it will set the text of a text field to "Welcome to Javatpoint." It imports necessary libraries, creates a frame with a button and text field, adds an anonymous ActionListener to the button to update the text field on click, adds the components to the frame, sets frame properties, and makes the frame visible.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Java ActionListener Button Example

The document shows an example of using an ActionListener in Java to add interactivity to a button. When the button is clicked, it will set the text of a text field to "Welcome to Javatpoint." It imports necessary libraries, creates a frame with a button and text field, adds an anonymous ActionListener to the button to update the text field on click, adds the components to the frame, sets frame properties, and makes the frame visible.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java ActionListener Example: On Button click

1. import [Link].*;
2. import [Link].*;
3. public class ActionListenerExample {
4. public static void main(String[] args) {
5. Frame f=new Frame("ActionListener Example");
6. final TextField tf=new TextField();
7. [Link](50,50, 150,20);
8. Button b=new Button("Click Here");
9. [Link](50,100,60,30);
10.
11. [Link](new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. [Link]("Welcome to Javatpoint.");
14. }
15. });
16. [Link](b);[Link](tf);
17. [Link](400,400);
18. [Link](null);
19. [Link](true);
20. }
21. }

Output:
Java ItemListener Example
1. import [Link].*;
2. import [Link].*;
3. public class ItemListenerExample implements ItemListener{
4. Checkbox checkBox1,checkBox2;
5. Label label;
6. ItemListenerExample(){
7. Frame f= new Frame("CheckBox Example");
8. label = new Label();
9. [Link]([Link]);
10. [Link](400,100);
11. checkBox1 = new Checkbox("C++");
12. [Link](100,100, 50,50);
13. checkBox2 = new Checkbox("Java");
14. [Link](100,150, 50,50);
15. [Link](checkBox1); [Link](checkBox2); [Link](label);
16. [Link](this);
17. [Link](this);
18. [Link](400,400);
19. [Link](null);
20. [Link](true);
21. }
22. public void itemStateChanged(ItemEvent e) {
23. if([Link]()==checkBox1)
24. [Link]("C++ Checkbox: "
25. + ([Link]()==1?"checked":"unchecked"));
26. if([Link]()==checkBox2)
27. [Link]("Java Checkbox: "
28. + ([Link]()==1?"checked":"unchecked"));
29. }
30. public static void main(String args[])
31. {
32. new ItemListenerExample();
33. }
34. }

Output:

You might also like