Categories: Java

Java Swing Components and Containers -II

Java JTable:

The JTable class is used to display tabular data. It is made up of rows and columns.

Constructors:

JTable()
JTable(Object[][] rows, Object[] columns)
Java JList:

The JList class object represents a list of text items. The list of text items can be configured so that the user can select one or more items. It derives from the JComponent class.

Constructors:

JList()
JList(ary[] listData)
JList(ListModel<ary> dataModel)
Java JOptionPane:

The JOptionPane class is used to create standard dialogue boxes such as message, confirm, and input dialogue boxes. These dialogue boxes are used to display information or to solicit user input.

Constructors:

JOptionPane()
JOptionPane(Object message)
JOptionPane(Object message, int messageType)
Java JScrollBar:

The JScrollbar class object is used to add horizontal and vertical scrollbars. It is a scrollbar implementation. It derives from the JComponent class.

Constructors:

JScrollBar()
JScrollBar(int orientation)
JScrollBar(int orientation, int value, int extent, int min, int max)
Java JMenuBar, JMenu and JMenuItem:

JMenuBar is a class used to display a menu bar on a window or frame. It could have multiple menus.

The JMenu class’s object is a pull-down menu component in the menu bar. It derives from the JMenuItem class.

The JMenuItem class object adds a simple labeled menu item. The items in a menu must be of the JMenuItem class or one of its subclasses.

Java JPopupMenu:

PopupMenu can be dynamically triggered at any point within a component. It derives from the JComponent class.

Constructors:

JPopupMenu()
JPopupMenu(String label)
Java JCheckBoxMenuItem:

The JCheckBoxMenuItem class represents a checkbox that can be added to a menu. A CheckBoxMenuItem can be associated with text, a graphic icon, or both. MenuItems can be chosen or deselected. Actions can be used to configure and control MenuItems.

Constructors:

JCheckBoxMenuItem()
JCheckBoxMenuItem(Action a)
JCheckBoxMenuItem(Icon icon)
JCheckBoxMenuItem(String text)
JCheckBoxMenuItem(String text, boolean b)
JCheckBoxMenuItem(String text, Icon icon)
JCheckBoxMenuItem(String text, Icon icon, boolean b)
Java JSeparator:

The JSeparator class provides a general-purpose component for implementing divider lines. It’s used to draw a line between widgets in a Layout. It derives from the JComponent class.

Constructors:

JSeparator()
JSeparator(int orientation)
Java JProgressBar:

The JProgressBar class is used to display the task’s progress. It derives from the JComponent class.

Constructors:

JProgressBar()
JProgressBar(int min, int max)
JProgressBar(int orient)
JProgressBar(int orient, int min, int max)
Java JTree:

The JTree class is used to display data that is tree-structured or hierarchical. JTree is a complex component. It has a ‘root node’ at the very top that serves as a parent for all nodes in the tree.

Constructors:

