1 of 15

Advanced JAVA Programming

(COURSE CODE: B19CS2201)

Exploring Swing

Sri. M.Jeevana Sujitha

Assistant Professor

Department of Computer Science and Engineering SRKR Engineering College, Bhimavaram, A.P. - 534204

2 of 15

OBJECTIVES

The Objective of this lecture is

  • To learn about how to explore Swing.

3 of 15

Exploring Swing

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. Constructors:
  • JButton(): creates a button with no text and icon.
  • JButton(String s): creates a button with specified text.
  • JButton(Icon i): creates a button with the specified icon object.

Example:

JButton b=new JButton("Click Here");

4 of 15

Exploring Swing

JLabel: 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. Constructors:
  • JLabel(): Creates a JLabel instance with no image and with an empty string for the title.
  • JLabel(String s): Creates a JLabel instance with the

specified text.

  • JLabel(Icon i): Creates a JLabel instance with the specified image.

Example:

JLabel l=new JLabel(“username");

5 of 15

Exploring Swing

JTextField: The object of a JTextField class is a text component that allows the editing of a single line text.

  • It inherits JTextComponent class.

Constructors:

  • JTextField(): Creates a new TextField.
  • JTextField(String txt): Creates a new TextField initialized with the specified text.
  • JTextField(String text, int columns): Creates a new

TextField initialized with the specified text and columns.

  • JTextField(int columns): Creates a new empty TextField with the specified number of columns.

Example:

JTextField t=new JTextField(“srinu”);

6 of 15

Exploring Swing

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. Constructors:
  • JTextArea(): Creates a text area that displays no text initially.
  • JTextArea(String s): Creates a text area that displays specified text initially.
  • JTextArea(int rows, int columns): Creates a text area with the specified number of rows and columns that displays no text initially.
  • JTextArea(String s, int rows, int columns): Creates a text area

with the specified number of rows and columns that displays specified text.

Example:

JTextArea area=new JTextArea(“palakollu");

7 of 15

Exploring Swing

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. Constructors:
  • JPasswordField(): Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.
  • JPasswordField(int columns): Constructs a new empty

JPasswordField with the specified number of columns.

  • JPasswordField(String text): Constructs a new JPasswordField

initialized with the specified text.

  • JPasswordField(String text, int columns): Construct a new JPasswordField initialized with the specified text and columns.

Example:

JPasswordField value = new JPasswordField();

8 of 15

Exploring Swing

JCheckBox: The JCheckBox class is used to create a checkbox.

  • It is used to turn an option on (true) or off (false).

Constructors:

  • JCheckBox(): Creates an initially unselected check box button with no text, no icon.
  • JChechBox(String s): Creates an initially unselected check

box with text.

  • JCheckBox(String text, boolean selected): Creates a check box with text and specifies whether or not it is initially selected.

Example:

JCheckBox checkBox1 = new JCheckBox("C++");

JCheckBox checkBox2 = new JCheckBox("Java", true);

9 of 15

Exploring Swing

JRadioButton: The JRadioButton class is used to create a radio button.

  • It is used to choose one option from multiple options.

Constructors:

  • JRadioButton(): Creates an unselected radio button with no text.
  • JRadioButton(String s): Creates an unselected radio button with specified text.
  • JRadioButton(String s, boolean selected): Creates a radio button with the specified text and selected status.

Example:

JRadioButton r1=new JRadioButton("Male"); JRadioButton r2=new JRadioButton("Female");

10 of 15

Exploring Swing

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.

Constructors:

  • JComboBox(): Creates a JComboBox with a default data model.
  • JComboBox(Object[] items): Creates a JComboBox that

contains the elements in the specified array.

  • JComboBox(Vector<?> items): Creates a JComboBox that contains the elements in the specified Vector.

Example:

String languages[]={"C","C++","C#","Java","PHP"};

final JComboBox cb=new JComboBox(languages);

11 of 15

Exploring Swing

JTable: The JTable class is used to display data in tabular form. It is composed of rows and columns.

Constructors:

  • JTable(): Creates a table with empty cells.
  • JTable(Object[][] rows, Object[] columns): Creates

String

a table with the specified data.

Example:

String data[][]={ {"101","Amit","CSE"},

{"102","Jai","ECE"},

{"101","Sachin","IT"}};

column[]={"ROLLNO","NAME","DEPARTMENT"};

JTable jt=new JTable(data,column);

12 of 15

Exploring Swing

JTree: The JTree class is used to display the tree structured data or hierarchical data.

  • JTree is a complex component.
  • It has a 'root node' at the top most which is a parent for all nodes in the tree.
  • It inherits JComponent class. Constructors:
  • JTree(): Creates a JTree with a sample model.
  • JTree(Object[] value): Creates a JTree with every element of the specified array as the child of a new root node.
  • JTree(TreeNode root): Creates a JTree with the specified TreeNode as its root, which displays the root node.

13 of 15

Exploring Swing

JTabbedPane: The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or icon.

  • It inherits JComponent class. Constructors:
  • JTabbedPane(): Creates an empty TabbedPane with a default tab placement of JTabbedPane.Top.
  • JTabbedPane(int tabPlacement): Creates an empty TabbedPane with a specified tab placement.

14 of 15

Exploring Swing

JScrollPane: A JscrollPane is used to make scrollable view of a component.

  • When screen size is limited, we use a scroll pane to display a large component or a component whose size can change dynamically.

15 of 15

THANK YOU