Adavanced Java 1
Adavanced Java 1
(AJP-22517)
AKUDE P.A.
Chapter 1&2
Introduction to Abstract
Windowing Toolkit(AWT) & Swing
•
Here are the constructors defined by BorderLayout:
BorderLayout( ):- creates a default border layout.
BorderLayout(int horz, int vert):- specify
the horizontal and vertical space left between
components in horz and vert, respectively
BorderLayout defines the following constants that specify the regions:
BorderLayout.CENTER ,BorderLayout.SOUTH,BorderLayout.EAST ,BorderLayout.WEST,
BorderLayout.NORTH.
• When adding components, you will use these constants with the following form
of add( ), which is defined by Container:
void add(Component compObj, Object region)
Here, compObj is the component to be added, and region specifies where the component
will be added.
}
15/03/2024 Akude P.A. 40
GridLayout
The GridLayout is used to arrange the components
component is displayed in each rectangle.
in rectangular grid. One
GridLayout lays out components in a two-dimensional grid.
you define the number of rows and columns.
The constructors supported by GridLayout are shown here:
GridLayout( )- creates a single-column grid layout
GridLayout(int numRows, int
numColumns)- creates a grid layout with the specified
number of rows and columns
GridLayout(int numRows, int numColumns, int horz, int vert)- allows you to
specify the horizontal
and vertical space left between components in horz and
vert, respectively
Either numRows or numColumns can be zero. Specifying numRows as zero
allows for unlimited-length
columns. Specifying numColumns as zero allows
for unlimited-length rows.
CardLayout cl;
Button Win, Other;
public void init() {
Win = new Button("Windows");
Other = new Button("Other");
add(Win);
add(Other);
cl = new CardLayout();
p = new Panel();
p.setLayout(cl); // set panel layout to card layout
15/03/2024 Akude P.A. 48
winXP = new Checkbox("Windows XP", null, true);
winVista = new Checkbox("Windows Vista");
solaris = new Checkbox("Solaris"); mac = new
Checkbox("Mac OS");
Panel p1 = new Panel();
// add Windows check boxes to a
panel p1.add(winXP);
p1.add(winVista);
// add other OS check boxes to a panel
Panel p2 = new Panel();
p2.add(solaris);
p2.add(mac);
// add panels to card deck panel
p.add(p1, "Windows");
p.add(p2, "Other");
// add cards to main applet
panel add(p);
// register to receive action events
Win.addActionListener(this);
15/03/2024 Akude P.A. 49
Other.addActionListener(this); }
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == Win) {
cl.show(p, "Windows");
}
else {
cl.show(p, "Other");
}}}
}
// Display current state of the check
boxes. public void paint(Graphics g) {
msg = "Current state: ";
g.drawString(msg, 6, 80);
msg = " Windows XP: " + winXP.getState();
g.drawString(msg, 6, 100);
msg = " Windows Vista: " + winVista.getState();
g.drawString(msg, 6, 120);
msg = " Solaris: " + solaris.getState();
g.drawString(msg, 6, 140);
msg = " Mac: " + mac.getState();
g.drawString(msg, 6, 160);
}
}
• It is built on the top of AWT API and all objects in Swing are derived
from AWT, and most objects in Swing start with the letter J.
• Java Swing is a part of Java Foundation Classes (JFC) that is used to
create window-based applications.
• Swing API is a set of extensible GUI Components to create
JAVA based Front End/GUI Applications.
• Java Swing is also known as Java GUI widget toolkit.
• Unlike AWT, Java Swing provides platform-independent
and lightweight components.
• Swing follow MVC architecture.
• Package: Import javax.swing.*;
• Pluggable Look and Feel: Swings supports pluggable look and feel i.e.,
the appearance of a component can be separated from how the
component behaves.
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
tree.setShowsRootHandles(true);
tree.setRootVisible(false);
add(tree);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTree Example"); this.setSize(200, 200);
this.setVisible(true);
}