Ajp Presentation

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Shri Chhatrapati Shivaji Maharaj

College Of Engineering

Subject Teacher :-

Team
prof.S.V.Chitale
 Ostwal Isha Lalit
Members :-  Puri Sakshi Ramesh
Shinde Suhani Vijay
Gaikwad Snehal Sunil

Gunjal Punam Khandu


Gavhane Geeta Dnyaneshwar



Topic  Java Swing


Name :-
What is Java Swing
• Java Swing is a part of Java Foundation Classes (JFC) that is
used to create window-based applications.
• It is built on the top of AWT (Abstract Windowing Toolkit) API
and entirely written in java.
• Swing is a set of classes that provides more powerful and
flexible components than are possible with the AWT.
• Unlike AWT components, Swing components are not
implemented by platform-specific code.
• The term lightweight is used to describe such elements.
• The javax.swing package provides classes for java swing API
such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox,
JMenu, JColorChooser etc.
What is JFC
• JFC stands for Java Foundation Classes.
• The Java Foundation Classes (JFC) are a set of GUI components
which simplify the development of desktop applications.
• It is a rich and comprehensive set of GUI components and
services that simplify the development and deployment of
desktop, client-side, and internet applications.
• It is a superset that contains AWT. JFC extends AWT by adding
many components and services.
• The Java Foundation Classes (JFC) are a graphical framework for
building portable Java-based graphical user interfaces (GUIs).
• JFC consists of the Abstract Window Toolkit (AWT), Swing and
Java.
Hierarchy of Java Swing
classes
Commonly used Methods of Component class

Components of swing
JButton, JLabel, JTextField, JTextArea, JPasswordField, JCheckBox,
JOptionPane, JRadioButton, JComboBox, Tabbed Panes , JScrollPane,
JMenuBar, JMenu JMenuItem, JTree, JTable, JProgressBar.
JButto
n JButton class is used to create a labeled button that has platform independent
The
implementation. The application result in some action when the button is pushed.
It inherits AbstractButton class.

Constructors:-
JButton() :- It creates a button with no text and icon.
JButton(String s) :- It creates a button with the specified text.
JButton(Icon i) :- It creates a button with the specified icon object.

Methods :
- setText(String s) :- It is used to set specified text on button
void
String getText() :- It is used to return the text of the button.
void setEnabled(boolean b) :- It is used to enable or disable the button.
void setIcon(Icon b) :- It is used to set the specified Icon on the button.
Icon getIcon() :- It is used to get the Icon of the button.
void addActionListener(ActionListener a) :- It is used to add the action listener to this
object.
Java JButton Example :-
import javax.swing.*;
public class ButtonExample1
{
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);
}
}
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 text) :- 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.

