Professional Documents
Culture Documents
UNIT-V Topics To Be Covered: Object Oriented Programming Through Java
UNIT-V Topics To Be Covered: Object Oriented Programming Through Java
UNIT-V Topics To Be Covered: Object Oriented Programming Through Java
Introduction to swings
scroll
bars
Method Description
add a component on another
public void add(Component c)
component.
public void setSize(int width,int height) sets size of the component.
sets the layout manager for the
public void setLayout(LayoutManager m)
component.
sets the visibility of the component.
public void setVisible(boolean b)
It is by default false.
import javax.swing.*;
import java.awt.*;
public class SwingP1{
public static void main(String args[])
{
JFrame f= new JFrame(“Welcome to SNIST");
f.setVisible(true);
f.setSize(400,400);
}
}
AWT SWINGS
AWT components platform dependent Swings components are platform
AWT components are heavy weight Light weight
Less components than Swings More components are created with
swings like tables, trees, color
picker, tabbedpane etc.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JLabelEx extends JFrame
{
JLabelEx()
{
ImageIcon i= new ImageIcon("/example.png");
JLabel j= new JLabel("Hello",i,JLabel.CENTER);
setSize(400,400);
add(j);
setVisible(true);
}
public static void main(String args[])
{
new JLabelEx();
}
}
1. JButton(Icon i)
2. JButton(String s);
3. Jbutton(String s,Icon i);
1. JTextField()
1. TextField- it is a text component allows the user to edit a single line of text.
3. TextArea(String str)
Methods
6. Void append(String str); appends the string at end
7. Void insert(String str, int pos); inserts the string in specified position
8. getText()
9. setText(String str)
10. getSelectedText()
Methods
Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)
String getItem(int) //To get the specified item from the list
Methods
getValue()
setValue(int newValue);
int getMinimum( )
int getMaximum( )
It is recommended that you place the user interface components in panels and
place the panels in a frame.
Panel()
Panel(LayoutManager layout)
//PanelEx.java
import java.awt.*;
public class PanelEx extends Frame{
public PanelEx(){
setLayout(new FlowLayout());
Panel pan1 = new Panel();
pan1.setSize(200,100);
pan1.setBackground(Color.red);
pan1.setLocation(50,50);
add( pan1 );
setSize(400,400);
setVisible(true);
Button button1 = new Button("ok");
Button button2 = new Button("cancel");
pan1.add( button1 );
pan1.add( button2 );
}
public static void main(String args[]){
new PanelEx();
}
}
It is similar to a panel.
If size of the component held is larger than the size of the ScrollPane,
Scroll bars will be automatically generated.
Constructors
ScrollPane()
It is needed if you do any drawing or painting other than just using standard
GUI components.
You can cause the paint method to be called at any time by calling the
component’s repaint() method.
1. It calls update(Graphics g), which writes over the old drawing in background
color (thus erasing it).
x
Java coordinate system ( 0,0)
(x,y)
y
width
(x, y)
drawOval parameters
height
width
Vigneswara Reddy,IT of Dept
g.drawOval(x1, y1, width, height)
//Draws an oval with specified width and height. The bounding
rectangle’s top left corner is at the coordinate (x,y).The oval
touches all four sides all four sides of the bounding rectangle.
g.setColor(Color.RED)
//Sets color, it is remain active until new color is set.
90 90
180 0 180 0
270 270
int xpoints[]={30,200,30,200,30}
int ypoints[]={30,30,200,200,30}
int x=5;
g.drawPolygon(xpoints,ypoints,x);
1. BoarderLayout
2. CardLayout
3. GridLayout
4. FlowLayout
5. GridBagLayout
6. BoxLayout
7. GroupLayout
8. SpringLayout
Example:
setLayout(gridbag);
Constructors
1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and
vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and the given
horizontal and vertical gap.
Relieving
Vigneswara Reddy,IT of Dept
CardLayout
The CardLayout each component in a container as card. Only one component is visible at a
time.
It treats each component as a card that is why it is known as CardLayout.
Constructors
1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and
vertical gap.
Methods
public void next(Container parent): is used to flip to the next card of the given container.
public void previous(Container parent): is used to flip to the previous card of the given
container.
public void first(Container parent): is used to flip to the first card of the given container.
public void last(Container parent): is used to flip to the last card of the given container.
public void show(Container parent, String name): is used to flip to the specified card
with the given name.
New Card
Not:- in swing Content pane layer used to hold components to retrieve content pane
getContentPane() is called so that you can add components
- when the user interact with GUI components events are generated.
- For Example,
• Click on a Button
• Moving mouse
• Enter a character through keyboard
• Select an item from a list
• Scrolling the page.
1. Event source
Event classes and Listener classes are provided by package called java.awt.event
Every Event source generates an event named with Event class. Example event
generated by a Button is known as ActionEvent and event generated by
CheckBox is known as ItemEvent.
- a mouse is clicked
- a mouse is dragged
- a mouse is moved
- a mouse is entered
- a mouse is pressed
- a mouse is released
- a mouse is exited
This event class is available in java.awt.event.MouseEvent
package.
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;
import java.applet.*;
public class KeyEventEx extends Applet implements KeyListener{
String msg="";
public void init(){
addKeyListener(this);}
public void keyPressed(KeyEvent ke){
showStatus("key is pressed");}
public void keyReleased(KeyEvent ke){
showStatus("key is released");}
public void keyTyped(KeyEvent ke){
msg=msg+ke.getKeyChar();
repaint();// calls paint() explicitly and execute the updated method of component.
}
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString(msg,30,50);
}} Vigneswara Reddy,IT of Dept
ADAPTER CLASSES
An adapter class provides empty implementation of all the methods in event
listener interfaces.
Adapter class provides default modifications of all the methods in an interface.
Adapter classes are useful when the user want to implement only few methods
of an interface.
We can define a new class to act as an event listener by extending adapter
classes and implement only those events in which you are interested.
Ex MouseMotionListener interface has two methods
mouseDragged(MouseEvent e) and mouseMoved(MouseEvent e).
but the user is interested only in mouse dragged event . If we use adapter class
(MouseMotionAdapter) We only need to implement those methods which we
are interested.
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionLister
Windowadapter WindowListener
ComponentAdapter componentListener
ContainerAdapter ContainerListener
Used when the current window is not activated for a long time.
public void windowDeiconfied(WindowEvent we)
public class WindowAdapter implements WindowListner
{
}
https://www.tutorialspoint.com/swing/swing_controls.ht
m
http://www.javatpoint.com/java-swing
https://www.tutorialspoint.com/awt/awt_event_handling.
htm
http://www.javatpoint.com/event-handling-in-java
http://www.studytonight.com/java/event-handling-in-jav
a
http://way2java.com/awt-events/java-event-handling/
http://csis.pace.edu/~marchese/Cs396N/Lecture/L4/l4.ht
ml
http://vip.cs.utsa.edu/classes/java/tutorial/gridbaglayout.
html