19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

646 Chapter 17 GUI Components<br />

wrap line<br />

wrap word<br />

read only<br />

scroll pane<br />

17 // Set the font in the label and the text field<br />

18 jlblImageTitle.setFont(new Font("SansSerif", Font.BOLD, 16));<br />

19 jtaDescription.setFont(new Font("Serif", Font.PLAIN, 14));<br />

20<br />

21 // Set lineWrap and wrapStyleWord true for the text area<br />

22<br />

23<br />

24<br />

25<br />

jtaDescription.setLineWrap(true);<br />

jtaDescription.setWrapStyleWord(true);<br />

jtaDescription.setEditable(false);<br />

26 // Create a scroll pane <strong>to</strong> hold the text area<br />

27 JScrollPane scrollPane = new JScrollPane(jtaDescription);<br />

28<br />

29 // Set BorderLayout for the panel, add label and scroll pane<br />

30 setLayout(new BorderLayout(5, 5));<br />

31 add(scrollPane, BorderLayout.CENTER);<br />

32 add(jlblImageTitle, BorderLayout.WEST);<br />

33 }<br />

34<br />

35 /** Set the title */<br />

36 public void setTitle(String title) {<br />

37 jlblImageTitle.setText(title);<br />

38 }<br />

39<br />

40 /** Set the image icon */<br />

41 public void setImageIcon(ImageIcon icon) {<br />

42 jlblImageTitle.setIcon(icon);<br />

43 }<br />

44<br />

45 /** Set the text description */<br />

46 public void setDescription(String text) {<br />

47 jtaDescription.setText(text);<br />

48 }<br />

49 }<br />

The text area is inside a JScrollPane (line 27), which provides scrolling functions for<br />

the text area. Scroll bars au<strong>to</strong>matically appear if there is more text than the physical size of the<br />

text area.<br />

The lineWrap property is set <strong>to</strong> true (line 22) so that the line is au<strong>to</strong>matically wrapped<br />

when the text cannot fit in one line. The wrapStyleWord property is set <strong>to</strong> true (line 23) so<br />

that the line is wrapped on words rather than characters. The text area is set as noneditable<br />

(line 24), so you cannot edit the description in the text area.<br />

It is not necessary <strong>to</strong> create a separate class for DescriptionPanel in this example.<br />

However, this class was created for reuse in the next section, where you will use it <strong>to</strong> display<br />

a description panel for various images.<br />

create descriptionPanel<br />

create frame<br />

LISTING 17.3<br />

TextAreaDemo.java<br />

1 import java.awt.*;<br />

2 import javax.swing.*;<br />

3<br />

4 public class TextAreaDemo extends JFrame {<br />

5 // Declare and create a description panel<br />

6 private DescriptionPanel descriptionPanel = new DescriptionPanel();<br />

7<br />

8 public static void main(String[] args) {<br />

9 TextAreaDemo frame = new TextAreaDemo();<br />

10 frame.pack();<br />

11 frame.setLocationRelativeTo(null); // Center the frame

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!