INEW 2338, Advanced Java Study Guide:  Swing

Published:  November 8, 2004
Revised:  January 9, 2005
By Richard G. Baldwin

File: Inew2338Sg005.htm


Welcome

The purpose of this series of tutorial lessons is to help you learn the essential topics covered in INEW 2338, Advanced Java.

These lessons provide questions, answers, and explanations designed to help you to understand the essential Java features covered by the Advanced Java course.

The textbook for this course is Advanced Java Internet Applications, Second Edition by Art Gittleman, ISBN 1-57676-096-0.  This study guide is for Chapter 5 in the textbook.

In addition to the textbook, I recommend that you study my extensive collection of online Java tutorial lessons.  Those tutorial lessons are published at http://www.dickbaldwin.com.

For this particular study guide, you should study lessons 108 through 120, lesson 124, lessons 190 through 212 and lessons 1000 through 1068 at the URL given above.  These lessons provide a great deal of information about the use of Swing that goes far beyond the information contained in the textbook.


Questions

1.  Which of the following comes closest to representing the output that would be produced by the following program when the program is executed using Java SDK 1.4.2 or a later version of the SDK under a Windows operating system?

import javax.swing.UIManager;

class Inew2338_070{
  public static void main(String[] args){
    UIManager.LookAndFeelInfo[] laf =
            UIManager.getInstalledLookAndFeels();
    for(int cnt = 0;cnt < laf.length;cnt ++){
      String lafStr = laf[cnt].toString();
      System.out.println(lafStr.substring(
                     lafStr.lastIndexOf(".") + 1,
                     lafStr.length() - 1));
    }//end for loop

  }//end main
}//end class Inew2338_070

Answer and Explanation

2.  A Java application and three GUI images are provided below.  Which image best matches the GUI that would be produced by this application when run under a Windows operating system, A, B, or C?  Assume that the Java SDK is version 1.4.2 or later.

import javax.swing.*;
import java.awt.*;

class Inew2338_072a extends JFrame{
  public static void main(String[] args){
    new Inew2338_072a();
  }//end main

  Inew2338_072a(){
    System.out.println(UIManager.
                               getLookAndFeel());
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(new JButton("OK"));
    setSize(275,100);
    setTitle("Copyright 2004, R.G.Baldwin");
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }//end constructor
}//end class Inew2338_072a



Choice A



Choice B



Choice C

Answer and Explanation

3.  A Java application and three GUI images are provided below.  Which image best matches the GUI that would be produced by this application when it is run under Windows, A, B, or C?  Assume that the Java SDK is version 1.4.2 or later.
 
class Inew2338_074 extends JFrame{
  public static void main(String[] args){
    new Inew2338_074();
  }//end main

  Inew2338_074(){
    try{
      UIManager.setLookAndFeel(UIManager.
                getSystemLookAndFeelClassName());
    }catch(Exception e){
      e.printStackTrace();
    }//end catch

    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(new JButton("OK"));
    setSize(275,100);
    setTitle("Copyright 2004, R.G.Baldwin");
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);

    setVisible(true);
  }//end constructor
}//end class Inew2338_074



Choice A



Choice B



Choice C

Answer and Explanation

4.  The first box below shows a Java application.  The second box shows an image contained in a file in the current directory named inew2338sg005g.gif.  The third box shows a GUI.

True or false.  The Java application is capable of producing the GUI shown.

/*File Inew2338_076b.java
Copyright 2004, R.G.Baldwin

Tested using SDK 1.4.2 and WinXP
************************************************/
import javax.swing.*;
import java.awt.*;

class Inew2338_076b extends JFrame{
  public static void main(String[] args){
    new Inew2338_076b();
  }//end main

  Inew2338_076b(){

    getContentPane().setLayout(new FlowLayout());
    JButton theButton = new JButton("OK");
    theButton.setIcon("inew2338sg005g.gif");
    getContentPane().add(theButton);
    setSize(275,100);
    setTitle("Copyright 2004, R.G.Baldwin");
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }//end constructor
}//end class Inew2338_076b



The red ball image file named
inew2338sg005g.gif

Answer and Explanation

5.  The following box contains a Java application.  The next three boxes show the contents of image files in the current directory with the file names shown.  The fifth box shows the GUI produced by the program when it first appears on the screen.

