ADVANCE JAVA PROGRAMMING UNIT 2
JApplet:
import javax.swing.*;
import java.awt.*;
public class A extends JApplet
{
public void paint(Graphics g)
{
g.drawString("hey there this is nikhil rathod", 40, 40);
}
}
/*
* <applet code=A width=300 height=400>
* </applet>
*/
We can create frame by 2 ways
1. By extending frame class (Association)
2. By instance of frame class (Inheritance)
• By extending frame class (Association)
import javax.swing.*;
import java.awt.*;
public class A extends JFrame
{
A(){
setVisible(true);
setSize(500, 500);
setTitle("JFrame");
}
public static void main(String[] args) {
new A();
}
}
• By instance of frame class (Inheritance)
import javax.swing.*;
import java.awt.*;
public class A
{
public static void main(String[] args)
{
JFrame jf = new JFrame("by instance");
jf.setSize(800,800);
jf.setVisible(true);
}
}
Swing Components:
1. Icons and Label.
import javax.swing.*;
import java.awt.*;
public class A extends JFrame
{
A(){
setVisible(true);
setSize(500, 500);
setTitle("JFrame");
setLayout(null);
// JLabel jl = new JLabel("hello"); // when u add normal label
// add(jl);
ImageIcon ic = new ImageIcon("C:\\Users\\NIKHIL RATHOD\\OneDrive\\Pictures\\Saved
Pictures\\m8 cs.jpg");
JLabel jl2 = new JLabel("Image",ic,JLabel.LEFT);
jl2.setBounds(40,40, 400, 300);
add(jl2);
}
public static void main(String[] args)
{
new A();
}
}
Output:
2. Text Fields.
import javax.swing.*;
import java.awt.*;
public class A extends Frame
{
A(){
setTitle("JTextField");
setVisible(true);
setSize(500,500);
setLayout(new FlowLayout());
JLabel jl = new JLabel("Enter your Name : ");
add(jl);
JTextField jt = new JTextField(30);
add(jt);
JLabel jl1 = new JLabel("Enter Password : ");
add(jl1);
JPasswordField jp = new JPasswordField(30);
add(jp);
JLabel jl2 = new JLabel("Enter your Address : ");
add(jl2);
JTextArea jta = new JTextArea(20, 30);
add(jta);
}
public static void main(String[] args) {
new A();
}
}
Output:
3. JCombo Boxes.
Constructors:
JComboBox();
JComboBox(object[] list);
import javax.swing.*;
import java.awt.*;
public class B extends JFrame
{
B()
{
setTitle("JComboBox");
setVisible(true);
setSize(500, 500);
setLayout(new FlowLayout(FlowLayout.CENTER));
String str[] = {"A","B","C","D","E","F","G","H","I","J","K"};
JComboBox jc = new JComboBox(str);
add(jc);
}
public static void main(String[] args)
{
new B();
}
}
Output:
4. JList.
Constructor:
JList();
JList(object[] list);
import javax.swing.*;
import java.awt.*;
public class newpractice extends JFrame
{
newpractice()
{
setTitle("JList");
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout(FlowLayout.LEFT));
String str[] =
{"arvi","nashik","mumbai","masuri","pune","amravti","nagpur","wardha","selu"};
JList jl = new JList(str);
add(jl);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new newpractice();
}
}
Output:
5. JScrollbar.
Constructor:
1. JScrollbar();
2. JScrollbar(Orientation);
3. JScrollbar(Orientation, int value, int extent, int min, int max);
import javax.swing.*;
import java.awt.*;
public class newpractice extends JFrame
{
newpractice()
{
setTitle("JList");
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout(FlowLayout.LEFT));
JScrollBar js = new JScrollBar(JScrollBar.VERTICAL);
add(js);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new newpractice();
}
}
Output:
6. JSeparator.
import javax.swing.*;
import java.awt.*;
public class B extends Frame {
B()
{
setTitle("JSeparator");
setSize(500, 500);
setVisible(true);
JSeparator js = new JSeparator(JSeparator.HORIZONTAL);
JButton jb = new JButton("OKK");
JButton jb1 = new JButton("ENTER");
add(jb);
add(js);
add(jb1);
jb.setBounds(40, 40, 120, 30);
js.setBounds(40, 80, 120, 20);
jb1.setBounds(40, 90, 120, 30);
}
public static void main(String[] args)
{
new B();
}
}
Output:
7. JMenu bar & JMenu.
import javax.swing.*;
import java.awt.*;
public class newpractice extends JFrame
{
newpractice()
{
setTitle("JList");
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout(FlowLayout.LEFT));
JMenuBar jb = new JMenuBar();
add(jb);
JMenu jm1 = new JMenu("File");
JMenu jm2 = new JMenu("Edit");
JMenu jm3 = new JMenu("View");
JMenu jm4 = new JMenu("Help");
jb.add(jm1);
jb.add(jm2);
jb.add(jm3);
jb.add(jm4);
JSeparator js = new JSeparator();
JMenuItem ji1 = new JMenuItem("save");
JMenu ji2 = new JMenu("save as");
JMenuItem ji3 = new JMenuItem("save all");
jm1.add(ji1);
jm1.add(js);
jm1.add(ji2);
jm1.add(ji3);
JMenuItem i1 = new JMenuItem("1");
JMenuItem i2 = new JMenuItem("2");
JMenuItem i3 = new JMenuItem("3");
ji2.add(i1);
ji2.add(i2);
ji2.add(i3);
JMenuItem ji4 = new JMenuItem("cut");
JMenuItem ji5 = new JMenuItem("copy");
JMenuItem ji6 = new JMenuItem("paste");
jm2.add(ji4);
jm2.add(ji5);
jm2.add(ji6);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new newpractice();
}
}
Output:
Buttons:
1. JButton
import javax.swing.*;
import java.awt.*;
public class A extends JFrame
{
A(){
setVisible(true);
setSize(500, 500);
setTitle("JFrame");
setLayout(new FlowLayout());
JButton j = new JButton("BMW M8 compition");
add(j);
}
public static void main(String[] args)
{
new A();
}
}
Output:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
public class A extends JFrame
{
A(){
setVisible(true);
setSize(600, 600);
setTitle("JFrame");
setLayout(null);
ImageIcon ic = new ImageIcon("C:\\Users\\NIKHIL RATHOD\\OneDrive\\Pictures\\Saved
Pictures\\m8 cs.jpg");
JButton jb = new JButton("submit", ic);
jb.setBounds(40, 40, 500, 200); // it aquire that much of Area..
jb.setMnemonic(KeyEvent.VK_S); // you can set Mnemonic
add(jb);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new A();
}
}
Output:
2. Check Boxes.
import javax.swing.*;
import java.awt.*;
public class A extends Frame
{
A(){
setTitle("JCheckBox");
setVisible(true);
setSize(500,500);
setLayout(new FlowLayout());
JCheckBox jc1 = new JCheckBox("pawnar");
JCheckBox jc2 = new JCheckBox("sawangi");
JCheckBox jc3 = new JCheckBox("borgaon");
JCheckBox jc4 = new JCheckBox("ramnagar");
add(jc1);
add(jc2);
add(jc3);
add(jc4);
}
public static void main(String[] args)
{
new A();
}
Output:
3. Radio button.
import javax.swing.*;
import java.awt.*;
public class A extends Frame
{
A(){
setTitle("JTextField");
setVisible(true);
setSize(500,500);
setLayout(new FlowLayout());
JRadioButton jb1 = new JRadioButton("pawan");
JRadioButton jb2 = new JRadioButton("nikhil");
JRadioButton jb3 = new JRadioButton("aditya");
JRadioButton jb4 = new JRadioButton("vivek");
JRadioButton jb5 = new JRadioButton("Chetan");
ButtonGroup bg = new ButtonGroup();
bg.add(jb1);
bg.add(jb2);
bg.add(jb3);
bg.add(jb4);
bg.add(jb5);
add(jb1);
add(jb2);
add(jb3);
add(jb4);
add(jb5);
}
public static void main(String[] args)
{
new A();
}
Output;
Advance Swing Components:
1. Tabbed Panes.
We can create frame inside frame.
Constructor:
1) JTabbedPane();
2) JTabbedPane(orientation);
import javax.swing.*;
import java.awt.*;
public class B extends JFrame
{
B()
{
setTitle("TabbedPen");
setSize(500, 500);
setLayout(new FlowLayout(FlowLayout.LEFT));
setVisible(true);
JTabbedPane jt = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JButton jb = new JButton("Hello");
jp1.add(jb);
JLabel jl = new JLabel("Enter your Name : ");
jp2.add(jl);
JTextField jt11 = new JTextField(30);
jp2.add(jt11);
JLabel jl1 = new JLabel("Enter Password : ");
jp3.add(jl1);
JPasswordField jp = new JPasswordField(30);
jp3.add(jp);
JLabel jl2 = new JLabel("Enter your Address : ");
jp4.add(jl2);
JTextArea jta = new JTextArea(20, 30);
jp4.add(jta);
jt.add("Tab-1", jp1);
jt.add("Tab-2", jp2);
jt.add("Tab-3", jp3);
jt.add("Tab-4", jp4);
add(jt);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new B();
}
}
Output:
2. Scroll Panes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
public class A extends JFrame
{
A(){
setVisible(true);
setSize(600, 600);
setTitle("JFrame");
setLayout(null);
JButton jb = new JButton("submit"); // this button is optional
JScrollPane js = new JScrollPane(jb);
js.setBounds(40, 40, 180, 120);
add(js);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new A();
}
}
Output:
3. Tree.
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
public class A extends JFrame
{
A(){
setVisible(true);
setSize(600, 600);
setTitle("JFrame");
setLayout(new FlowLayout(FlowLayout.LEFT));
DefaultMutableTreeNode d = new DefaultMutableTreeNode("BMW SPORTS");
DefaultMutableTreeNode d1 = new DefaultMutableTreeNode("M2");
DefaultMutableTreeNode d2 = new DefaultMutableTreeNode("M4");
DefaultMutableTreeNode d3 = new DefaultMutableTreeNode("M5");
DefaultMutableTreeNode d4 = new DefaultMutableTreeNode("M8");
DefaultMutableTreeNode d5 = new DefaultMutableTreeNode("M4 CS ");
DefaultMutableTreeNode d6 = new DefaultMutableTreeNode("M5 CS ");
DefaultMutableTreeNode d7 = new DefaultMutableTreeNode("M8 CS");
DefaultMutableTreeNode d8 = new DefaultMutableTreeNode("M8 CS COMPITION");
d.add(d1);
d.add(d2);
d.add(d3);
d.add(d4);
d2.add(d5);
d3.add(d6);
d4.add(d7);
d4.add(d8);
JTree jt = new JTree(d);
add(jt);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new A();
}
}
Output:
4. Tables.
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
public class A extends JFrame
{
A(){
setVisible(true);
setSize(600, 600);
setTitle("JFrame");
setLayout(new FlowLayout(FlowLayout.LEFT));
String label[] = {"SR","NAME","MARKS"};
String data[][] =
{{"1","asd","520"},{"1","asd","520"},{"1","asd","520"},{"1","asd","520"},{"1","asd","520"}
,{"1","asd","520"},{"1","asd","520"}};
JTable jt = new JTable(data, label);
JScrollPane js = new JScrollPane(jt);
add(js);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new A();
}
}
Output:
5. Progress Bar.
➔ Constructor of progress bar
1) JProgressBar() // default its orientation is HORIZONTAL
2) JProgressBar(int min, int maz)
3) JProgressBar(orientation)
4) JProgressBar(orientation, int min, int max)
import javax.swing.*;
import java.awt.*;
public class B extends Frame {
B()
{
setTitle("JSeparator");
setSize(500, 500);
setVisible(true);
setLayout(null);
JProgressBar jp = new JProgressBar(0, 100);
jp.setBounds(40, 50, 200, 50);
add(jp);
jp.setStringPainted(true);
for(int i=0;i<=100;i+=10)
{
jp.setValue(i);
try
{
Thread.sleep(1000);
}
catch(Exception e){}
if (i==100) {
JLabel jl = new JLabel("FreeFire downloded Sucessfuly");
jl.setBounds(40, 120, 200, 50);
add(jl);
}
}
public static void main(String[] args)
{
new B();
}
}
Output:
6. Tool tips.
The tool tip is generally gives the information about the components by just a hover over them.
Constructor of tool tip:
➔ setToolTipText(str);
import javax.swing.*;
import java.awt.*;
public class B extends JFrame
{
B()
{
setTitle("ToolTip");
setSize(500, 500);
setLayout(null);
setVisible(true);
JButton jb = new JButton("Button");
jb.setToolTipText("this is button");
jb.setBounds(40, 40, 150, 50);
add(jb);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new B();
}
}
output:
MVC:
• M = model
• V = View
• C = Controller