*Graphical User Interfaces* Flashcards

1
Q

Components

A

Components present data to users and/or allow them to interact with the application. Examples include buttons, labels, text fields, check boxes and radio buttons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Java Foundational Classes

A

These are used to create graphical user interfaces (GUI). The two primary sets of such classes are the AWT and Swing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Abstract Windowing Toolkit (AWT)

A

The first set of classes for drawing graphics and creating GUI in Java. Programmers are limited in what they can do with the AWT because they do not actually draw components on the screen. Instead, they communicate with another layer of software, known as the peer classes, which directs the underlying operating system to draw its own built-in components. This means that applications built with Java have a look that’s consistent with other applications on the same operating system, but behavior can vary slightly. In addition, in order for Java to maintain its portability, AWT is limited to offering only those components that every operating system supports.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Swing

A

A third limitation with AWT is that programmers cannot easily customize components. Hence, Swing was introduced with the release of Java 2. Swing is a library of classes that do not replace AWT, but provide an improved alternative for creating GUI applications and applets. Very few Swing classes rely on peer classes. Instead, Swing draws components on the screen. This means that Swing components can have a consistent look and predictable behavior on any operating system.

To use the Swing classes, you need to use the following import statement:
import javax.swing.*;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Heavyweight components

A

Components that are coupled with their underlying peer classes, such as AWT components

*Note, components that are not coupled with peer classes are referred to as lightweight components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Event-driven programming

A

Programs that operate in a GUI environment must be event-driven. That is, they create and respond to various events that user perform such as clicking a button.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Event listener

A

An object that automatically executes one of its methods when a specific event occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Frame

A

A frame is a basic window that has a border around it, a title bar, and a set of buttons for minimizing, maximizing, and closing the window. In a Swing application, you create a frame from the JFrame class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you set a frame’s size?

A

With the setSize method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you display a frame on the screen?

A

With the setVisible method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Using inheritance to extend the JFrame class

A
While you can create an instance of the JFrame class and display it, it's much more common to use inheritance to create a new class that extends the JFrame class. For example:
public class CustomWindow extends JFrame

When you do this the new class inherits methods of the JFrame class such as setTitle, setSize, setDefaultCloseOperation, and setVisible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Content panes and panels

A

A content pane is a container that is part of every JFrame object. You cannot see the content pane and it does not have a border, but any component that is to be displayed in a JFrame must be added to it content pane.

A panel is also a container that holds GUI components but unlike content panes, they cannot be displayed by themselves; they must be added to content panes, since they are used to hold and organize collections of related components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Event object

A

When an event takes place in a GUI, the component responsible for the event (the event source) creates an event object in memory. The event object contains information about the event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Event listener

A

An event listener is an object that responds to events. If the event source component is connected to an event listener then the event object is automatically passed as an argument to a specific method in the event listener. Then the method performs the appropriate action in response to the event. Note that an event source can be linked to more than one event listener.

*Note that when you’re building a GUI application, you need to write the classes for the event listeners that your application needs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Event firing

A

The process of an event object being created by a source component and responded to by an event listener.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If you’re writing an event listener class for a JButton component, what interface must the class implement? What method must the class have and when is it executed?

A

The class must implement the ActionListener interface, and it must have a method named actionPerformed. The method is executed when the user clicks the button.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a common technique for writing an event listener class?

A

To write it as a private inner class, inside the class that creates the GUI

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is one requirement of all event listeners?

A

They must implement an interface. The specific interface used depends on the type of event the listener needs to handle. For example, action events require an action listener that implements the ActionListener interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Registering an event listener object

A

The process of connecting an event listener object to a GUI component. For example:

// Adds an action listener to the button

calcButton.addActionListener(new CalcButtonListener());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Layout manager

A
A layout manager is an object that governs the positions and sizes of components in a container. The layout manager automatically repositions and, in some cases, resizes the components when the container is resized. These are used in Java since you do not normally specify the exact location of a component within a window. Instead, you let the layout manager control the position of components. In order to use a layout manager with a group of components, you must place the components in a container and then create a layout manager object. For example:
JPanel panel = new JPanel( );
**panel.setLayout(new BorderLayout( ));**
21
Q

What are the 3 main layout managers?

A
  1. FlowLayout: arranges components in rows that flow from one to the next once there’s no more room horizontally. (This is the default layout manager for JPanel objects!)
  2. BorderLayout: divides a container into five regions - north, south, east, west, and center - each of which holds a single component and each component fills the region entirely. (This is the default layout manager for a JFrame object’s content pane!)
  3. GridLayout: creates a grid with rows and columns, much like a spreadsheet.
22
Q

How do you change the background color of a component or the color of text displayed by a label or button?

A

You use the setBackground method to set the color of a component, and the setForeground method to set the color of text displayed on a component.

23
Q

How can you cause a content pane to be automatically sized to accommodate the components contained within it?

A

By calling the pack method

24
Q

If you want a user to only be able to select one option from a group of items, should you use radio buttons or check boxes?

A

Radio buttons, because these are used to represent mutually exclusive choices.

In contrast, if the user can select multiple options, then check boxes are appropriate.

25
Q

What is the purpose of the ButtonGroup class?

A

Once you’ve created JRadioButton objects you must create an instance of the ButtonGroup class and then add the JRadioButton objects to it. The ButtonGroup object creates the mutually exclusive relationship among the radio buttons that it contains. That is, it’s function is to deselect all the other radio buttons when one of them is selected.

For example:
// Create three radio buttons
JRadioButton button1 = new JRadioButton(“Choice 1”, true);
JRadioButton button2 = new JRadioButton(“Choice 2”);
​JRadioButton button3 = new JRadioButton(“Choice 3”);

**//Create a ButtonGroup object**
ButtonGroup group = new ButtonGroup( );
  • *// Add the radio buttons to the ButtonGroup object**
    group. add(button1);

group.add(button2);

group.add(button3);

*Note that the additional parameter of true in the code above for button1 is an optional parameter that can be passed in order for the button to be selected by default.

26
Q

How do you create a check box in Java?

A
With the JCheckBox class. For example:
JCheckBox check1 = new JCheckBox("Macaroni");
\*Note that you can similarly pass another optional boolean parameter to the constructor in order for the box to be selected or deselected by default.
27
Q

What type of an event does a radio button generate when clicked?

A

An action event

28
Q

What type of an event does a check box generate when clicked?

A
An **item event**. You handle these events similar to action events. First, you write an *item listener* class which must implement the ItemListener interface and must have a method named itemStateChanged with the following header:
public void itemStateChanged(ItemEvent e)

Once you have written an item listener class, you create an object of that class and then register it with the JCheckBox component. That way, when the JCheckBox component generates an event, it automatically executes the itemStateChanged method of the item listener object that’s registered to it by passing the event as an argument.

29
Q

How can you determine in code whether radio buttons and check boxes are selected?

A

By using the isSelected method. This is particularly helpful when writing action / item listeners. For example:
if (redCheckBox.isSelected( ))
// perform some action….
else
// perform some other action…

30
Q

What method do you use to set a border around a component?

A

