730 likes | 901 Views
Java Swing, continued. Swing component hierarchy. Graphical components in Java form an inheritance hierarchy: java.lang.Object +-- java.awt.Component +-- java.awt.Container | +-- javax.swing.JComponent | +-- javax.swing. JButton
E N D
Swing component hierarchy • Graphical components in Java form an inheritance hierarchy: java.lang.Object +--java.awt.Component +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.JButton | +--javax.swing.JLabel | +--javax.swing.JMenuBar | +--javax.swing.JOptionPane | +--javax.swing.JPanel | +--javax.swing.JTextArea | +--javax.swing.JTextField | +--java.awt.Window +--java.awt.Frame +--javax.swing.JFrame • When doing GUI programming, always import these packages: import java.awt.*;import javax.swing.*;
What Can be Summarized? Derived a new container class from, say, JFrame • In the derived class • Define a constructor that sets up the title and size of the window • Set up the proper lay out of the outer container • Create inner containers (using JPanel or other containers) • Set up the proper lay out of each inner containers • Add the interface objects, such as buttons and others, to the corresponding containers • Remember to associate a listener object for each interface object • Add the containers to the Frame object in order Define listener classes to handle possible events fired by the interface objects added in the window • In the main function • Create the object of the derived window class • Launch the interface by setting it as visible
Text Fields • A text field is an object of the class JTextField • It is displayed as a field that allows the user to enter a single line of text private JTextField name; . . . name = new JTextField(NUMBER_OF_CHAR); • In the text field above, at least NUMBER_OF_CHAR characters can be visible
Text Fields • There is also a constructor with one additional String parameter for displaying an initial Stringin the text field JTextField name = new JTextField( "Enter name here.", 30); • A Swing GUI can read the text in a text field using the getTextmethod String inputString = name.getText(); • The method setText can be used to display a new text string in a text field name.setText("This is some output");
A Text Field (Part 7 of 7) What layout is it used?
Text Areas • A text area is an object of the class JTextArea • It is the same as a text field, except that it allows multiple lines • Two parameters to the JTextArea constructor specify the minimum number of lines, and the minimum number of characters per line that are guaranteed to be visible JTextAreatheText = new JTextArea(5,20); • Another constructor has one addition String parameter for the string initially displayed in the text area JTextAreatheText = new JTextArea( "Enter\ntext here." 5, 20);
Text Areas • The line-wrapping policy for a JTextArea can be set using the method setLineWrap • The method takes one boolean type argument • If the argument is true, then any additional characters at the end of a line will appear on the following line of the text area • If the argument is false, the extra characters will remain on the same line and not be visible theText.setLineWrap(true);
Text Fields and Text Areas • A JTextField or JTextArea can be set so that it can not be changed by the user theText.setEditable(false); • This will set theText so that it can only be edited by the GUI program, not the user • To reverse this, use true instead (this is the default) theText.setEditable(true);
Tip: Labeling a Text Field • In order to label one or more text fields: • Use an object of the class JLabel • Place the text field(s) and label(s) in a JPanel • Treat the JPanel as a single component
Numbers of Characters Per Line • The number of characters per line for a JTextField or JTextArea object is the number of em spaces • An em space is the space needed to hold one uppercase letter M • The letter M is the widest letter in the alphabet • A line specified to hold 20 M 's will almost always be able to hold more than 20 characters
Tip: Inputting and Outputting Numbers • When attempting to input numbers from any Swing GUI, input text must be converted to numbers • If the user enters the number 42 in a JTextField, the program receives the string "42" and must convert it to the integer 42 String input = name.getText(); intnum = Integer.parseInt(input); • The same thing is true when attempting to output a number • In order to output the number 42, it must first be converted to the string "42" Integer.toString(num);
Scroll Bar • When a text area is created, the number of lines that are visible and the number of characters per line are specified as follows: JTextAreamemoDisplay = new JTextArea(15, 30); • However, it would often be better not to have to set a firm limit on the number of lines or the number of characters per line • This can be done by using scroll bars with the text area
Scroll Bar • Scroll bars can be added to text areas using the JScrollPaneclass • –The JScrollPaneclass is in the javax.swingpackage • –An object of the classJScrollPaneis like a view port with scroll bars
Scroll Bar • When a JScrollPaneis created, the text area to be viewed is given as an argument JTextAreamemoDisplay = new JTextArea(15, 30); JScrollPanescrolledText= new JScrollPane(memoDisplay); • The JScrollPanecan then be added to a container, such as a JPanelor JFrame textPanel.add(scrolledText);
Scroll Bar • The scroll bar policies can be set as follows: • scrolledText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); • scrolledText.setVerticalScrollBarPolicy(JscrollPane.VERTICAL_SCROLLBAR_ALWAYS); • If invocations of these methods are omitted, then the scroll bars will be visible only when needed • If all the text fits in the view port, then no scroll bars will be visible • If enough text is added, the scroll bars will appear automatically
A Text Area with Scroll Bar What layout is it used?
Icons • JLabels, JButtons, and JMenuItems can have icons • An icon is just a small picture (usually) • It is not required to be small • An icon is an object of the ImageIcon class • It is based on a digital picture file such as .gif, .jpg, or .tiff • Labels, buttons, and menu items may display a string, an icon, a string and an icon, or nothing
Icons • The class ImageIcon is used to convert a picture file to a Swing icon ImageIcon dukeIcon = new ImageIcon("duke_waving.gif"); • The picture file must be in the same directory as the class in which this code appears, unless a complete or relative path name is given • Note that the name of the picture file is given as a string
Icons • An icon can be added to a label using the setIcon method as follows: JLabel dukeLabel = new JLabel("Mood check"); dukeLabel.setIcon(dukeIcon); • Instead, an icon can be given as an argument to the JLabel constructor: JLabel dukeLabel = new JLabel(dukeIcon); • Text can be added to the label as well using the setText method: dukeLabel.setText("Mood check");
Icons • Icons and text may be added to JButtons and JMenuItems in the same way as they are added to a JLabel JButtonhappyButton = new JButton("Happy"); ImageIconhappyIcon = new ImageIcon("smiley.gif"); happyButton.setIcon(happyIcon);
Icons • Button or menu items can be created with just an icon by giving the ImageIcon object as an argument to the JButton or JMenuItem constructor ImageIcon happyIcon = new ImageIcon("smiley.gif"); JButton smileButton = new JButton(happyIcon); JMenuItem happyChoice = new JMenuItem(happyIcon); • A button or menu item created without text should use the setActionCommand method to explicitly set the action command, since there is no string
Using Icons What layout is it used?
Window Listeners • Clicking the close-window button on a JFrame fires a window event • Window events are objects of the class WindowEvent • The setWindowListener method can register a window listener for a window event • A window listener can be programmed to respond to this type of event • A window listener is any class that satisfies the WindowListener interface
Window Listeners • A class that implements the WindowListener interface must have definitions for all seven method headers in this interface • Should a method not be needed, it is defined with an empty body public void windowDeiconified(WindowEvent e) { }