True or false?  Pointing to the light bulb in the JButton with the mouse pointer causes the image of the light bulb to change to an image of an illuminated light bulb.  Pressing the JButton causes the image of the light bulb to change to a blue light bulb.

import javax.swing.*;
import java.awt.*;

class Inew2338_078 extends JFrame{
  public static void main(String[] args){
    new Inew2338_078();
  }//end main

  Inew2338_078(){

    getContentPane().setLayout(new FlowLayout());
    JButton theButton = new JButton("OK");
    theButton.setIcon(new ImageIcon(
                          "inew2338sg005j.gif"));
    theButton.setPressedIcon(new ImageIcon(
                          "inew2338sg005m.gif"));
    theButton.setRolloverIcon(new ImageIcon(
                          "inew2338sg005k.gif"));
    getContentPane().add(theButton);
    setSize(275,100);
    setTitle("Copyright 2004, R.G.Baldwin");
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }//end constructor
}//end class Inew2338_078



Image file inew2338sg005j.gif



Image file inew2338sg005m.gif



Image file inew2338sg005k.gif

Answer and Explanation

6.  The following box contains a Java application, which produces the GUI shown in the second box.

True or false?  Holding down the ctrl key and pressing the B key (without holding down the shift key) produces the same result that is produced by pressing the JButton with the mouse pointer.

import javax.swing.*;
import java.awt.*;

class Inew2338_080 extends JFrame{
  public static void main(String[] args){
    new Inew2338_080();
  }//end main

  Inew2338_080(){

    getContentPane().setLayout(new FlowLayout());
    JButton theButton = new JButton("OK");

    theButton.setMnemonic('B');

    getContentPane().add(theButton);
    setSize(275,100);
    setTitle("Copyright 2004, R.G.Baldwin");
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }//end constructor
}//end class Inew2338_080

Answer and Explanation

7.  A Java application, which produces a GUI, is shown in the following box.

True or false?  Given that the file named inew2338sg005p.gif contains a picture of a small blue ball, this application produces the GUI shown in the second box below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class Inew2338_082 extends JFrame{

  //---------------------------------------//

  public static void main(String args[]) {
      new Inew2338_082();
  }//end main()
  //---------------------------------------//

  //The purpose of this method is to create
  // and return an opaque pink JLabel with
  // a border.  The text content of the
  // label is provided as the first
  // parameter.  The border type is provided
  // as the second parameter.  When the
  // label is displayed, the left and top
  // insets are displayed following the
  // text content of the label.
  JLabel makeLabel(
           String content,Border borderType){

    JLabel label = new JLabel();
    label.setBorder(borderType);
    label.setOpaque(true);
    label.setBackground(Color.pink);

    label.setText(content + ","
    +label.getInsets().left + ","
    +label.getInsets().top);

    return label;

  }//end makeLabel()
  //---------------------------------------//

  Inew2338_082(){//constructor

    getContentPane().setLayout(
                           new FlowLayout());

    getContentPane().add(makeLabel(
         "EtchedBorder",new EtchedBorder()));
    getContentPane().add(makeLabel(
        "BevelBorder RAISED",new BevelBorder(
                       BevelBorder.RAISED)));
    getContentPane().add(makeLabel(
       "BevelBorder LOWERED",new BevelBorder(
                      BevelBorder.LOWERED)));
    getContentPane().add(makeLabel(
         "EmptyBorder",new EmptyBorder(
                                  5,5,5,5)));
    getContentPane().add(makeLabel(
      "Compound, Empty + BevelBorder RAISED",
      new CompoundBorder(new BevelBorder(
      BevelBorder.RAISED),new EmptyBorder(
                                 5,5,5,5))));
    getContentPane().add(makeLabel(
     "Compound, Empty + BevelBorder LOWERED",
     new CompoundBorder(new BevelBorder(
     BevelBorder.LOWERED),new EmptyBorder(
                                 5,5,5,5))));

    getContentPane().add(makeLabel(
      "Compound, Empty + SoftBevelBorder " +
      "RAISED",
      new CompoundBorder(new SoftBevelBorder(
      SoftBevelBorder.RAISED),new EmptyBorder(
                                 5,5,5,5))));
    getContentPane().add(makeLabel(
      "Compound, Empty + SoftBevelBorder " +
      "LOWERED",
      new CompoundBorder(new SoftBevelBorder(
      SoftBevelBorder.LOWERED),new EmptyBorder(
                                 5,5,5,5))));
    getContentPane().add(makeLabel(
      "Compound, Empty + LineBorder",
      new CompoundBorder(new LineBorder(
      Color.blue,5),new EmptyBorder(
                                 5,5,5,5))));
    getContentPane().add(makeLabel(
      "Compound, Empty + MatteBorder Image",
      new CompoundBorder(new MatteBorder(
      19,19,19,19,new ImageIcon(
      "inew2338sg005p.gif")),new EmptyBorder(
                                 5,5,5,5))));

    getContentPane().add(makeLabel(
      "Compound, Empty + MatteBorder Color",
      new CompoundBorder(new MatteBorder(
               19,19,19,19,Color.blue),
                 new EmptyBorder(5,5,5,5))));


    getContentPane().add(makeLabel(
      "Compound, Empty + TitledBorder",
      new CompoundBorder(new TitledBorder(
        "Title"),new EmptyBorder(5,5,5,5))));

    setTitle("Copyright 2000, R.G.Baldwin");
    setSize(329,500);
    setDefaultCloseOperation(
                        JFrame.EXIT_ON_CLOSE);
    setVisible(true);

  }//end constructor

}//end class Inew2338_082

