Java GUI
Java GUI
Java GUI
Java AWT components are platform-dependent i.e. components are displayed according to
the view of operating system. AWT is heavyweight i.e. its components are using the
resources of OS.
Window
The window is the container that have no borders and menu bars. You must use frame,
dialog or another window for creating a window.
Panel
The Panel is the container that doesn't contain title bar and menu bars. It can have other
components like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.
Java AWT Example
To create simple awt example, you need a frame. There are two ways to create a frame in
AWT.
1. import java.awt.*;
2. class First extends Frame{
3. First(){
4. Button b=new Button("click me");
5. b.setBounds(30,100,80,30);// setting button position
6. add(b);//adding button into frame
7. setSize(300,300);//frame size 300 width and 300 height
8. setLayout(null);//no layout manager
9. setVisible(true);//now frame will be visible, by default not visible
10. }
11. public static void main(String args[]){
12. First f=new First();
13. }}
The setBounds(int xaxis, int yaxis, int width, int height) method is used in the above
example that sets the position of the awt button.
1. import java.awt.*;
2. class First2{
3. First2(){
4. Frame f=new Frame();
5. Button b=new Button("click me");
6. b.setBounds(30,50,80,30);
7. f.add(b);
8. f.setSize(300,300);
9. f.setLayout(null);
10. f.setVisible(true);
11. }
12. public static void main(String args[]){
13. First2 f=new First2();
14. }}
Registration Methods
For registering the component with the Listener, many classes provide the registration
methods. For example:
o Button
o public void addActionListener(ActionListener a){}
o MenuItem
o public void addActionListener(ActionListener a){}
o TextField
o public void addActionListener(ActionListener a){}
o public void addTextListener(TextListener a){}
o TextArea
o public void addTextListener(TextListener a){}
o Checkbox
o public void addItemListener(ItemListener a){}
o Choice
o public void addItemListener(ItemListener a){}
o List
o public void addActionListener(ActionListener a){}
o public void addItemListener(ItemListener a){}
1. Within class
2. Other class
3. Anonymous class
public void setBounds(int xaxis, int yaxis, int width, int height); have been used in
the above example that sets the position of the component it may be button, textfield etc.
2) Java event handling by outer class
1. import java.awt.*;
2. import java.awt.event.*;
3. class AEvent2 extends Frame{
4. TextField tf;
5. AEvent2(){
6. //create components
7. tf=new TextField();
8. tf.setBounds(60,50,170,20);
9. Button b=new Button("click me");
10. b.setBounds(100,120,80,30);
11. //register listener
12. Outer o=new Outer(this);
13. b.addActionListener(o);//passing outer class instance
14. //add components and set size, layout and visibility
15. add(b);add(tf);
16. setSize(300,300);
17. setLayout(null);
18. setVisible(true);
19. }
20. public static void main(String args[]){
21. new AEvent2();
22. }
23. }
1. import java.awt.event.*;
2. class Outer implements ActionListener{
3. AEvent2 obj;
4. Outer(AEvent2 obj){
5. this.obj=obj;
6. }
7. public void actionPerformed(ActionEvent e){
8. obj.tf.setText("welcome");
9. }
10. }
Output:
Java AWT Button Example with ActionListener
1. import java.awt.*;
2. import java.awt.event.*;
3. public class ButtonExample {
4. public static void main(String[] args) {
5. Frame f=new Frame("Button 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. b.addActionListener(new ActionListener(){
11. public void actionPerformed(ActionEvent e){
12. tf.setText("Welcome to Javatpoint.");
13. }
14. });
15. f.add(b);f.add(tf);
16. f.setSize(400,400);
17. f.setLayout(null);
18. f.setVisible(true);
19. }
20. }
Output:
Java AWT Label
The object of Label class is a component for placing text in a container. It is used to display
a single line of read only text. The text can be changed by an application but a user cannot
edit it directly.
Output:
Output:
Output:
Output:
Output:
Output:
Java AWT Checkbox
The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off
(false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to "on".
Output:
Java AWT CheckboxGroup
The object of CheckboxGroup class is used to group together a set of Checkbox. At a time
only one check box button is allowed to be in "on" state and remaining check box button in
"off" state. It inherits the object class.
Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special
control for creating radio buttons in AWT.
Output:
Java AWT CheckboxGroup Example with ItemListener
1. import java.awt.*;
2. import java.awt.event.*;
3. public class CheckboxGroupExample
4. {
5. CheckboxGroupExample(){
6. Frame f= new Frame("CheckboxGroup Example");
7. final Label label = new Label();
8. label.setAlignment(Label.CENTER);
9. label.setSize(400,100);
10. CheckboxGroup cbg = new CheckboxGroup();
11. Checkbox checkBox1 = new Checkbox("C++", cbg, false);
12. checkBox1.setBounds(100,100, 50,50);
13. Checkbox checkBox2 = new Checkbox("Java", cbg, false);
14. checkBox2.setBounds(100,150, 50,50);
15. f.add(checkBox1); f.add(checkBox2); f.add(label);
16. f.setSize(400,400);
17. f.setLayout(null);
18. f.setVisible(true);
19. checkBox1.addItemListener(new ItemListener() {
20. public void itemStateChanged(ItemEvent e) {
21. label.setText("C++ checkbox: Checked");
22. }
23. });
24. checkBox2.addItemListener(new ItemListener() {
25. public void itemStateChanged(ItemEvent e) {
26. label.setText("Java checkbox: Checked");
27. }
28. });
29. }
30. public static void main(String args[])
31. {
32. new CheckboxGroupExample();
33. }
34. }
Output:
Output:
Java AWT Choice Example with ActionListener
1. import java.awt.*;
2. import java.awt.event.*;
3. public class ChoiceExample
4. {
5. ChoiceExample(){
6. Frame f= new Frame();
7. final Label label = new Label();
8. label.setAlignment(Label.CENTER);
9. label.setSize(400,100);
10. Button b=new Button("Show");
11. b.setBounds(200,100,50,20);
12. final Choice c=new Choice();
13. c.setBounds(100,100, 75,75);
14. c.add("C");
15. c.add("C++");
16. c.add("Java");
17. c.add("PHP");
18. c.add("Android");
19. f.add(c);f.add(label); f.add(b);
20. f.setSize(400,400);
21. f.setLayout(null);
22. f.setVisible(true);
23. b.addActionListener(new ActionListener() {
24. public void actionPerformed(ActionEvent e) {
25. String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());
26. label.setText(data);
27. }
28. });
29. }
30. public static void main(String args[])
31. {
32. new ChoiceExample();
33. }
34. }
Output:
Java AWT List
The object of List class represents a list of text items. By the help of list, user can choose
either one item or multiple items. It inherits Component class.
Output:
Output:
Java AWT Canvas
The Canvas control represents a blank rectangular area where the application can draw or
trap input events from the user. It inherits the Component class.
Output:
Java AWT Scrollbar
The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is
a GUI component allows us to see invisible number of rows and columns.
Output:
Java AWT Scrollbar Example with AdjustmentListener
1. import java.awt.*;
2. import java.awt.event.*;
3. class ScrollbarExample{
4. ScrollbarExample(){
5. Frame f= new Frame("Scrollbar Example");
6. final Label label = new Label();
7. label.setAlignment(Label.CENTER);
8. label.setSize(400,100);
9. final Scrollbar s=new Scrollbar();
10. s.setBounds(100,100, 50,100);
11. f.add(s);f.add(label);
12. f.setSize(400,400);
13. f.setLayout(null);
14. f.setVisible(true);
15. s.addAdjustmentListener(new AdjustmentListener() {
16. public void adjustmentValueChanged(AdjustmentEvent e) {
17. label.setText("Vertical Scrollbar value is:"+ s.getValue());
18. }
19. });
20. }
21. public static void main(String args[]){
22. new ScrollbarExample();
23. }
24. }
Output:
The object of Menu class is a pull down menu component which is displayed on the menu
bar. It inherits the MenuItem class.
Output:
Java AWT PopupMenu
PopupMenu can be dynamically popped up at specific position within a component. It
inherits the Menu class.
Output:
Output:
Java AWT Dialog
The Dialog control represents a top level window with a border and a title used to take some
form of input from the user. It inherits the Window class.
Frame vs Dialog
Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons
but Dialog doesn't have.
Output:
Java AWT Toolkit
Toolkit class is the abstract superclass of every implementation in the Abstract Window
Toolkit. Subclasses of Toolkit are used to bind various components. It inherits Object class.
Output:
Screen resolution = 96
Screen width = 1366
Screen height = 768
Output:
Java AWT Toolkit Example: Change TitleBar Icon
1. import java.awt.*;
2. class ToolkitExample {
3. ToolkitExample(){
4. Frame f=new Frame();
5. Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
6. f.setIconImage(icon);
7. f.setLayout(null);
8. f.setSize(400,400);
9. f.setVisible(true);
10. }
11. public static void main(String args[]){
12. new ToolkitExample();
13. }
14. }
Output:
Java ActionListener Interface
The Java ActionListener is notified whenever you click on the button or menu item. It is
notified against ActionEvent. The ActionListener interface is found in
java.awt.event package. It has only one method: actionPerformed().
actionPerformed() method
The actionPerformed() method is invoked automatically whenever you click on the
registered component.
1. public abstract void actionPerformed(ActionEvent e);
1. public class ActionListenerExample Implements ActionListener
1. component.addActionListener(instanceOfListenerclass);
1. public void actionPerformed(ActionEvent e){
2. //Write the code here
3. }
Output:
Java ActionListener Example: Using Anonymous class
We can also use the anonymous class to implement the ActionListener. It is the shorthand
way, so you do not need to follow the 3 steps:
1. b.addActionListener(new ActionListener(){
2. public void actionPerformed(ActionEvent e){
3. tf.setText("Welcome to Javatpoint.");
4. }
5. });
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:
1. public abstract void mouseClicked(MouseEvent e);
2. public abstract void mouseEntered(MouseEvent e);
3. public abstract void mouseExited(MouseEvent e);
4. public abstract void mousePressed(MouseEvent e);
5. public abstract void mouseReleased(MouseEvent e);
Java MouseListener Example
1. import java.awt.*;
2. import java.awt.event.*;
3. public class MouseListenerExample extends Frame implements MouseListener{
4. Label l;
5. MouseListenerExample(){
6. addMouseListener(this);
7.
8. l=new Label();
9. l.setBounds(20,50,100,20);
10. add(l);
11. setSize(300,300);
12. setLayout(null);
13. setVisible(true);
14. }
15. public void mouseClicked(MouseEvent e) {
16. l.setText("Mouse Clicked");
17. }
18. public void mouseEntered(MouseEvent e) {
19. l.setText("Mouse Entered");
20. }
21. public void mouseExited(MouseEvent e) {
22. l.setText("Mouse Exited");
23. }
24. public void mousePressed(MouseEvent e) {
25. l.setText("Mouse Pressed");
26. }
27. public void mouseReleased(MouseEvent e) {
28. l.setText("Mouse Released");
29. }
30. public static void main(String[] args) {
31. new MouseListenerExample();
32. }
33. }
Output:
Java MouseListener Example 2
1. import java.awt.*;
2. import java.awt.event.*;
3. public class MouseListenerExample2 extends Frame implements MouseListener{
4. MouseListenerExample2(){
5. addMouseListener(this);
6.
7. setSize(300,300);
8. setLayout(null);
9. setVisible(true);
10. }
11. public void mouseClicked(MouseEvent e) {
12. Graphics g=getGraphics();
13. g.setColor(Color.BLUE);
14. g.fillOval(e.getX(),e.getY(),30,30);
15. }
16. public void mouseEntered(MouseEvent e) {}
17. public void mouseExited(MouseEvent e) {}
18. public void mousePressed(MouseEvent e) {}
19. public void mouseReleased(MouseEvent e) {}
20.
21. public static void main(String[] args) {
22. new MouseListenerExample2();
23. }
24. }
Output:
1. public abstract void mouseDragged(MouseEvent e);
2. public abstract void mouseMoved(MouseEvent e);
Output:
Java MouseMotionListener Example 2
1. import java.awt.*;
2. import java.awt.event.MouseEvent;
3. import java.awt.event.MouseMotionListener;
4. public class Paint extends Frame implements MouseMotionListener{
5. Label l;
6. Color c=Color.BLUE;
7. Paint(){
8. l=new Label();
9. l.setBounds(20,40,100,20);
10. add(l);
11.
12. addMouseMotionListener(this);
13.
14. setSize(400,400);
15. setLayout(null);
16. setVisible(true);
17. }
18. public void mouseDragged(MouseEvent e) {
19. l.setText("X="+e.getX()+", Y="+e.getY());
20. Graphics g=getGraphics();
21. g.setColor(Color.RED);
22. g.fillOval(e.getX(),e.getY(),20,20);
23. }
24. public void mouseMoved(MouseEvent e) {
25. l.setText("X="+e.getX()+", Y="+e.getY());
26. }
27. public static void main(String[] args) {
28. new Paint();
29. }
30. }
Output:
Java ItemListener Interface
The Java ItemListener is notified whenever you click on the checkbox. It is notified against
ItemEvent. The ItemListener interface is found in java.awt.event package. It has only one
method: itemStateChanged().
itemStateChanged() method
The itemStateChanged() method is invoked automatically whenever you click or unclick on
the registered checkbox component.
1. public abstract void itemStateChanged(ItemEvent e);
Output:
1. public abstract void keyPressed(KeyEvent e);
2. public abstract void keyReleased(KeyEvent e);
3. public abstract void keyTyped(KeyEvent e);
Output:
Output:
Java WindowListener Interface
The Java WindowListener is notified whenever you change the state of window. It is notified
against WindowEvent. The WindowListener interface is found in java.awt.event package. It
has three methods.
1. public abstract void windowActivated(WindowEvent e);
2. public abstract void windowClosed(WindowEvent e);
3. public abstract void windowClosing(WindowEvent e);
4. public abstract void windowDeactivated(WindowEvent e);
5. public abstract void windowDeiconified(WindowEvent e);
6. public abstract void windowIconified(WindowEvent e);
7. public abstract void windowOpened(WindowEvent e);
Output:
1. public abstract void windowActivated(WindowEvent e);
2. public abstract void windowClosed(WindowEvent e);
3. public abstract void windowClosing(WindowEvent e);
4. public abstract void windowDeactivated(WindowEvent e);
5. public abstract void windowDeiconified(WindowEvent e);
6. public abstract void windowIconified(WindowEvent e);
7. public abstract void windowOpened(WindowEvent e);
Output:
Java Adapter Classes
Java adapter classes provide the default implementation of listener interfaces. If you inherit
the adapter class, you will not be forced to provide the implementation of all the methods of
listener interfaces. So it saves code.
Output:
Output:
Output:
Output:
How to close AWT Window in Java
We can close the AWT Window or Frame by calling dispose() or System.exit() inside
windowClosing() method. The windowClosing() method is found
in WindowListener interface and WindowAdapter class.
If you implement the WindowListener interface, you will be forced to override all the 7
methods of WindowListener interface. So it is better to use WindowAdapter class.
o By anonymous class
o By inheriting WindowAdapter class
o By implementing WindowListener interface
Output:
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.
Hierarchy of Java Swing classes
The hierarchy of java swing API is given below.
Java Swing Examples
There are two ways to create a frame:
We can write the code of swing inside the main(), constructor or any other method.
File: FirstSwingExample.java
1. import javax.swing.*;
2. public class FirstSwingExample {
3. public static void main(String[] args) {
4. JFrame f=new JFrame();//creating instance of JFrame
5.
6. JButton b=new JButton("click");//creating instance of JButton
7. b.setBounds(130,100,100, 40);//x axis, y axis, width, height
8.
9. f.add(b);//adding button in JFrame
10.
11. f.setSize(400,500);//400 width and 500 height
12. f.setLayout(null);//using no layout managers
13. f.setVisible(true);//making the frame visible
14. }
15. }
Example of Swing by Association inside constructor
We can also write all the codes of creating JFrame, JButton and method call inside the java
constructor.
File: Simple.java
1. import javax.swing.*;
2. public class Simple {
3. JFrame f;
4. Simple(){
5. f=new JFrame();//creating instance of JFrame
6.
7. JButton b=new JButton("click");//creating instance of JButton
8. b.setBounds(130,100,100, 40);
9.
10. f.add(b);//adding button in JFrame
11.
12. f.setSize(400,500);//400 width and 500 height
13. f.setLayout(null);//using no layout managers
14. f.setVisible(true);//making the frame visible
15. }
16.
17. public static void main(String[] args) {
18. new Simple();
19. }
20. }
The setBounds(int xaxis, int yaxis, int width, int height)is used in the above example that
sets the position of the button.
File: Simple2.java
1. import javax.swing.*;
2. public class Simple2 extends JFrame{//inheriting JFrame
3. JFrame f;
4. Simple2(){
5. JButton b=new JButton("click");//create button
6. b.setBounds(130,100,100, 40);
7.
8. add(b);//adding button on frame
9. setSize(400,500);
10. setLayout(null);
11. setVisible(true);
12. }
13. public static void main(String[] args) {
14. new Simple2();
15. }}
Java JButton
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It inherits
AbstractButton class.
1. public class JButton extends AbstractButton implements Accessible
Output:
Output:
Output:
Java JLabel
The object of JLabel class is a component for placing text in a container. It is used to display
a single line of read only text. The text can be changed by an application but a user cannot
edit it directly. It inherits JComponent class.
public class JLabel extends JComponent implements SwingConstants, Accessible
Java JLabel Example
1. import javax.swing.*;
2. class LabelExample
3. {
4. public static void main(String args[])
5. {
6. JFrame f= new JFrame("Label Example");
7. JLabel l1,l2;
8. l1=new JLabel("First Label.");
9. l1.setBounds(50,50, 100,30);
10. l2=new JLabel("Second Label.");
11. l2.setBounds(50,100, 100,30);
12. f.add(l1); f.add(l2);
13. f.setSize(300,300);
14. f.setLayout(null);
15. f.setVisible(true);
16. }
17. }
Output:
Output:
Java JTextField
The object of a JTextField class is a text component that allows the editing of a single line
text. It inherits JTextComponent class.
1. public class JTextField extends JTextComponent implements SwingConstants
Output:
Output:
Java JTextArea
The object of a JTextArea class is a multi line region that displays text. It allows the editing
of multiple line text. It inherits JTextComponent class
public class JTextArea extends JTextComponent
Java JTextArea Example
1. import javax.swing.*;
2. public class TextAreaExample
3. {
4. TextAreaExample(){
5. JFrame f= new JFrame();
6. JTextArea area=new JTextArea("Welcome to javatpoint");
7. area.setBounds(10,30, 200,200);
8. f.add(area);
9. f.setSize(300,300);
10. f.setLayout(null);
11. f.setVisible(true);
12. }
13. public static void main(String args[])
14. {
15. new TextAreaExample();
16. }}
Output:
Output:
Java JPasswordField
The object of a JPasswordField class is a text component specialized for password entry. It
allows the editing of a single line of text. It inherits JTextField class.
1. public class JPasswordField extends JTextField
Java JPasswordField Example
1. import javax.swing.*;
2. public class PasswordFieldExample {
3. public static void main(String[] args) {
4. JFrame f=new JFrame("Password Field Example");
5. JPasswordField value = new JPasswordField();
6. JLabel l1=new JLabel("Password:");
7. l1.setBounds(20,100, 80,30);
8. value.setBounds(100,100,100,30);
9. f.add(value); f.add(l1);
10. f.setSize(300,300);
11. f.setLayout(null);
12. f.setVisible(true);
13. }
14. }
Output:
Java JPasswordField Example with ActionListener
1. import javax.swing.*;
2. import java.awt.event.*;
3. public class PasswordFieldExample {
4. public static void main(String[] args) {
5. JFrame f=new JFrame("Password Field Example");
6. final JLabel label = new JLabel();
7. label.setBounds(20,150, 200,50);
8. final JPasswordField value = new JPasswordField();
9. value.setBounds(100,75,100,30);
10. JLabel l1=new JLabel("Username:");
11. l1.setBounds(20,20, 80,30);
12. JLabel l2=new JLabel("Password:");
13. l2.setBounds(20,75, 80,30);
14. JButton b = new JButton("Login");
15. b.setBounds(100,120, 80,30);
16. final JTextField text = new JTextField();
17. text.setBounds(100,20, 100,30);
18. f.add(value); f.add(l1); f.add(label); f.add(l2); f.add(b); f.add(text);
19. f.setSize(300,300);
20. f.setLayout(null);
21. f.setVisible(true);
22. b.addActionListener(new ActionListener() {
23. public void actionPerformed(ActionEvent e) {
24. String data = "Username " + text.getText();
25. data += ", Password: "
26. + new String(value.getPassword());
27. label.setText(data);
28. }
29. });
30. }
31. }
Output:
Java JCheckBox
The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or
off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on
".It inherits JToggleButton class.
Output:
Output:
Java JCheckBox Example: Food Order
1. import javax.swing.*;
2. import java.awt.event.*;
3. public class CheckBoxExample extends JFrame implements ActionListener{
4. JLabel l;
5. JCheckBox cb1,cb2,cb3;
6. JButton b;
7. CheckBoxExample(){
8. l=new JLabel("Food Ordering System");
9. l.setBounds(50,50,300,20);
10. cb1=new JCheckBox("Pizza @ 100");
11. cb1.setBounds(100,100,150,20);
12. cb2=new JCheckBox("Burger @ 30");
13. cb2.setBounds(100,150,150,20);
14. cb3=new JCheckBox("Tea @ 10");
15. cb3.setBounds(100,200,150,20);
16. b=new JButton("Order");
17. b.setBounds(100,250,80,30);
18. b.addActionListener(this);
19. add(l);add(cb1);add(cb2);add(cb3);add(b);
20. setSize(400,400);
21. setLayout(null);
22. setVisible(true);
23. setDefaultCloseOperation(EXIT_ON_CLOSE);
24. }
25. public void actionPerformed(ActionEvent e){
26. float amount=0;
27. String msg="";
28. if(cb1.isSelected()){
29. amount+=100;
30. msg="Pizza: 100\n";
31. }
32. if(cb2.isSelected()){
33. amount+=30;
34. msg+="Burger: 30\n";
35. }
36. if(cb3.isSelected()){
37. amount+=10;
38. msg+="Tea: 10\n";
39. }
40. msg+="-----------------\n";
41. JOptionPane.showMessageDialog(this,msg+"Total: "+amount);
42. }
43. public static void main(String[] args) {
44. new CheckBoxExample();
45. }
46. }
Output:
Java JRadioButton
The JRadioButton class is used to create a radio button. It is used to choose one option from
multiple options. It is widely used in exam systems or quiz.
public class JRadioButton extends JToggleButton implements Accessible
Output:
Output:
Java JComboBox
The object of Choice class is used to show popup menu of choices. Choice selected by user
is shown on the top of a menu. It inherits JComponent class.
1. public class JComboBox extends JComponent implements ItemSelectable, ListDataListen
er, ActionListener, Accessible
Java JComboBox Example
1. import javax.swing.*;
2. public class ComboBoxExample {
3. JFrame f;
4. ComboBoxExample(){
5. f=new JFrame("ComboBox Example");
6. String country[]={"India","Aus","U.S.A","England","Newzealand"};
7. JComboBox cb=new JComboBox(country);
8. cb.setBounds(50, 50,90,20);
9. f.add(cb);
10. f.setLayout(null);
11. f.setSize(400,500);
12. f.setVisible(true);
13. }
14. public static void main(String[] args) {
15. new ComboBoxExample();
16. }
17. }
Output:
Output:
Java JTable
The JTable class is used to display data in tabular form. It is composed of rows and
columns.
Output:
If you select an element in column NAME, name of the element will be displayed on the
console:
1. Table element selected is: Sachin
Java JList
The object of JList class represents a list of text items. The list of text items can be set up
so that the user can choose either one item or multiple items. It inherits JComponent class.
Output:
Output:
Java JOptionPane
The JOptionPane class is used to provide standard dialog boxes such as message dialog box,
confirm dialog box and input dialog box. These dialog boxes are used to display information
or get input from the user. The JOptionPane class inherits JComponent class.
Output:
Output:
Java JOptionPane Example: showInputDialog()
1. import javax.swing.*;
2. public class OptionPaneExample {
3. JFrame f;
4. OptionPaneExample(){
5. f=new JFrame();
6. String name=JOptionPane.showInputDialog(f,"Enter Name");
7. }
8. public static void main(String[] args) {
9. new OptionPaneExample();
10. }
11. }
Output:
Output:
JAVA APPLETS
All of the preceding examples learned so far are console based applications.
Applets are small Internet-based programs written in Java and can be downloaded
by any computer.
The applet is usually embedded in an HTML page on a Web site and can be
executed from within a browser.
The Applet class provides the standard interface between the applet and the
browser environment.
The JApplet class should be used for all applets that use Swing components to
construct their graphical user interfaces (GUIs).
Note: One disadvantage of Applets is that plugins are required at the client
An applet undergoes various stages between its creation of objects and object
removal as the work of the applet will get done.
init () method: intended for whatever initialization needed for the applet
start () method: automatically called after the browser calls the init method or
when a user returns to the page containing the applet.
paint () method: invoked immediately after the start method, and also anytime the
applet needs repaint itself in the browser.
stop () method: automatically called when user moves off the page on which the
applet sits.
destroy () method: Only called when the browser shuts down normally.
import java.applet.*;
import java.awt.*;
<html>
<head>
</head>
<body>
</body>
</html>