0% found this document useful (0 votes)
46 views

Java Actionlistener Example: On Button Click

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)
46 views

Java Actionlistener Example: On Button Click

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
You are on page 1/ 3

Java ActionListener Example: On Button click

1. import java.awt.*;
2. import java.awt.event.*;
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. tf.setBounds(50,50, 150,20);
8. Button b=new Button("Click Here");
9. b.setBounds(50,100,60,30);
10.
11. b.addActionListener(new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. tf.setText("Welcome to Javatpoint.");
14. }
15. });
16. f.add(b);f.add(tf);
17. f.setSize(400,400);
18. f.setLayout(null);
19. f.setVisible(true);
20. }
21. }

Output:
Java ItemListener Example
1. import java.awt.*;
2. import java.awt.event.*;
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. label.setAlignment(Label.CENTER);
10. label.setSize(400,100);
11. checkBox1 = new Checkbox("C++");
12. checkBox1.setBounds(100,100, 50,50);
13. checkBox2 = new Checkbox("Java");
14. checkBox2.setBounds(100,150, 50,50);
15. f.add(checkBox1); f.add(checkBox2); f.add(label);
16. checkBox1.addItemListener(this);
17. checkBox2.addItemListener(this);
18. f.setSize(400,400);
19. f.setLayout(null);
20. f.setVisible(true);
21. }
22. public void itemStateChanged(ItemEvent e) {
23. if(e.getSource()==checkBox1)
24. label.setText("C++ Checkbox: "
25. + (e.getStateChange()==1?"checked":"unchecked"));
26. if(e.getSource()==checkBox2)
27. label.setText("Java Checkbox: "
28. + (e.getStateChange()==1?"checked":"unchecked"));
29. }
30. public static void main(String args[])
31. {
32. new ItemListenerExample();
33. }
34. }

Output:

You might also like