Notes on Java Swing
Components
What is Swings in java ?
- A
part of The JFC
- Swing
Java consists
of
Look and feel
Accessibility
Java 2D
Drag and Drop, etc
Swing is built on top of
AWT and is entirely written in Java, using AWT’s lightweight
component support. In particular, unlike AWT, t he architecture of Swing
components makes it easy to customize both their appearance and behavior.
Components from AWT and Swing can be mixed, allowing you to add Swing support
to existing AWT-based programs. For example, swing components such as JSlider,
JButton and JCheckbox could be used in the same program with standard AWT
labels, textfields and scrollbars. You could subclass the existing Swing UI,
model, or change listener classes without having to reinvent the entire
implementation. Swing also has the ability to replace these objects on-the-fly.
|
Swing Components
1. JFrame
The
components added to the frame are
referred to as its contents; these are managed by the contentPane. To add a component to a JFrame, we must use its contentPane
instead.JFrame is a Window with border,
title and buttons. When JFrame is set visible, an eventdispatching thread is started. JFrame objects store several objects
including a Container object known as the content pane. To add a component to a JFrame, add it to the content pane.
By default, a Jframe is
displayed in the upper-left corner of the screen. To display a frame
at a specified location, you can use the setLocation(x, y) method in the JFrame class. This
method places the upper-left corner of a frame at location (x, y).
at a specified location, you can use the setLocation(x, y) method in the JFrame class. This
method places the upper-left corner of a frame at location (x, y).
The Swing API keeps improving with abstractions such as the
setDefaultCloseOperation(EXIT_ON_CLOSE) method for the JFrame
JFrame Constructors
JFrame()
Constructs a new frame that is initially invisible.
Constructs a new frame that is initially invisible.
JFrame(GraphicsConfiguration gc)
Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
JFrame(String title)
Creates a new, initially invisible Frame with the specified title.
Creates a new, initially invisible Frame with the specified title.
JFrame(String title, GraphicsConfiguration gc)
Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.
Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.
2. JPanel
It is Swing’s version of the AWT class Panel and
uses the same default layout, FlowLayout. JPanel is descended directly from
JComponent.
3. JInternalFrame
A JInternalFrame is confined to a visible area of a container it is
placed in. JInternalFrame a top level swing component that has a contentpane.
- It can be iconified — in this case the icon
remains in the main application container.
- It can be maximized — Frame consumes the main application
- It can be closed using standard popup window controls
- It can be layered
JInternalFrame
Constructor
JInternalFrame()
Creates a non-resizable, non-closable, non-maximizable, non-iconifiable JInternalFrame with no title.
JInternalFrame(String title)
Creates a non-resizable, non-closable, non-maximizable, non-iconifiable JInternalFrame with the specified title.
Creates a non-resizable, non-closable, non-maximizable, non-iconifiable JInternalFrame with no title.
JInternalFrame(String title)
Creates a non-resizable, non-closable, non-maximizable, non-iconifiable JInternalFrame with the specified title.
JInternalFrame(String title,
boolean resizable)
Creates a non-closable, non-maximizable, non-iconifiable JInternalFrame with the specified title and resizability.
Creates a non-closable, non-maximizable, non-iconifiable JInternalFrame with the specified title and resizability.
JInternalFrame(String title,
boolean resizable, boolean closable)
Creates a non-maximizable, non-iconifiable JInternalFrame with the specified title, resizability, and closability.
Creates a non-maximizable, non-iconifiable JInternalFrame with the specified title, resizability, and closability.
JInternalFrame(String title, boolean
resizable, boolean closable, boolean maximizable)
Creates a non-iconifiable JInternalFrame with the specified title, resizability, closability, and maximizability.
Creates a non-iconifiable JInternalFrame with the specified title, resizability, closability, and maximizability.
JInternalFrame(String title,
boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)
Creates a JInternalFrame with the specified title, resizability, closability, maximizability
Creates a JInternalFrame with the specified title, resizability, closability, maximizability
4. JLabels
JLabel,
descended from JComponent, is used to create text labels. A JLabel object
provides text instructions or information on a GUI — display a single line of read-only text, an image or both text and image. We use a
Swing JLabel when we need a user interface component that displays a message or an image.
JLabel Constructor
JLabel()
Creates a JLabel instance with no image and with an empty string for the title.
Creates a JLabel instance with no image and with an empty string for the title.
JLabel(Icon image)
Creates a JLabel instance with the specified image.
Creates a JLabel instance with the specified image.
JLabel(Icon image, int
horizontalAlignment)
Creates a JLabel instance with the specified image and horizontal alignment.
Creates a JLabel instance with the specified image and horizontal alignment.
JLabel(String text)
Creates a JLabel instance with the specified text.
Creates a JLabel instance with the specified text.
JLabel(String text, Icon icon,
int horizontalAlignment)
Creates a JLabel instance with the specified text, image, and horizontal alignment.
Creates a JLabel instance with the specified text, image, and horizontal alignment.
JLabel(String text, int
horizontalAlignment)
Creates a JLabel instance with the specified text and horizontal alignment.
Creates a JLabel instance with the specified text and horizontal alignment.
5. JTextField
JTextField allows
editing/displaying of a single line of
text. New features include the ability to justify the text left, right, or
center, and to set the text’s font. When the user types data into them and presses the
Enter key, an action event occurs.
If the program registers an
event listener, the listener processes the event and can use the data in the text field
at the time of the event in the program. JTextField is an input area
where the user can type in characters. If you want to
let the user enter multiple lines
of text, you cannot use JTextfield’s unless you create several of them. The
solution is to use JTextArea, which enables the
user to enter multiple lines of
text.
JTextField Constructor
JTextField()
Constructs a new TextField.
Constructs a new TextField.
JTextField(Document doc, String text, int columns)
Constructs a new JTextField that uses the given text storage model and the given number of columns.
Constructs a new JTextField that uses the given text storage model and the given number of columns.
JTextField(int columns)
Constructs a new empty TextField with the specified n
umber of columns.
Constructs a new empty TextField with the specified n
umber of columns.
JTextField(String text)
Constructs a new TextField initialized with the specified text.
Constructs a new TextField initialized with the specified text.
JTextField(String text, int
columns)
Constructs a new TextField initialized with the specified text and columns.
Constructs a new TextField initialized with the specified text and columns.
6. JPasswordField
JPasswordField (a direct
subclass of JTextField) you can suppress the display of input. Each character entered can be replaced by an echo character.
This allows confidential input for passwords, for example. By default, the echo character
is the asterisk,
*. When the user types data into them and presses the
Enter key, an action event occurs. If the program registers an
event listener, the
listener processes the
event and can use the data in the text field at the time of the event in the
program. If you need to provide an editable text field that doesn’t show the
characters the user types – use the JPasswordField class.
JPasswordField
Constructor
JPasswordField()
Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.
Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.
JPasswordField(Document doc,
String txt, int columns)
Constructs a new JPasswordField that uses the given text storage model and the given number of columns.
Constructs a new JPasswordField that uses the given text storage model and the given number of columns.
JPasswordField(int columns)
Constructs a new empty JPasswordField with the specified number of columns.
Constructs a new empty JPasswordField with the specified number of columns.
JPasswordField(String text)
Constructs a new JPasswordField initialized with the specified text.
Constructs a new JPasswordField initialized with the specified text.
JPasswordField(String text, int
columns)
Constructs a new JPasswordField initialized with the specified text and columns.
Constructs a new JPasswordField initialized with the specified text and columns.
7. JTextArea
J. JTextArea allows editing of multiple
lines of text. JTextArea can be used in conjunction with class JScrollPane to achieve scrolling. The
underlying JScrollPane can be forced to always or never have either the vertical or horizontal scrollbar.
JTextArea Constructor
JTextArea()
Constructs a new TextArea.
Constructs a new TextArea.
JTextArea(Document doc)
Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).
Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).
JTextArea(Document doc, String text, int rows, int columns)
Constructs a new JTextArea with the specified number of rows and columns, and the given model.
Constructs a new JTextArea with the specified number of rows and columns, and the given model.
JTextArea(int rows, int
columns)
Constructs a new empty TextArea with the specified number of rows and columns.
Constructs a new empty TextArea with the specified number of rows and columns.
JTextArea(String text)
Constructs a new TextArea with the specified text displayed.
Constructs a new TextArea with the specified text displayed.
JTextArea(String text, int rows, int columns)
Constructs a new TextArea with the specified text and number of rows and columns.
Constructs a new TextArea with the specified text and number of rows and columns.
8. JButton
The abstract class AbstractButton extends class Component and provides a foundation for a family of button classes,
including JButton. A button is a component the user clicks
to trigger a specific action.
There are several types of buttons in
Java, all are subclasses of AbstractButton.
- command
buttons: is
created with class JButton. It generates ActionEvent.
- toggle
buttons: have
on/off or true/false values.
- check boxes: a
group of buttons. It generates ItemEvent.
- radio buttons: a
group of buttons in which only one can be selected. It generates
ItemEvent.
JButton Constructor
JButton()
Creates a button with no set text or icon.
Creates a button with no set text or icon.
JButton(Action a)
Creates a button where properties are taken from the Action supplied.
Creates a button where properties are taken from the Action supplied.
JButton(Icon icon)
Creates a button with an icon.
Creates a button with an icon.
JButton(String text)
Creates a button with text.
Creates a button with text.
JButton(String text, Icon icon)
Creates a button with initial text and an icon.
Creates a button with initial text and an icon.
9. JCheckBox
JCheckBox is not a member of a checkbox
group. A checkbox can be selected and deselected, and it also displays its
current state.
10.
JComboBox
JComboBox is like a drop down box — you can click a drop-down arrow and
select an option from a list. It generates ItemEvent. For example,
when the component has focus, pressing a key that corresponds to the first
character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists.
JComboBox Constructor
JComboBox()
Creates a JComboBox with a default data model.
Creates a JComboBox with a default data model.
JComboBox(ComboBoxModel aModel)
Creates a JComboBox that takes it’s items from an existing ComboBoxModel.
Creates a JComboBox that takes it’s items from an existing ComboBoxModel.
JComboBox(Object[] items)
Creates a JComboBox that contains the elements in the specified array.
Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector items)
Creates a JComboBox that contains the elements in the specified Vector.
Creates a JComboBox that contains the elements in the specified Vector.
11.
JList
JList provides a scrollable set
of items from which one or more may be selected. JList can be populated from an Array or Vector.
JList does not support scrolling directly—instead, the list must be associated with a scrollpane.
The view port used by the scrollpane can also have a user-defined border. JList
actions are handled using ListSelectionListener.
JList
Constructor
JList()
Constructs a JList with an empty model.
Constructs a JList with an empty model.
JList(ListModel dataModel)
Constructs a JList that displays the elements in the specified, non-null model.
Constructs a JList that displays the elements in the specified, non-null model.
JList(Object[] listData)
Constructs a JList that displays the elements in the specified array.
Constructs a JList that displays the elements in the specified array.
JList(Vector listData)
Constructs a JList that displays the elements in the specified Vector.
Constructs a JList that displays the elements in the specified Vector.






0 comments:
Post a Comment