Methods :-
void addActionListener(ActionListener) :- It is used to add the specified action
listener to
receive action events from this textfield.
Action getAction() It returns the currently set Action for this ActionEvent source,
or null if no Action is set.
Java JTextField Example :-
import javax.swing.*;
class TextFieldExample
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to Arrow World.");
t1.setBounds(50,100, 200,30);
t2=new JTextField("Swing Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1);
f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
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.
Constructors :-

JPasswordField() :- Constructs a new JPasswordField, with a default document,


and null starting text string
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 thespecified text and columns.
Java JPasswordField Example :-
import javax.swing.*;
public class PasswordFieldExample
{
public static void main(String[] args)
{
JFrame f=new JFrame("Password Field
Example");
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(100,100,100,30);
f.add(value);
f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
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.
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.
Java JCheckBox Example :-
import javax.swing.*;
public class CheckBoxExample
{
public static void main(String args[])
{
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C+
+");
checkBox1.setBounds(100,100,100,50);
JCheckBox checkBox2 = new
JCheckBox("Java", true);
checkBox2.setBounds(100,150,100,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
JScrollPane :-
A scroll pane is a component that presents a rectangular area in which a
component may be viewed. Horizontal and/or vertical scroll bars may be
provided if necessary. Scroll panes are implemented in Swing by the
JScrollPane class, which extends JComponent.

Constructors :-
JScrollPane()
JScrollPane(Component comp)
JScrollPane(int vsb, int hsb) JScrollPane(Component comp, int vsb, int hsb)

HORIZONTAL_SCROLLBAR_ALWAYS
Always provide horizontal scroll bar
HORIZONTAL_SCROLLBAR_AS_NEEDED
Provide horizontal scroll bar, if needed
VERTICAL_SCROLLBAR_ALWAYS
Always provide vertical scroll bar
VERTICAL_SCROLLBAR_AS_NEEDED
Provide vertical scroll bar, if needed
Java JScrollPane Example :-
import javax.swing.*;
import java.awt.*;
public class JScrollPaneDemo
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Scroll
Pane Example");
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(20, 20));
int b = 0;
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 20; j++)
{
jp.add(new JButton("Button " + b));
++b;
}
}
int v =
ScrollPaneConstants.VERTICAL_SCROL
LBAR_AS_NEEDED;
int h =
ScrollPaneConstants.HORIZONTAL_SC
ROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jp,
v, h);
frame.add(jsp,BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Java JMenuBar, JMenu and JMenuItem :-
The JMenuBar class is used to display menubar on the window or frame. It may have
several menus.
The object of JMenu class is a pull down menu component which is displayed from
the menubar.
It inherits the JMenuItem class.
The object of JMenuItem class adds a simple labeled menu item.

Java JMenuBar, JMenu and JMenuItem Example :-


import javax.swing.*;
import java.awt.event.*;
public class MenuExample extends JFrame
implements ActionListener
{
JMenuBar mb;
JMenu file,edit,help;
JMenuItem cut,copy,paste,selectAll;
JTextArea ta;
MenuExample()
{
cut=new JMenuItem("cut");
copy=new JMenuItem("copy");
paste=new JMenuItem("paste");
selectAll=new JMenuItem("selectAll");
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
mb=new JMenuBar();
file=new JMenu("File");
edit=new JMenu("Edit");
help=new JMenu("Help");
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(selectAll);
mb.add(file);
mb.add(edit);
mb.add(help);
ta=new JTextArea();
ta.setBounds(5,5,360,320);
add(ta);
setJMenuBar(mb); }
public void
actionPerformed(ActionEvent e) {
if(e.getSource()==cut)
ta.cut();
if(e.getSource()==paste)
ta.paste();
if(e.getSource()==copy)
ta.copy();
if(e.getSource()==selectAll)
ta.selectAll();
}
public static void main(String[]
args) {
MenuExample f = new
MenuExample();
f.setLayout(null);
f.setSize(400,400);
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.
Trees are implemented in Swing by the JTree class, which extends JComponent.

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.
• The DefaultMutableTreeNode class implements the
MutableTreeNode interface.
• It represents a node in a tree. One of its
constructors is shown here:
• DefaultMutableTreeNode(Object obj) :-

Here, obj is the object to be enclosed in this tree


node.
The new tree node doesn’t have a parent or children.
To create a hierarchy of tree nodes, the add( ) method
of DefaultMutableTreeNode can be
used
Java JTree Example ;-
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
public class Tree2
{
public static void main(String args[])
{
JFrame jf = new JFrame("JTree");
JLabel label1 = new JLabel("Displaying tree");

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Nutritious Food");

DefaultMutableTreeNode fruits = new DefaultMutableTreeNode("Fruits");

DefaultMutableTreeNode vegetables = new DefaultMutableTreeNode("Vegetables");

DefaultMutableTreeNode dryFruits = new DefaultMutableTreeNode("Dry Fruits");


rootNode.add(fruits);
rootNode.add(vegetables);
rootNode.add(dryFruits);

DefaultMutableTreeNode fruit1 = new DefaultMutableTreeNode("Apple");


DefaultMutableTreeNode fruit2= new DefaultMutableTreeNode("Mango")
fruits.add(fruit1);
fruits.add(fruit2);

DefaultMutableTreeNode vegetable1 = new DefaultMutableTreeNode("Cabbage");


DefaultMutableTreeNode vegetable2= new DefaultMutableTreeNode("Tomato");

vegetables.add(vegetable1);
vegetables.add(vegetable2);

DefaultMutableTreeNode dryFruit1 = new DefaultMutableTreeNode("Almonds");


DefaultMutableTreeNode dryFruit2 = new DefaultMutableTreeNode("Walnuts");

dryFruits.add(dryFruit1);
dryFruits.add(dryFruit2);
JTree tree = new JTree(rootNode);

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

JScrollPane jsp = new JScrollPane(tree,v,h);

jf.add(label1);
jf.add(jsp,BorderLayout.CENTER);
jf.setSize(300,200);
jf.setVisible(true);
}
}

You might also like