Answer and Explanation

8.  The Java application in the following box produces one of the dialogs shown in the second, third, and fourth boxes.  Which dialog is produced, Choice A, B, or C?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Inew2338_084b extends JFrame{

  static JFrame thisFrame;

  public static void main(String[] args){
    thisFrame = new Inew2338_084b();
  }//end main

  Inew2338_084b(){
    JOptionPane.showConfirmDialog(thisFrame,
                                      "Message");
  }//end constructor
}//end class Inew2338_084b



Choice A



Choice B



Choice C

Answer and Explanation

9.  The Java application in the following box produces a GUI consisting of a JTabbedPane component.  The JTabbedPane component has three layers or pages consisting of the following components:

True or false?  This application produces the GUI shown in the second box below.

import javax.swing.*;

class Inew2338_086a extends JFrame{
  public static void main(String[] args){
    new Inew2338_086a();
  }//end main

  Inew2338_086a(){

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab(
                    "JButton",new JButton("OK"));
    tabbedPane.addTab("JLabel",new JLabel("OK"));
    tabbedPane.addTab("JPanel",new JPanel());
    getContentPane().add(tabbedPane);
    setSize(275,100);
    setTitle("Copyright 2004, R.G.Baldwin");
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }//end constructor
}//end class Inew2338_086a

Answer and Explanation

10.  True or false?  The Java application in the following box produces the GUI shown in the second box below.  This GUI consists of a vertically-scrollable table of empty cells with 5 rows and 6 columns.  The user can enter data into the cells as shown in the GUI.

import javax.swing.*;

class Inew2338_086a extends JFrame{
  public static void main(String[] args){
    new Inew2338_086a();
  }//end main

