2. Difference between AWT and Swing
AWT Swing
platform-dependent. platform-independent
heavyweight. lightweight.
doesn't support pluggable look supports pluggable look and
feel. and feel.
provides less components than provides more powerful
Swing. components such as
tables, lists, scrollpanes,
colorchooser,tabbedpane.
2
3. Methods
public void add(Component c)
public void setSize(int width,int height)
public void setLayout(LayoutManager m)
public void setVisible(boolean b)
3
4. JButton class
Constructors:
JButton()
JButton(String s)
JButton(Icon i)
Methods
1) public void setText(String s)
2) public String getText()
3) public void setEnabled(boolean b)
4) public void setIcon(Icon b)
5) public Icon getIcon()
6) public void setMnemonic(int a)
7) public void addActionListener(ActionListener a)
4
5. JRadioButton class
Constructors
JRadioButton()
JRadioButton(String s)
JRadioButton(String s, boolean selected)
Methods
1) public void setText(String s)
2) public String getText()
3) public void setEnabled(boolean b)
4) public void setIcon(Icon b)
5) public Icon getIcon()
6) public void setMnemonic(int a)
7) public void addActionListener(ActionListener a)
5
6. JTextArea class
Constructors
JTextArea():
JTextArea(String s)
JTextArea(int row, int column)
JTextArea(String s, int row, int column)
Methods
1) public void setRows(int rows)
2) public void setColumns(int cols)
3) public void setFont(Font f)
4) public void insert(String s, int position)
5) public void append(String s)
6
8. Graphics class
Methods
public abstract void drawString(String str, int x, int y)
is used to draw the specified string.
public void drawRect(int x, int y, int width, int height)
draws a rectangle with the specified width and height.
public abstract void fillRect(int x, int y, int width, int height)
is used to fill rectangle with the default color and specified width and
height.
public abstract void drawOval(int x, int y, int width, int height)
is used to draw oval with the specified width and height.
public abstract void setFont(Font font)
is used to set the graphics current font to the specified font
8
9. Methods
public abstract void fillOval(int x, int y, int width, int height)
is used to fill oval with the default color and specified width and
height.
public abstract void drawLine(int x1, int y1, int x2, int y2)
is used to draw line between the points(x1, y1) and (x2, y2).
public abstract void drawArc(int x, int y, int width, int height,
int startAngle, int arcAngle)
is used draw a circular or elliptical arc.
public abstract void fillArc(int x, int y, int width, int height, int
startAngle, int arcAngle)
is used to fill a circular or elliptical arc.
public abstract void setColor(Color c)
is used to set the graphics current color to the specified color. 9
10. Methods
The following three methods are involved in displaying graphics.
Default versions of each are provided by class Component.
Methods update() and paint() should be redefined to perform the
desired graphics operations.
repaint()
The repaint() method requests that a component be repainted.
public void repaint()
public void repaint(long tm)
public void repaint(int x, int y, int w, int h)
public void repaint(long tm, int x, int y, int w, int h)
10
11. update()
public void update(Graphics g)
The update() method is called in response to a repaint() request,
or in response to a portion of the component being uncovered or
displayed for the first time.
paint()
public void paint(Graphics g)
The paint() method is called from an update() method, and is responsible
for actually drawing the graphics.
11
12. JComboBox class
Constructors
JComboBox()
JComboBox(Object[] items)
methods
1)public void addItem(Object anObject):
is used to add an item to the item list.
2) public void removeItem(Object anObject)
is used to delete an item to the item list.
3) public void removeAllItems()
is used to remove all the items from the list.
4) public void setEditable(boolean b)
is used to determine whether the JComboBox is editable.
5) public void addActionListener(ActionListener a)
is used to add the ActionListener.
6) public void addItemListener(ItemListener i)
is used to add the ItemListener.
12
13. JColorChooser class
Constructors
JColorChooser()
is used to create a color chooser pane with white color initially.
JColorChooser(Color initialColor)
is used to create a color chooser pane with the specified color initially.
Method
public static Color showDialog(Component c, String title, Color
initialColor)
is used to show the color-chooser dialog box.
13
14. JProgressBar class
Constructors
JProgressBar()
is used to create a horizontal progress bar but no string text.
JProgressBar(int min, int max)
is used to create a horizontal progress bar with the specified minimum
and maximum value.
JProgressBar(int orient)
is used to create a progress bar with the specified orientation, it can be
either Vertical or Horizontal by using SwingConstants.VERTICAL and
SwingConstants.HORIZONTAL constants.
JProgressBar(int orient, int min, int max)
is used to create a progress bar with the specified orientation, minimum
and maximum value 14
15. Methods
1)public void setStringPainted(boolean b):
is used to determine whether string should be displayed.
2) public void setString(String s)
is used to set value to the progress string.
3) public void setOrientation(int orientation)
is used to set the orientation, it may be either vertical or horizontal
by using SwingConstants. VERTICAL and
SwingConstants.HORIZONTAL constants..
4) public void setValue(int value)
is used to set the current value on the progress bar
15
16. JSlider class
Constructors
JSlider()
creates a slider with the initial value of 50 and range of 0 to 100.
JSlider(int orientation)
creates a slider with the specified orientation set by either
JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100
and initial value 50.
JSlider(int min, int max)
creates a horizontal slider using the given min and max.
JSlider(int min, int max, int value): creates a horizontal slider using the
given min, max and value.
JSlider(int orientation, int min, int max, int value): creates a slider using
the given orientation, min, max and value 16
17. Methods
1)public void setMinorTickSpacing(int n):
is used to set the minor tick spacing to the slider.
2) public void setMajorTickSpacing(int n)
is used to set the major tick spacing to the slider.
3) public void setPaintTicks(boolean b)
is used to determine whether tick marks are painted
4) public void setPaintLabels(boolean b)
is used to determine whether labels are painted.
5) public void setPaintTracks(boolean b)
is used to determine whether track is painted.
17
19. 19
Roots in MVC Swing architecture is rooted in the model-view-controller
( MVC) design that dates back to SmallTalk. MVC architecture calls for
a visual application to be broken up into three separate parts:
• A model that represents the data for the application.
• The view that is the visual representation of that data.
• A controller that takes user input on the view and translates that to
changes in the model.
20. 20
Menus
A menu provides a space-saving way to let the user choose one of
several options. Other components with which the user can make a one-
of-many choice include combo boxes, lists, radio buttons, spinners,
and tool bars.
Creating menu bars
To create a menu bar:
add a JMenuBar to a JFrame
add JMenus to the JMenuBar and
add JMenuItems to the JMenus
27. 27
Pluggable Look and Feel
The Swing toolkit allows you to decide how to configure the particular
look and feel of your application. If you don't specify a look and feel, the
Swing UI manager figures out which one to use.
Leave it up to the Swing UI manager. If a particular look and feel is not
specified by the program, Swing's UI manager checks whether the user
has specified a preference. If that preference hasn't been specified or isn't
available, the default look and feel is used.
The default look and feel is determined by the supplier of the JRE. For
the JRE that Sun provides, the Java look and feel (called Metal) is used.
The Java look and feel works on all platforms.
28. 28
Use the look and feel of the native platform. If the application is
running on a Microsoft Windows XP machine, the Windows look and
feel is used. On Mac OS platforms, the Aqua look and feel is used. On
UNIX platforms, such as Solaris or Linux, either the GTK+ look and feel
or the CDE/Motif look and feel is used, depending on the user's desktop
choice.
Specify a particular look and feel. Swing ships with four look and
feels: Java (also called Metal), Microsoft Windows, GTK+, and
CDE/Motif. The GTK+ look and feel requires a theme, and there are
many available for free on the Internet.
Create your own look and feel using the Synth package.
Use an externally provided look and feel.