1 / 72

Java Swing, continued

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

Download Presentation

Java Swing, continued

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Swing, continued

  2. 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.*;

  3. 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

  4. text field and text area and scroll bars

  5. Text Field and Text Area Examples

  6. 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

  7. 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");

  8. A Text Field (Part 6 of 7)

  9. A Text Field (Part 7 of 7) What layout is it used?

  10. A Text Field (Part 1 of 7)

  11. A Text Field (Part 2 of 7)

  12. A Text Field (Part 3 of 7)

  13. A Text Field (Part 4 of 7)

  14. A Text Field (Part 5 of 7)

  15. 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);

  16. 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);

  17. 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);

  18. 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

  19. 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

  20. 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);

  21. 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

  22. 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

  23. View Port for a Text Area

  24. 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);

  25. 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

  26. A Text Area with Scroll Bar What layout is it used?

  27. A Text Area with Scroll Bar

  28. A Text Area with Scroll Bar

  29. A Text Area with Scroll Bar

  30. A Text Area with Scroll Bar

  31. A Text Area with Scroll Bar

  32. A Text Area with Scroll Bar

  33. A Text Area with Scroll Bar

  34. icons

  35. 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

  36. 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

  37. 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");

  38. 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);

  39. 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

  40. Using Icons What layout is it used?

  41. Using Icons (Part 1 of 5)

  42. Using Icons (Part 2 of 5)

  43. Using Icons (Part 3 of 5)

  44. Using Icons (Part 4 of 5)

  45. Using Icons(Part 5 of 5)

  46. window listeners

  47. 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

  48. 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) { }

  49. Methods in the WindowListener Interface (Part 1 of 2)

  50. Methods in the WindowListener Interface (Part 2 of 2)

More Related