  Inew2338_086a(){

    JTable aTable = new JTable(5,6);
    JScrollPane scrollPane =
                         new JScrollPane(aTable);
    getContentPane().add(scrollPane);
    setSize(275,100);
    setTitle("Copyright 2004, R.G.Baldwin");
    setDefaultCloseOperation(
                           JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }//end constructor
}//end class Inew2338_086a

Answer and Explanation


Copyright 2004, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which have gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com


Answers and Explanations


Answer 10

True.

Explanation 10

Not much in the way of an explanation is needed here.  The JTable component provides a very large number of options and capabilities.  This one is just about as simple as it gets, using the DefaultTableModel and specifying the number of rows and the number of columns as parameters to the JTable constructor.  The only thing fancy about this one is the encapsulation of the JTable object in a JScrollPane object to provide the ability to scroll up and down the rows.

You can learn more about this topic at http://www.codeguru.com/java/Swing/JTable/index.shtml.

You might also want go to Google and search for the following keywords:

This might help you to locate some useful publications on this topic.

Back to Question 10


Answer 9

False.

Explanation 9

This Java application produces the GUI shown below.

The default case produced by instantiating the JTabbedPane using the following constructor

new JTabbedPane();

results in a tabbed pane with the tabs along the top.  To cause the tabs to be on the right, instantiate the JTabbedPane object using the following constructor and parameter:

new JTabbedPane(JTabbedPane.RIGHT);

The tabs can be placed on the top, bottom, left, or right by instantiating the JTabbedPane object and passing one of the following constants to the constructor:

As mentioned earlier, the top location is the default if the object is instantiated using the constructor that takes no parameters.

You can learn more about this topic at http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTabbedPane.html.

You might also want go to Google and search for the following keywords:

This might help you to locate some useful publications on these topics.

Back to Question 9


Answer 8

The correct answer is Choice A.

Explanation 8

The dialog identified as Choice A is produced by invoking the static showConfirmDialog method of the JOptionPane class.  The dialog in Choice B is produced by invoking the showInputDialog method.  The dialog in Choice C is produced by invoking the showMessageDialog method.

You can learn more about this topic at http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html.

You might also want go to Google and search for the following keywords:

This might help you to locate some useful publications on these topics.

Back to Question 8


Answer 7

True.

Explanation 7

This application produces the GUI shown to illustrate many aspects of the border property.  (The small blue ball image is used to construct the MatteBorder Image.)  You can read about the various aspects of this program, and other programs as well in tutorial lessons 1000 through 1025 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 7


Answer 6

False.

Explanation 6

Holding down the alt key (not the ctrl key) and pressing the B key (without holding down the shift key) produces the same result that is produced by pressing the JButton with the mouse pointer.

You can learn more about this topic at http://java.sun.com/docs/books/tutorial/uiswing/components/button.html.

You might also want go to Google and search for the following keywords:

This might help you to locate some useful publications on the topic.

Back to Question 6


Answer 5

False.

Explanation 5

The true statement would be, pointing to the light bulb in the JButton with the mouse pointer causes the image of the light bulb to change to an image of a blue light bulb.  Pressing the JButton causes the image of the light bulb to change to an illuminated light bulb.  This is the reverse of the statements in the question.

You can learn more about this topic in lesson 124 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 5


Answer 4

False.

Explanation 4

While it is possible for an image to be placed in a JButton, (and in many other Swing components as well), it is first necessary to encapsulate the image in an object of the interface type Icon.  Therefore, this application won't compile.

One class that implements Icon is ImageIcon.  Therefore, the compilation error can be eliminated and the program can be made to produce the GUI shown by replacing the statement that reads

theButton.setIcon("inew2338sg005g.gif");

with a statement that reads

theButton.setIcon(new ImageIcon("inew2338sg005g.gif"));

You can learn more about this topic in lesson 124 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 4


Answer 3

Choice C is the correct answer.

Explanation 3

This question stipulates that the program is run under a Windows operating system.  The LookAndFeel is set by executing the following method:

 UIManager.setLookAndFeel(UIManager.
                                       getSystemLookAndFeelClassName());

According to Sun, this method

"Returns the name of the LookAndFeel class that implements the native systems look and feel if there is one, otherwise the name of the default cross platform LookAndFeel class."

Because the Windows version of the SDK 1.4.2 or later provides a LookAndFeel that implements the native look and feel for the operating system, this program displays a GUI that exhibits the Windows LookAndFeel.

You can learn more about this topic in lesson 2 at http://www.dickbaldwin.com/java/Java002.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 3


Answer 2

Choice A is the correct answer.

Explanation 2

Because this application does not set the LookAndFeel, the default, or Metal LookAndFeel is used.  The image in Choice A shows the Metal LookAndFeel.

Choice B shows the Motif LookAndFeel, while Choice C shows the Windows LookAndFeel.

You can learn more about this topic in lesson 9010 at http://www.austincc.edu/baldwin/Java9010.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 2


Answer 1

C.  MetalLookAndFeel
    MotifLookAndFeel
    WindowsLookAndFeel

Explanation 1

The Windows version of Java SDK 1.4.2 supports all three of the LookAndFeels indicated by answer C.  Future releases are expected to support at least these three, and possibly more.

Note however, that the WindowsLookAndFeel may not be supported on all operating systems.  Therefore, when the program is executed under an operating system other than Windows, the correct answer may be:

B.  MetalLookAndFeel
    MotifLookAndFeel

You can learn more about this topic in lesson 111 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 1


Copyright 2004, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which have gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-