Displaying Information in a Component Core Java

In this section, we show you how to display information inside a frame. For example, rather than displaying “Not a Hello, World program” in text mode in a console window as we did before, we display the message in a frame.

A frame that displays information

A frame that displays information

You could draw the message string directly onto a frame, but that is not considered good programming practice. In Java, frames are really designed to be containers for components such as a menu bar and other user interface elements. You normally draw on another component which you add to the frame.

The structure of a JFrameis surprisingly complex. Look at Figure below, which shows the makeup of a JFrame. As you can see, four panes are layered in a JFrame. The root pane, layered 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 components into the content pane, using code such as the following:

Internal structure of a JFrame

Internal structure of a JFrame

Up to Java SE 1.4, the add method of the JFrameclass was defined to throw an exception with the message “Do not use JFrame.add(). Use JFrame.getContentPane().add() instead.” As of Java SE 5.0, the JFrame.addmethod has given up trying to reeducate programmers, and it simply calls add on the content pane.

Thus, as of Java SE 5.0, you can simply use the call

In our case, we want to add a single component to the frame onto which we will draw our message. To draw on a component, you define a class that extends JComponent and

override the paintComponentmethod in that class.

The paint Component method takes one parameter of type Graphics. A Graphics object remembers a collection of settings for drawing images and text, such as the font you set or the current color. All drawing in Java must go through a Graphics object. It has methods that draw patterns, images, and text.

NOTE: The Graphics parameter is similar to a device context in Windows or a graphics context in X11 programming.

Here’s how to make a component onto which you can draw:

Each time a window needs to be redrawn, no matter what the reason, the event handler notifies the component. This causes the paintComponentmethods of all components to be executed.

Never call the paintComponentmethod yourself. It is called automatically whenever a part of your application needs to be redrawn, and you should not interfere with this automatic process.

What sorts of actions trigger this automatic response? For example, painting occursbecause the user increased the size of the window or minimized and then restored the window. If the user popped up another window and it covered an existing window and then made the overlaid window disappear, the application window that was covered is now corrupted and will need to be repainted. (The graphics system does not save the pixels underneath.) And, of course, when the window is displayed for the first time, it needs to process the code that specifies how and where it should draw the initial elements.

TIP: If you need to force repainting of the screen, call the repaint method instead of paintComponent. The repaint method will cause paintComponentto be called for all components, with a properly configured Graphics object.

As you saw in the code fragment above, the paintComponentmethod takes a single parameter of type Graphics. Measurement on a Graphics object for screen display is done in pixels.

The (0, 0) coordinate denotes the top-left corner of the component on whose surface you are drawing.

Displaying text is considered a special kind of drawing. The Graphics class has a drawstring method that has the following syntax:

In our case, we want to draw the string "Not a Hello, World Program" in our original window, roughly one -quarter of the way across and halfway down. Although we don’t yet know how to measure the size of the string, we’ll start the string at coordinates (75, 100). This means the first character in the string will start at a position 75 pixels tothe right and 100 pixels down. (Actually, it is the baseline for the text that is 100 pixels

down —see page 313 for more on how text is measured.) Thus, our paintComponentmethod looks like this:

NotHelloWorld.java

NOTE: Instead of extending JComponent, some programmers prefer to extend the JPanelclass. A JPanelis intended to be a container that can contain other components, but it isalso possible to paint on it. There is just one difference. A panel is opaque, which meansthat it is responsible for painting all pixels within its bounds. The easiest way to achievethat is to paint the panel with the background color, by calling super.paintComponentin the paint Component method of each panel subclass:

javax.swing.JFrame

  • Container getContentPane()
    returns the content pane object for this JFrame.
  • Component add(Component c)
    adds and returns the given component to the content pane of this frame. (Before
  • Java SE 5.0, this method threw an exception.)

  • void repaint()
    causes a repaint of the component “as soon as possible.”

javax.swing.JComponent

  • public void repaint(int x, int y, int width, int height)
    causes a repaint of a part of the component “as soon as possible.”
  • void paintComponent(Graphics g)
    overrides this method to describe how your component needs to be painted.


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics