Java Swing Flashcards

1
Q

What thread does swing event handling run on?

A

The Event Dispatch Thread.

Most code that invokes Swing methods also runs on this thread. This is necessary because most Swing object methods are not “thread safe”: invoking them from multiple threads risks thread interference or memory consistency errors. Some Swing component methods are labelled “thread safe” in the API specification; these can be safely invoked from any thread.

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

How do you determine if your code is running on the event dispatch thread?

A

invoke javax.swing.SwingUtilities.isEventDispatchThread.

You can also:

//Print the name of the current thread:
System.out.println(“This thread is: “ + Thread.currentThread().getName());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the code to create a menu and add to a JFrame?

A
// menubar 
        static JMenuBar menuBar; 
        // JMenu 
        static JMenu menu; 
        // Menu items 
        static JMenuItem menuItem1, menuItem2, menuItem3; 
        // create a frame 
        static JFrame frame; 
        // create a frame 
        frame = new JFrame("Menu demo"); 
        // create a menubar 
        menuBar = new JMenuBar(); 
        // create a menu 
        menu = new JMenu("Menu"); 
        // create menuitems 
        menuItem1 = new JMenuItem("MenuItem1"); 
        menuItem2 = new JMenuItem("MenuItem2"); 
        menuItem3 = new JMenuItem("MenuItem3"); 
    // add menu items to menu 

    menu. add(menuItem1); 
    menu. add(menuItem2); 
    menu. add(menuItem3); 
        // add menu to menu bar 
        menuBar.add(menu); 
        // add menubar to frame 
        frame.setJMenuBar(menuBar); 
    // set the size of the frame 

    frame. setSize(500, 500); 
    frame. setVisible(true);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can menu items be used in more than one place?

A

No. Menu items, like other components, can be in at most one container. If you try to add a menu item to a second menu, the menu item will be removed from the first menu before being added to the second. For a way of implementing multiple components that do the same thing, see How to Use Actions.

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

What kinds of keyboard operations can be used on menus?

A

Menus support two kinds of keyboard alternatives: mnemonics and accelerators. Mnemonics offer a way to use the keyboard to navigate the menu hierarchy, increasing the accessibility of programs. Accelerators, on the other hand, offer keyboard shortcuts to bypass navigating the menu hierarchy. Mnemonics are for all users; accelerators are for power users.

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

What is a Mnemonic keyboard operation?

A

A mnemonic is a key that makes an already visible menu item be chosen. For example, in MenuDemo the first menu has the mnemonic A, and its second menu item has the mnemonic B. This means that, when you run MenuDemo with the Java look and feel, pressing the Alt and A keys makes the first menu appear. While the first menu is visible, pressing the B key (with or without Alt) makes the second menu item be chosen. A menu item generally displays its mnemonic by underlining the first occurrence of the mnemonic character in the menu item’s text, as the following snapshot shows.

You set a mnemonic by specifying the KeyEvent constant corresponding to the key the user should press.

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

What is an accelerator keyboard operation?

A

An accelerator is a key combination that causes a menu item to be chosen, whether or not it’s visible. For example, pressing the Alt and 2 keys in MenuDemo makes the first item in the first menu’s submenu be chosen, without bringing up any menus. Only leaf menu items — menus that don’t bring up other menus — can have accelerators. The following snapshot shows how the Java look and feel displays a menu item that has an accelerator.

To specify an accelerator you must use a KeyStroke object, which combines a key (specified by a KeyEvent constant) and a modifier-key mask (specified by an ActionEvent constant).

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

What kinds of dialogs can be created?

A
  • In Java Swing, we can create two kinds of dialogs: standard dialogs and custom dialogs.
  • Custom dialogs are created by programmers. They are based on the JDialog class.
  • Standard dialogs are predefined dialogs available in the Swing toolkit, for example the JColorChooser or the JFileChooser.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the two types of dialogs?

A
  • There are two basic types of dialogs: modal and modeless. Modal dialogs block input to other top-level windows.
  • Modeless dialogs allow input to other windows.
    • An open file dialog is a good example of a modal dialog.
  • While choosing a file to open, no other operation should be permitted. A typical modeless dialog is a find text dialog. It is handy to have the ability to move the cursor in the text control and define, where to start the finding of the particular text.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are message dialogs created?

A

Message dialogs are created with the JOptionPane.showMessageDialog() method.

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