Cce103 Second Topic
Cce103 Second Topic
Constructor is a special method used in Java to initialize objects and this can be called when an object is created within a class.
Vector is a class that implements adding and removing of items in an array of objects.
ArrayList
•The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as
needed.
•Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must
know in advance how many elements an array will hold.
•Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are
removed, the array may be shrunk.
ArrayList Constructors
The ArrayList class supports three constructors. The first constructor builds an
empty array list:
ArrayList( )
The following constructor builds an array list that is initialized with the elements of
the collection c.
ArrayList(Collection c)
The following constructor builds an array list that has the specified initial capacity.
The capacity is the size of the underlying array that is used to store the elements.
The capacity grows automatically as elements are added to an array list.
ArrayList(int capacity)
ArrayList Methods:
add(int index, Object element) – used to insert specified element at the specified position index in the list
set(int index, Object element) – used to replace the element at the specified index position
get(int index) – used to display the element of the specified index position from the list.
size() – used to display the number of elements from the array list.
remove(int index) – used to remove the element from the array list.
package sam;
import java.util.ArrayList;
public class Sam {
public static void main(String[] args) {
ArrayList phones = new ArrayList();
phones.add("Samsung"); Output:
phones.add("Vivo"); Samsung
phones.add("Oppo"); Vivo
phones.add("iPhone"); Oppo
phones.add("Huawei"); [Samsung, Vivo, Oppo, iPhone, Huawei]
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
System.out.println(phones);
}
}
package sam;
import java.util.ArrayList;
public class Sam {
phones.add("Samsung");
phones.add("Vivo");
phones.add("Oppo"); Output:
phones.add("iPhone"); [Nokia, Vivo, Oppo, iPhone, Huawei]
phones.add("Huawei"); Nokia
phones.set(0,"Nokia"); Vivo
System.out.println(phones); Oppo
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
}
}
package sam;
import java.util.ArrayList;
public class Sam {
phones.add("Samsung");
Output:
phones.add("Vivo");
phones.add("Oppo");
[Nokia, Vivo, Oppo, iPhone, Huawei]
phones.add("iPhone");
Nokia
phones.add("Huawei");
Vivo
phones.set(0,"Nokia");
Oppo
System.out.println(phones);
5
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
System.out.println(phones.size());
}
}
package sam;
import java.util.ArrayList;
public class Sam {
public static void main(String[] args) {
ArrayList phones = new ArrayList();
phones.add("Samsung");
phones.add("Vivo"); Output:
phones.add("Oppo"); [Nokia, Oppo, iPhone, Huawei]
phones.add("iPhone"); Nokia
phones.add("Huawei"); Oppo
phones.set(0,"Nokia"); iPhone
phones.remove(1); 4
System.out.println(phones);
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
System.out.println(phones.size());
}
}
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
// create an empty array list with an initial capacity
ArrayList<Integer> arrlist = new ArrayList<Integer>();
•The ArrayList a collection which can contain many objects of the same type and implement a List interface to add items,
change items, remove items and clear the list of items.
• A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second
most-used data structure after array. Following are the important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link called First.
package sam;
import java.util.*;
public class Sam {
}
}
package sam;
import java.util.*;
public class Sam {
Syntax:
public static void main(String[] args) {
getLast() – accessing an item at the end of the list.
LinkedList phones = new LinkedList();
phones.add("Samsung");
phones.add("Vivo");
phones.add("Oppo"); Output:
phones.getLast(); Oppo
System.out.println(phones);
}
}
Swing in Java is a toolkit that composed of Graphical User Interface components.
Java Swing Library is a built-in library on top of the old Java Abstract Widget Toolkit (AWT).
Graphical User Interface in Java is a user-friendly visual experience builder for Java applications.
BorderLayout - refers to the places of the components, which can either be on top, bottom, left, right, and center.
FlowLayout – default layout manager of every JPanel, which lays out components in a single row commonly one after the
another.
Swing GUI composition
1.Containers
2. Components
Container
•is a class that is designed to hold the group of components.
•A container is also known as component
Swing in Java is a toolkit that composed of Graphical User Interface components.
Java Swing Library is a built-in library on top of the old Java Abstract Widget Toolkit (AWT).
Graphical User Interface in Java is a user-friendly visual experience builder for Java applications.
Java Layout Manager
•is used to organize the GUI components within a container.
•The common layout managers are BorderLayout, and FlowLayout.
BorderLayout - refers to the places of the components, which can either be on top, bottom, left, right, and center.
FlowLayout – default layout manager of every JPanel, which lays out components in a single row commonly one after
the another.
Swing GUI composition
1. Containers J
The identifies all Swing components. Swing used to be
2. Components marketed as Java Foundation Classes, before it became an
integral part of the JDK. On Dec. 5 2010
Container
•is a class that is designed to hold the group of components.
•A container is also known as component
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0); Sample Border Layout
}
package swinglayoutdemo; });
controlPanel = new JPanel();
import java.awt.*; controlPanel.setLayout(new FlowLayout());
import java.awt.event.*; mainFrame.add(headerLabel);
import javax.swing.*; mainFrame.add(controlPanel);
public class SwingLayoutDemo { mainFrame.add(statusLabel);
private JFrame mainFrame; mainFrame.setVisible(true);
private JLabel headerLabel; }
private void showBorderLayoutDemo(){
private JLabel statusLabel; headerLabel.setText("Layout in action: BorderLayout");
private JPanel controlPanel; JPanel panel = new JPanel();
private JLabel msglabel; panel.setBackground(Color.darkGray);
public SwingLayoutDemo(){ panel.setSize(300,300);
prepareGUI(); } BorderLayout layout = new BorderLayout();
public static void main(String[] args){ layout.setHgap(10);
layout.setVgap(10);
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo(); panel.setLayout(layout);
swingLayoutDemo.showBorderLayoutDemo(); panel.add(new JButton("Center"),BorderLayout.CENTER);
} panel.add(new JButton("Line Start"),BorderLayout.LINE_START);
private void prepareGUI(){ panel.add(new JButton("Line End"),BorderLayout.LINE_END);
mainFrame = new JFrame("Java SWING Examples"); panel.add(new JButton("East"),BorderLayout.EAST);
mainFrame.setSize(400,400); panel.add(new JButton("West"),BorderLayout.WEST);
panel.add(new JButton("North"),BorderLayout.NORTH);
mainFrame.setLayout(new GridLayout(3, 1)); panel.add(new JButton("South"),BorderLayout.SOUTH);
import java.awt.*;
import javax.swing.*;
package swingsample1;
import javax.swing.*; Output:
public class Swingsample1 {
JFrame f;
Swingsample1(){
f=new JFrame();
String name=JOptionPane.showInputDialog(f,"Enter Name");
Output:
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
JButton Example with ActionListener
import java.awt.event.*;
import javax.swing.*; Output:
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
JLabel Example
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
import javax.swing.*;
class LabelExample
{ Output:
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Second Label.");
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true); } }
JLabel Example with ActionListener
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LabelExample extends Frame implements ActionListener{
JTextField tf; JLabel l; JButton b; Otput:
LabelExample(){
tf=new JTextField();
tf.setBounds(50,50, 150,20);
l=new JLabel();
l.setBounds(50,100, 250,20);
b=new JButton("Find IP");
b.setBounds(50,150,95,30);
b.addActionListener(this);
add(b);add(tf);add(l);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try{
String host=tf.getText();
String ip=java.net.InetAddress.getByName(host).getHostAddress();
l.setText("IP of "+host+" is: "+ip);
}catch(Exception ex){System.out.println(ex);}
}
public static void main(String[] args) {
new LabelExample();
}}
JTextField Example
JTextField
The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class.
• public class JTextField extends JTextComponent implements SwingConstants
import javax.swing.*;
class TextFieldExample
{ Output:
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
t2=new JTextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true); } }
JTextField Example with ActionListener
import javax.swing.*;
import java.awt.event.*;
public class TextFieldExample implements ActionListener{
JTextField tf1,tf2,tf3;
Output:
JButton b1,b2; }
TextFieldExample(){ public void actionPerformed(ActionEvent e) {
JFrame f= new JFrame(); String s1=tf1.getText();
tf1=new JTextField(); String s2=tf2.getText();
tf1.setBounds(50,50,150,20); /*50=column spacing, 70=row spacing int a=Integer.parseInt(s1);
100=Textfield lenght, 20=box thickness */ int b=Integer.parseInt(s2);
int c=0;
tf2=new JTextField(); if(e.getSource()==b1){
tf2.setBounds(50,100,150,20); c=a+b;
tf3=new JTextField(); }else if(e.getSource()==b2){
tf3.setBounds(50,150,150,20); c=a-b;
tf3.setEditable(false); }
b1=new JButton("+"); String result=String.valueOf(c);
b1.setBounds(50,200,50,50); tf3.setText(result);
b2=new JButton("-"); }
b2.setBounds(120,200,50,50); public static void main(String[] args) {
b1.addActionListener(this); new TextFieldExample();
b2.addActionListener(this); }}
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
JCheckBox Example
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.
import javax.swing.*;
public class CheckBoxExample
{ Output:
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}}
JCheckBox Example with ItemListener
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxExample Output:
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);
}
JCheckBox checkbox1 = new JCheckBox("C++");
});
checkbox1.setBounds(150,100, 50,50);
f.setSize(400,400);
JCheckBox checkbox2 = new JCheckBox("Java");
f.setLayout(null);
checkbox2.setBounds(150,150, 50,50);
f.setVisible(true);
f.add(checkbox1); f.add(checkbox2); f.add(label);
}
checkbox1.addItemListener(new ItemListener() {
public static void main(String args[])
public void itemStateChanged(ItemEvent e) {
{
label.setText("C++ Checkbox: "
new CheckBoxExample();
+ (e.getStateChange()==1?"checked":"unchecked"));
}
}
JRadioButton Example
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.
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
Output:
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new RadioButtonExample(); } }
JRadioButton Example with ActionListener
import javax.swing.*; Output:
import java.awt.event.*;
class RadioButtonExample extends JFrame implements
ActionListener{
JRadioButton rb1,rb2;
JButton b;
RadioButtonExample(){
rb1=new JRadioButton("Male");
rb1.setBounds(100,50,100,30);
rb2=new JRadioButton("Female");
rb2.setBounds(100,100,100,30);
ButtonGroup bg=new ButtonGroup(); public void actionPerformed(ActionEvent e){
bg.add(rb1);bg.add(rb2); if(rb1.isSelected()){
b=new JButton("click"); JOptionPane.showMessageDialog(this,"You are Male.");
b.setBounds(100,150,80,30); }
b.addActionListener(this); if(rb2.isSelected()){
add(rb1);add(rb2);add(b); JOptionPane.showMessageDialog(this,"You are Female.");
setSize(300,300); }
setLayout(null); }
setVisible(true); public static void main(String args[]){
} new RadioButtonExample();
}}
Activity 4 Second_topic( given elements are 1,2,3,4,5)
1. Create a program that will perform the ArraList() methods. These methods are add(), set(), get(), size(), remove() which to
be used in the program coding.
1.1 Use get() method to display the elements of the specified index position from the list started numbers 0, 1, 2, 3, and 4.
1.2 Use set() method to replace the element on index 2 by 5 at the specified index position.
1.3 use remove() method to remove the element from the array list, index numbers 2 and 3.
1.4 Use size() method to determine the remaining size of the elements.
2. Create a program using JoptionPane to input two numbers, perform the four operations of addition, subtraction,
multiplication, and division, and display the results in one dialog message.
3. Create a program using JoptionPane with a condition of if or case statement in the program. The user will ask to choose
which of the four operations is to be performed. And if the user will pick one of the choices he displays then the output will show on
the screen.
4. Create a program using Jframe to input numbers to jtextfield and click the choices correspond the Jbutton that displays on the
screen the sign of four of the four operation.
Output:sample Jframe
import javax.swing.JFrame;
}
package swingsample4;
import javax.swing.JFrame;
import javax.swing.JLabel;
FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// true means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
FRAME.add(LABEL);
}
}
package swingsample4;
import javax.swing.JFrame; Sample display Jframe and Jlabel
import javax.swing.JLabel;
FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// tre means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
FRAME.add(LABEL);
FRAME.setLayout(null);//use to unlock the fixed setting
}
}
package swingsample4;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
//button setting
FRAME.add(Button);//clickcable
Button.setBounds(250,100,100,100);
// frame settings
FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// tre means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
FRAME.add(LABEL);
FRAME.setLayout(null);//use to unlock the fixed setting
}
}
package swingsample4;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
//button setting
Button.setBounds(250,100,100,100);
// frame settings
FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// tre means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
//label setting
FRAME.add(LABEL);
FRAME.add(Button);//transfer from the button setting
FRAME.setLayout(null);//use to unlock the fixed setting
}
}
Output:
package swing_demo.java;
//button setting
import java.awt.Color; Button.setBackground(Color.orange);
import javax.swing.JButton; Button.setBounds(250,100,100,100);
import javax.swing.JFrame; FRAME.add(Button);
import javax.swing.JLabel; // frame setting
import javax.swing.JPanel; FRAME.setSize(600,600);//Dimensions
public class Swing_demoJava {
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String[] args) { FRAME.setTitle("Sting_sample");
JFrame FRAME= new JFrame();
JPanel basicpanel= new JPanel(); //label setting
JLabel LABEL= new JLabel(" BASIC GUI LABEL"); FRAME.add(basicpanel);
//FRAME SETTINGS FRAME.add(LABEL);
LABEL.setBounds(100,100,100,100); FRAME.add(Button);
basicpanel.setBounds(250,80,200,200); FRAME.setLayout(null);
basicpanel.setBackground(Color.gray); FRAME.setVisible(true);// tre means frame will be visible
JButton Button=new JButton("Click Here"); }