JTree()
JTree(Object[] value)
JTree(TreeNode root)
Example of Registration form:
// Java program to implement
// a Simple Registration Form
// using Java Swing

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Registration
 extends JFrame
 implements ActionListener {

 /**
  * 
  */ private static final long serialVersionUID = 1L;
 // Components of the Form
 private Container c;
 private JLabel title;
 private JLabel name;
 private JTextField tname;
 private JLabel mno;
 private JTextField tmno;
 private JLabel gender;
 private JRadioButton male;
 private JRadioButton female;
 private ButtonGroup gengp;
 private JLabel dob;
 private JComboBox date;
 private JComboBox month;
 private JComboBox year;
 private JLabel add;
 private JTextArea tadd;
 private JCheckBox term;
 private JButton sub;
 private JButton reset;
 private JTextArea tout;
 private JLabel res;
 private JTextArea resadd;

 private String dates[]
  = { "1", "2", "3", "4", "5",
   "6", "7", "8", "9", "10",
   "11", "12", "13", "14", "15",
   "16", "17", "18", "19", "20",
   "21", "22", "23", "24", "25",
   "26", "27", "28", "29", "30",
   "31" };
 private String months[]
  = { "Jan", "feb", "Mar", "Apr",
   "May", "Jun", "July", "Aug",
   "Sup", "Oct", "Nov", "Dec" };
 private String years[]
  = { "1995", "1996", "1997", "1998",
   "1999", "2000", "2001", "2002",
   "2003", "2004", "2005", "2006",
   "2007", "2008", "2009", "2010",
   "2011", "2012", "2013", "2014",
   "2015", "2016", "2017", "2018",
   "2019" };

 // constructor, to initialize the components
 // with default values.
 public Registration()
 {
  setTitle("Registration Form");
  setBounds(300, 90, 900, 600);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setResizable(false);

  c = getContentPane();
  c.setLayout(null);

  title = new JLabel("Registration Form");
  title.setFont(new Font("Arial", Font.PLAIN, 30));
  title.setSize(300, 30);
  title.setLocation(300, 30);
  c.add(title);

  name = new JLabel("Name");
  name.setFont(new Font("Arial", Font.PLAIN, 20));
  name.setSize(100, 20);
  name.setLocation(100, 100);
  c.add(name);

  tname = new JTextField();
  tname.setFont(new Font("Arial", Font.PLAIN, 15));
  tname.setSize(190, 20);
  tname.setLocation(200, 100);
  c.add(tname);

  mno = new JLabel("Mobile");
  mno.setFont(new Font("Arial", Font.PLAIN, 20));
  mno.setSize(100, 20);
  mno.setLocation(100, 150);
  c.add(mno);

  tmno = new JTextField();
  tmno.setFont(new Font("Arial", Font.PLAIN, 15));
  tmno.setSize(150, 20);
  tmno.setLocation(200, 150);
  c.add(tmno);

  gender = new JLabel("Gender");
  gender.setFont(new Font("Arial", Font.PLAIN, 20));
  gender.setSize(100, 20);
  gender.setLocation(100, 200);
  c.add(gender);

  male = new JRadioButton("Male");
  male.setFont(new Font("Arial", Font.PLAIN, 15));
  male.setSelected(true);
  male.setSize(75, 20);
  male.setLocation(200, 200);
  c.add(male);

  female = new JRadioButton("Female");
  female.setFont(new Font("Arial", Font.PLAIN, 15));
  female.setSelected(false);
  female.setSize(80, 20);
  female.setLocation(275, 200);
  c.add(female);

  gengp = new ButtonGroup();
  gengp.add(male);
  gengp.add(female);

  dob = new JLabel("DOB");
  dob.setFont(new Font("Arial", Font.PLAIN, 20));
  dob.setSize(100, 20);
  dob.setLocation(100, 250);
  c.add(dob);

  date = new JComboBox(dates);
  date.setFont(new Font("Arial", Font.PLAIN, 15));
  date.setSize(50, 20);
  date.setLocation(200, 250);
  c.add(date);

  month = new JComboBox(months);
  month.setFont(new Font("Arial", Font.PLAIN, 15));
  month.setSize(60, 20);
  month.setLocation(250, 250);
  c.add(month);

  year = new JComboBox(years);
  year.setFont(new Font("Arial", Font.PLAIN, 15));
  year.setSize(60, 20);
  year.setLocation(320, 250);
  c.add(year);

  add = new JLabel("Address");
  add.setFont(new Font("Arial", Font.PLAIN, 20));
  add.setSize(100, 20);
  add.setLocation(100, 300);
  c.add(add);

  tadd = new JTextArea();
  tadd.setFont(new Font("Arial", Font.PLAIN, 15));
  tadd.setSize(200, 75);
  tadd.setLocation(200, 300);
  tadd.setLineWrap(true);
  c.add(tadd);

  term = new JCheckBox("Accept Terms And Conditions.");
  term.setFont(new Font("Arial", Font.PLAIN, 15));
  term.setSize(250, 20);
  term.setLocation(150, 400);
  c.add(term);

  sub = new JButton("Submit");
  sub.setFont(new Font("Arial", Font.PLAIN, 15));
  sub.setSize(100, 20);
  sub.setLocation(150, 450);
  sub.addActionListener(this);
  c.add(sub);

  reset = new JButton("Reset");
  reset.setFont(new Font("Arial", Font.PLAIN, 15));
  reset.setSize(100, 20);
  reset.setLocation(270, 450);
  reset.addActionListener(this);
  c.add(reset);

  tout = new JTextArea();
  tout.setFont(new Font("Arial", Font.PLAIN, 15));
  tout.setSize(300, 400);
  tout.setLocation(500, 100);
  tout.setLineWrap(true);
  tout.setEditable(false);
  c.add(tout);

  res = new JLabel("");
  res.setFont(new Font("Arial", Font.PLAIN, 20));
  res.setSize(500, 25);
  res.setLocation(100, 500);
  c.add(res);

  resadd = new JTextArea();
  resadd.setFont(new Font("Arial", Font.PLAIN, 15));
  resadd.setSize(200, 75);
  resadd.setLocation(580, 175);
  resadd.setLineWrap(true);
  c.add(resadd);

  setVisible(true);
 }

 // method actionPerformed()
 // to get the action performed
 // by the user and act accordingly
 public void actionPerformed(ActionEvent e)
 {
  if (e.getSource() == sub) {
   if (term.isSelected()) {
    String data1;
    String data
     = "Name : "
     + tname.getText() + "\n"
     + "Mobile : "
     + tmno.getText() + "\n";
    if (male.isSelected())
     data1 = "Gender : Male"
       + "\n";
    else
     data1 = "Gender : Female"
       + "\n";
    String data2
     = "DOB : "
     + (String)date.getSelectedItem()
     + "/" + (String)month.getSelectedItem()
     + "/" + (String)year.getSelectedItem()
     + "\n";

    String data3 = "Address : " + tadd.getText();
    tout.setText(data + data1 + data2 + data3);
    tout.setEditable(false);
    res.setText("Registration Successfully..");
   }
   else {
    tout.setText("");
    resadd.setText("");
    res.setText("Please accept the"
       + " terms & conditions..");
   }
  }

  else if (e.getSource() == reset) {
   String def = "";
   tname.setText(def);
   tadd.setText(def);
   tmno.setText(def);
   res.setText(def);
   tout.setText(def);
   term.setSelected(false);
   date.setSelectedIndex(0);
   month.setSelectedIndex(0);
   year.setSelectedIndex(0);
   resadd.setText(def);
  }
 }


// Driver Code


 public static void main(String[] args) throws Exception
 {
  Registration f = new Registration();
 }
}
output:

After filling out and submitting the form.:

Note: also read about the Java Swing Components and Containers -I

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago