Integrative Programming & Technology (Chapter 2.1 Swing Components)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

CHAPTER 2 – Part 1

Prepared by : Pn. Rosilawati Binti Mohamad


Learning Outcomes

2.1 Builds interface using swing


components

2.2 Organizes Layout Manager


2.1 Builds interface using swing
components

Identify Swing Construct Java


Components & programs using the
It’s Uses swing component
Introduction
 Java has a newer library for building graphical user
interfaces, known as “Swing.” Swing is more powerful
and sophisticated than the AWT.
 Swing is built around the existing AWT, so it helps to
understand the AWT first.
 Swing is similar enough to the AWT that it’s relatively
easy to switch if the need arises.
 Many Swing classes correspond to AWT classes. For
example, Swing’s JButton class corresponds to the
AWT’s Button class.
 The Swing classes provide greater compatibility across
different operating systems. They are fully
implemented in Java, and behave the same on
different operating systems.
Features of Java Swing
Swing Component Overview
Swing Component Hierarchy

 Container
 JComponent
AbstractButton
JButton
JMenuItem
JCheckBoxMenuItem
JMenu
JRadioButtonMenuItem
JToggleButton
JCheckBox
JRadioButton
Swing Component Hierarchy/2

 JComponent
 JComboBox
 JLabel
 JList
 JMenuBar
 JPanel
 JPopupMenu
 JScrollBar
 JScrollPane
Swing Component Hierarchy/3

 JComponent
 JTextComponent
JTextArea
JTextField
JPasswordField
JTextPane
JHTMLPane
More Components

 FontChooser  JRootPane
 JColorChooser
 JSeparator
 JDesktopIcon
 JSlider
 JDirectoryPane
 JSplitPane
 JFileChooser
 JTabbedPane
 JImagePreviewer
 JTable
 JInternalFrame
 JToolBar
 JLayeredPane
 JToolTip
 JDesktopPane
 JOptionPane  JTree
 JProgressBar  JViewport
Swing Packages

 javax.swing
 javax.swing.border
 javax.swing.event
 javax.accessibility
 javax.swing.colorchooser
 javax.swing.filechooser
 javax.swing.plaf
 javax.swing.table
 javax.swing.text.html
 javax.swing.tree
JFrames

 A JFrame is a Window with all of the adornments added.


 JFrame inherits from Frame, Window, Container,
Component, and Object
 A JFrame provides the basic building block for screen-
oriented applications.
JFrame win = new JFrame( “title” );
 Sizing a Frame
 You can specify the size
 Height and width given in pixels
 The size of a pixel will vary based on the resolution of
the device on which the frame is rendered
 Usually one uses an instance of the Dimension
class.
JFrames
 The method pack() will set the size of the frame
automatically, based on the size of the components
contained in the content pane:
 Note that pack does not look at the title bar
 Sometimes pack() does not work as you might
expect; try it and see.
 JFrames have several panes: Components are placed in the
Content Pane
Creating a Jframes – Example 1

import javax.swing.*;
public class SwingFrame {
public static void main( String args[] ) {
JFrame win = new JFrame( "My First GUI
Program" );
win.setVisible(true);
}
} // SwingFrame
Creating a JFrames – Example 2
import javax.swing.*;
import java.awt.*;

public class SwingFrame {


static Dimension windowSize = new Dimension( 250, 150 );

public static void main( String args[] ) {


JFrame win = new JFrame( "My First GUI Program" );
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
win.setSize( windowSize );
win.setVisible(true);
}
} // SwingFrame
The Content Pane of a JFrame

 The content pane is where we put GUI objects such as


buttons, labels, scroll bars, and others.
 We access the content pane by calling the frame’s
getContentPane method.

This gray area is


the content pane of
this frame.
Changing the Background Color

 Here's how we can change the background color of a


content pane to blue:

Container contentPane = getContentPane();


contentPane.setBackground(Color.BLUE);
JLabels

 JLabels are components that you can fill with text.


