Page 754 - Introduction to Programming with Java: A Problem Solving Approach
P. 754

                720
Chapter 17 GUI Programming—Component Layout, Additional GUI Components
public void setText(String text)
Assigns the text area’s text.
public void setEditable(boolean flag)
Makes the text box editable or non-editable.
public void setLineWrap(boolean flag)
Turns line wrap on or off.
public void setWrapStyleWord(boolean flag)
Specifies whether word boundaries are used for line wrapping.
JTextArea components are editable by default, which means users are allowed to type inside them. If you want to prevent users from editing a JTextArea component, call setEditable with an argument value of false. Doing so prevents users from updating the text area, but it does not prevent programmers from updating the text area. Programmers can call the setText method regardless of whether the text area is editable or non-editable.
JTextArea components have line wrap turned off by default. Normally, you’ll want to turn line wrap on by calling setLineWrap(true). That way, long lines automatically wrap to the next row, instead of disappearing when they reach the text area’s right boundary.
For JTextArea components with line wrap turned on, the default is to perform line wrap at the point
where the text meets the text area’s right boundary, regardless of whether that point is in the middle of a
  word. Normally, you’ll want to avoid that draconian3 default behavior and have line wrap occur only at word
 Fine
tune with other API methods.
background color is appropriate, but if you want it to blend in, then it’s inappropriate. How can you change its background color so that it blends in? More specifically, how can you change the code so that the window looks like Figure 17.12? The solution requires the use of a few methods not mentioned in the above API method list. But we’ve used the methods in the past for other GUI needs. Try to figure this out on your own before reading on.
Apago PDF Enhancer
boundaries. To change to a word-boundary line-wrap policy, call setWrapStyleWord(true). License-Agreement Example
Look back at the license-agreement JText Area component in Figure 7.10. Figure 17.11 contains the code associated with that component. Let’s now examine Figure 17.11’s code. Note the \n\n in the JTextArea constructor call. As you might recall, \n’s are ignored inside JLabel text. But they work fine inside JTextArea text. Note the setEditable(false), setLineWrap(true), and setWrapStyleWord(true) calls. Those calls are common for JTextArea components.
Take a look at the background color for the license-agreement JTextArea component in Figure 17.10. It’s white. That’s in contrast to the rest of the window. If you want your text area to stand out, then the white
    To change a component’s background color, call setBackground(<color>). For our license-agree- ment component, we want its color to match the window’s color, so we need to call setBackground with a color value equal to the window’s background color. To get the window’s background color, call getContentPane().getBackground(). Here’s the solution:
license.setBackground(getContentPane().getBackground());
3 A draconian policy is a policy that is harsh or severe. Draconion comes from Draco, a 7th century B.C. government official from Athens who was in charge of codifying local law. Draco’s laws were exceedingly severe. For example, even minor offenses were punishable by the death penalty.
 









































































   752   753   754   755   756