The setBorder method. For example:
panel.setBorder(BorderFactory.createTitledBorder(“Choices”);
*Note that in the example above “Choices” is the argument passed to the method as a String parameter is required.

31
Q

What is the preferred way of creating border object?

A

You should use the BorderFactory class to create them for you. The BorderFactory class has static methods that return various types of borders, such as:

  • createCompoundBorder
  • createEmptyBorder
  • createEtchedBorder
  • createLineBorder
  • createLoweredBevelBorder
  • createMatteBorder
  • createRaisedBevelBorder
  • createTitleBorder
32
Q

Splash screen

A

A graphic image that is displayed while an application loads into memory and starts up. Java supports GIF, PNG, and JPEG formats. To display a splash screen you use the Java command in the following way when you run the application:
java splash: GraphicFileName ClassFileName

Where *GraphicFileName* is the name of the file that contains the image to be displayed and *ClassFileName* is the name of the .class file that you're running. For example:
java splash: BrandiLogo.jpg Bagel
33
Q

How do you create a text field in Java?

A
JTextField field = new JTextField(10);
Where the argument of 10 indicates the length of the field.
34
Q

How do you make a field read-only versus editable?

A

With the setEditable(boolean editable) method. For example:
field.setEditable(false);

35
Q

How do you display a data point along with the text field?

A

With the setText method. For example:
field.setText(“Hello”);

36
Q

How do you create a list in Java?

A
You pass an array of objects to the JList constructor. For example:
String[] names = { "Bill", "Geri", "Laurie" };
JList namelist = new JList(names);
37
Q

What are the 3 selection modes available for JLists?

A
  • Single Selection Mode: only 1 item can be selected at a time
  • Single Interval Selection Mode: multiple items can be selected, but they must be in a single interval (i.e. a set of contiguous items)
  • Multiple Interval Selection Mode: multiple items may be selected with no restrictions (this is the default selection mode!)
38
Q

How do you respond to list events?

A

You handle list selection events with a list selection listener class, which must implement the ListSelectionListener interface and must have a method named valueChanged that takes an argument of ListSelectionEvent type.

Similar to other listeners, a list selection listener object must be created and registered with the appropriate component by passing it as an argument to the JList component’s addListSelectionListener method. This way, when the JList component generates an event, it will automatically execute the valueChanged method of the list selection listener object.

* Note, when writing the list selection listener class, there are two ways to retrieve the selected item:

  1. getSelectedValue method: returns a reference to the item selected. For example:
    String selectedName;
    selectedName = (String) nameList.getSelectedValue( );
  2. getSelectedIndex method: returns the index of the selected item. For example:
    int index;
    String selectedName;
    index = namesList.getSelectedIndex( );
    if ( index != -1 )
    selectedName = names[index];
39
Q

How do you add a scroll bar to a JList component?

A
  1. First you set the number of visible rows:
    nameList.setVisibleRowCount(3);
  2. Next you create a scroll pane object and add the JList component to it:
    JScrollPane scrollPane = new JScrollPane(nameList);
  3. Finally you add the scroll pane to a panel and place the panel on the JFrame object’s content pane:
    JPanel panel = new JPanel( );
    panel.add(scrollpane);
    add.panel;
40
Q

How do you create a combo box with Swing?

A
With the JComboBox class. For example:
JComboBox nameBox = new JComboBox(names);

* Note, unlike a list component, a combo box presents its items in a drop-down list. However, to create a combo box, you similarly pass an array of objects to its constructor.

41
Q

How do you respond to combo box events?

A

When an item in a combo box is selected it generates an action event so you handle combo box events with the action listener class like JButton events.

42
Q

How do you retrieve a selected item or the index of the selected item from a JComboBox component?

A

You use the getSelectedItem method to retrieve the selected item, and the getSelectedIndex method to get the index of the selected item.

43
Q

What is the difference between an editable and uneditable combo box? Which of these is the default behavior?

A

An uneditable combo box combines a button with a list and allows the user to select only items from its list. An editable combo box combines a text field and a list. In addition to selecting items from the list, the user may also type input into the text field. The default type of combo box is uneditable. This is controlled with the setEditable method.

44
Q

How do you display images in a GUI?

A

Images can be displayed in labels and buttons. You use the ImageIcon class to get an image from a file.

  1. First you create an instance of the ImageIcon class:
    ImageIcon image = new ImageIcon(“smiley.gif”);
  2. Next you display the image in a label by passing the image as an argument to the JLabel constructor:
    JLabel label = new JLabel(image);
  3. If you want to add text to the label as well, you should first create the label with text instead and then add the image (text is displayed to the right of the image):
    JLabel label = new JLabel(“Hello!”);
    label.setIcon(image);

* Note, the same process applies to JButtons!

45
Q

Mnemonic

A
A mnemonic is a key on the keyboard that you press in combination with the Alt key to quickly access a component such as a button. These are sometimes referred to as short-cut keys, or hot keys. You assign an access key to a component through the component’s setMnemonic method. For example, the following code creates an exit button and assigns the X key as its mnemonic:
JButton exitButton = new JButton("Exit");
exitButton.setMnemonic(KeyEvent.VK\_X);
46
Q

How do you add a tool tip to a GUI component?

A
With the setToolTipText method, which is inherited from the JComponent class. For example, the following code creates an exit button with a tool tip that explains what the component does:
JButton exitButton = new JButton("Exit");
exitButton.setToolTipText("Click here to exit");
47
Q

What is a text area and how do you add one to a GUI?

A
A text area is a multi-line text field that can accept multiple lines of text input (in contrast to a text field that can only handle a single line of text). You use the JTextArea class to create a text area. There are two constructors that can be used to create a new instance of the JTextArea class:
**JTextArea(int *rows*, int *columns*)
JTextArea(String *text*, int *rows*, int *columns*)**
In both constructors, *rows* is the number of lines of text to be displayed and *columns* is the number of characters per line. The second constructor has an additional, optional parameter to display a string of text to be initially displayed (maybe something like "search results to be displayed here" ? ). For example, the following code creates a text area with 20 lines of text and 40 characters per line that displays "info" by default:
**JTextArea input = new JTextArea(info, 20, 40);**

JTextArea

48
Q

How do you add a scroll bar to a text area?

A
Similar to JList components, scroll bars are not automatically displayed for text areas. To display a scroll bar with a text area, you must add it to a scroll pane:
**JScrollPane scrollpane = new JScrollPane(input);**

*Note that by default, scroll bars are not displayed until they are needed. However, you can alter this behavior by passing one of the following arguments to the setHorizontalScrollBarPolicy or setVerticalScrollBarPolicy methods:

  • HORIZONTAL_SCROLLBAR_AS_NEEDED
  • HORIZONTAL_SCROLLBAR_ALWAYS
  • HORIZONTAL_SCROLLBAR_NEVER
  • VERTICAL_SCROLLBAR_AS_NEEDED
  • VERTICAL_SCROLLBAR_ALWAYS
  • VERTICAL_SCROLLBAR_NEVER

For example, the following code sets the horizontal scroll bar to always appear:
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)