 When creating a label you can specify the initial value
and the alignment you wish to use within the label.
 You can use getText() and setText() to get and
change the value of the label.
lbl = new JLabel( ”text", JLabel.RIGHT );
JLabels - Example
import javax.swing.*;

public class SwingFrame {


public static void main( String args[] ) {
JFrame win = new JFrame("My First GUI
Program");
JLabel label = new JLabel( "Hello World" );
win.getContentPane().add( label );
win.setVisible(true);
}
} // SwingFrame
JButtons

 JButton extends Component , displays a string,


and delivers an ActionEvent for each mouse
click.
 Normally buttons are displayed with a border
 In addition to text, JButtons can also display
icons.
button = new JButton( ”text“ );
JButtons - Example
import javax.swing.*;

public class SwingFrame {


public static void main( String args[] ) {
JFrame win = new JFrame( "My First GUI Program"
);
JButton button = new JButton( "Click Me!!" );
win.getContentPane().add( button );
win.setVisible(true);
}
} // SwingFrame
JTextField – like a TextField
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JTextFieldExample extends JFrame{


public JTextFieldExample() {
JPanel panel = new JPanel();
JLabel label = new JLabel();
label.setText("TextField : ");
JTextField textField = new JTextField();
textField.setText("tutorialData.com");
panel.add(label);
panel.add(textField);
this.add(panel);
}
public static void main(String a[]) {
JTextFieldExample textFieldExample = new JTextFieldExample();
textFieldExample.setSize(250, 200);
textFieldExample.setTitle("tutorialData.com");
textFieldExample.setVisible(true);
}
}
JOptionPane – Message Dialog Window

 Another useful class for accepting user input, and


displaying results, is the JOptionPane class. This
is located in the javax.swing library.
 The JOptionPane class allows you to have input
boxes like this one:

> And message boxes


like this:
JOptionPane – Cont.

 The first thing to do is to reference the library we want


to use:
import javax.swing.JOptionPane;
 This tells java that we want to use the JOptionPane
class, located in the javax.swing library.
 JOptionPane method:

Method Name Description


showConfirmDialog Ask a confirmation question, like
Yes/No/Cancel
showInputDialog Prompt for some input from users
showMessageDialog Tell the users about something that has
happened
showOptionDialog The Grand Unification of the above three
JOptionPane – Cont.

 There are different kinds of icons for a dialog box, just replace the
last argument:
> JOptionPane.PLAIN_MESSAGE // this is a plain message
> JOptionPane.INFORMATION_MESSAGE // this is a info
message
> JOptionPane.ERROR_MESSAGE // this is a error message
> JOptionPane.WARNING_MESSAGE // this is a warning
message
 Example:
JOptionPane.showMessageDialog (
null, "Messagehere", "Title here", JOptionPane.PLAIN_MESSAGE);

 note: after using JOptionPane, dont forget to exit System.exit


JOptionPane – Example
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ShowMessageDialogExample1 {

public static void main(String[] args) {


String backupDir = "/Users/al/backups";
// create a jframe
JFrame frame = new JFrame("JOptionPane showMessageDialog
example");

// show a joptionpane dialog using showMessageDialog


JOptionPane.showMessageDialog(frame, "Problem writing to backup
directory: '" + backupDir + "'.");
System.exit(0);
}
}
AWT vs. Swing
AWT SWING
PROS 1. Speed: native peers speeds 1. Behavior: allows for a greater range of
component performance. behavior for Swing components since
2. Applet Portability: most Web they are not limited by the native peers
browsers support AWT classes so that AWT uses.
AWT applets can run without the 2. Features: Swing supports a wider range
Java plugin. of features like icons and pop-up tool-tips
3. Look and Feel: more closely reflect for components.
the look and feel of the OS they 3. Look and Feel: The pluggable look and
run on. feel automatically have the look and feel
of any OS platform
CONS 1. Portability: use of native peers 1. Applet Portability: Most Web browsers do
creates platform specific not include the Swing classes, so the
limitations. Some components may Java plugin must be used.
not function at all on some 2. Performance: Swing components are
platforms. generally slower and buggier than AWT,
2. Third Party Development: the due to both the fact that they are pure
majority of component makers, Java and to video issues on various
including Borland and Sun, base platforms.
new component development on 3. Look and Feel: Even when Swing
Swing components. components are set to use the look and
3. There is a much smaller set of AWT feel of the OS they are run on, they may
components available. not look like their native counterparts.
4. Features: AWT components do not
support features like icons and
tool-tips.
Lab Activity 2.1
Construct Java programs using
the swing component

You might also like