Saturday, January 5, 2013

Core Java - Swing

Java SE 5.0 introduced a look and feel, called Synth, that makes this process easier. In
Synth, you can define a new look and feel by providing image files and XML descriptors,
without doing any programming.
A top-level window (that is, a window that is not contained inside another window) is
called a frame in Java.
The Swing version of this class is called JFrame and extends the Frame class. The JFrame is one of the few Swing components that is not painted on a canvas. Thus, the decorations (but-tons, title bar, icons, and so on) are drawn by the user’s windowing system, not by
Swing. The Swing library draws everything inside the frame.
you should check the resolution of the user ’s screen and write code that resizes the
frames accordingly
If your frame contains only standard components such as buttons and text fields,
you can simply call the pack method to set the frame size. The frame will be set to the
smallest size that contains all components. It is quite common to set the main frame
of a program to the maximum size. As of Java SE 1.4, you can simply maximize a
frame by calling frame.setExtendedState(Frame.MAXIMIZED_BOTH);
If you write an application that takes advantage of multiple display screens, use the
GraphicsEnvironment and GraphicsDevice classes to find the dimensions of the display
screens. The GraphicsDevice class also lets you execute your application in full-screen mode.

four panes are layered in a JFrame. The root pane, lay-ered pane, and glass pane are of no interest to us; they are required to organize the
menu bar and content pane and to implement the look and feel. The part that most con-cerns Swing programmers is the content pane. When designing a frame, you add compo-nents into the content pane. as of Java SE 5.0, you can simply use the call frame.add(c);
Instead of extending JComponent, some programmers prefer to extend the JPanel
class. A JPanelis intended to be a containerthat can contain other components, but it is
also possible to paint on it. There is just one difference. A panel is opaque, which means
that it is responsible for painting all pixels within its bounds. The easiest way to achieve
that is to paint the panel with the background color, by calling super.paintComponentin the
paintComponentmethod of each panel subclass
Swing programs should override paintComponent() instead of overriding paint().

If you use a Graphics object instead of a Graphics2D object, you need to use the setColor method to set colors instead of setPaint

In Java, all event objects ultimately derive from
the class java.util.EventObject. Of course, there are subclasses for each event type, such as
ActionEvent and WindowEvent.
To sum up, here’s an overview of how event handling in the AWT works:
• A listener object is an instance of a class that implements a special interface called
(naturally enough) a listener interface.
• An event source is an object that can register listener objects and send them event
objects.
The event source sends out event objects to all registered listeners when that event
occurs.
• The listener objects will then use the information in the event object to determine
their reaction to the event.
Note that the Metal look and feel is located in the javax.swing package. The other look-and-feel packages are located in the com.sun.java package and need not be present in
every Java implementation.
Mnemonic
Here are the most commonly used semantic event classes in the java.awt.eventpackage:
• ActionEvent(for a button click, a menu selection, selecting a list item, or ENTERtyped
in a text field)
• AdjustmentEvent(the user adjusted a scrollbar)
• ItemEvent(the user made a selection from a set of checkbox or list items)
Implement three separate classes:
•The model,which stores the content
•The view,which displays the content
•The controller,which handles user input
The controller handles the user-input events such as mouse clicks and keystrokes. It
then decides whether to translate these events into changes in the model or the view.
Models are easy to separate, and
each user interface component has a model class. But the responsibilities of the view
and controller are not always clearly separated and are distributed over a number of dif-ferent classes.
After changing the size of a text box with the setColumnsmethod, call the revalidate
method of the surrounding container.
textField.setColumns(10);
panel.revalidate();
Therevalidatemethod recomputes the size and layout of all components in a container.
After you use the revalidatemethod, the layout manager resizes the container, and the
changed size of the text field will be visible.
Therevalidatemethod belongs to the JComponentclass. It doesn’t immediately resize the
component but merely marks it for resizing. This approach avoids repetitive calculations
if multiple components request to be resized. However, if you want to recompute all
components inside aJFrame, you have to call the validatemethod—JFramedoesn’t extend
JComponent.










No comments:

Post a Comment