Creating a GUI with JFC/Swing
Lesson: Using Swing Components
Answers: Using Swing Components

Use the information in this lesson and the component components/componentlisthow-to sections to help you complete these questions and exercises.

Questions

Question 1: Find the component that best fits each of the following needs. Write down both the component&146;s common name (such as &147;frame&148;) and find the component's how-to page online. [Hint: You can use components/componentsA Visual Index to the Swing Components to help you answer this question.]

Question 1a: A component that lets the user pick a color.
Answer 1a: http://java.sun/docs/books/tutorial/uiswing/components/colorchoosercolor chooser

Question 1b: A component that displays an icon, but that doesn&146;t react to user clicks.
Answer 1b: http://java.sun/docs/books/tutorial/uiswing/components/labellabel

Question 1c: A component that looks like a button and that, when pressed, brings up a menu of items for the user to choose from.
Answer 1c: http://java.sun/docs/books/tutorial/uiswing/components/comboboxuneditable combo box

Question 1d: A container that looks like a frame, but that appears (usually with other, similar containers) within a real frame.
Answer 1d: http://java.sun/docs/books/tutorial/uiswing/components/internalframeinternal frame

Question 1e: A container that lets the user determine how two components share a limited amount of space.
Answer 1e: http://java.sun/docs/books/tutorial/uiswing/components/splitpanesplit pane

Question 2: Which method do you use to add a menu bar to a top-level container such as a JFrame?
Answer 2: http://java.sun/j2se/1.4.2/docs/api/javax/swing/JFramesetJMenuBar(javax.swing.JMenuBar)setJMenuBar

Question 3: Which method do you use to specify the button for a top-level container such as a JFrame or JDialog?
Answer 3: JRootPane's http://java.sun/j2se/1.4.2/docs/api/javax/swing/JRootPanesetButton(javax.swing.JButton)setButton method. (You get the top-level container's root pane using the http://java.sun/j2se/1.4.2/docs/api/javax/swing/RootPaneContainergetRootPane()getRootPane method defined by the RootPaneContainer interface, which every top-level container implements.)

Question 4: Which method do you use to enable and disable components such as JButtons? What class is it defined in?
Answer 4: http://java.sun/j2se/1.4.2/docs/api/java/awt/ComponentsetEnabled(boolean)setEnabled, which is defined in the Component class

Question 5a: Which Swing components use http://java.sun/j2se/1.4.2/docs/api/javax/swing/ListSelectionModelListSelectionModel? [Hint: The &147;Use&148; link at the top of the specification for each interface and class takes you to a page showing where in the API that interface or class is referenced.]
Answer 5a: http://java.sun/j2se/1.4.2/docs/api/javax/swing/JListJList and http://java.sun/j2se/1.4.2/docs/api/javax/swing/JTableJTable

Question 5b: Do those components use any other models to handle other aspects of the components&146; state? If so, list the other models&146; types.
Answer 5b: JList also uses a ListModel, which holds the list's data. JTable uses a TableModel to hold its data and a TableColumnModel to manage the table's columns.

Question 6: Which type of model holds a text component&146;s content?
Answer 6: http://java.sun/j2se/1.4.2/docs/api/javax/swing/text/DocumentDocument

Exercises

Exercise 1. Implement a program with a GUI that looks like the one shown below. Put the main method in a class named MyDemo1.

uiswing/QandE/MyDemo1 16599ALIGNNATURALSIZEFLAG3ALTMyDemo1

Answer 1: See MyDemo1.javaexample-1dot4/MyDemo1.javasourceIcon (in a .java source file) Here's the code that adds the bold, italicized text:

JLabel label = new JLabel("My Demo); frame.getContentPane().add(BorderLayout.CENTER, label); label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD)); label.setHorizontalAlignment(JLabel.CENTER)

Exercise 2. Make a copy of MyDemo1.java named MyDemo2.java. Add a menu bar to MyDemo2.
Answer 2: See
MyDemo2.javaexample-1dot4/MyDemo2.javasourceIcon (in a .java source file) The menu bar can be implemented with this code:

JMenu menu = new JMenu("Menu); JMenuBar mb = new JMenuBar(); mb.add(menu); frame.setJMenuBar(mb);

Exercise 3. Copy MyDemo1.java to MyDemo3.java. Add a button (JButton) to MyDemo3.java. Make it the button.
Answer 3: See
MyDemo3.javaexample-1dot4/MyDemo3.javasourceIcon (in a .java source file) Here's the code that adds the button and makes it the button:

JButton b = new JButton("A button); frame.getContentPane().add(BorderLayout.PAGE_END, b); frame.getRootPane().setButton(b); Bhopal news
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india