1 / 140

Intro to Swing

Intro to Swing. GUI development. Swing is. A big API Built on AWT (another big API) Used for GUI's. Swing class hierarchy fragment. Component. AWT. Swing. Container. JComponent. Window. Panel. JWindow. Dialog. Frame. JFrame. JDialog. JTree. JPanel. JLabel. JTable. JApplet.

thad
Télécharger la présentation

Intro to Swing

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. Intro to Swing GUI development

  2. Swing is • A big API • Built on AWT (another big API) • Used for GUI's

  3. Swing class hierarchy fragment Component AWT Swing Container JComponent Window Panel JWindow Dialog Frame JFrame JDialog JTree JPanel JLabel JTable JApplet

  4. Other basic classes • AWT classes defined in the package java.awt • Color, ( an immutable class) • Point, • Dimension, • Font

  5. JOptionPane Examples

  6. JComboBox • A JComboBox looks like a text field with an arrow next to it. If you click on the arrow, the list of possible values is displayed. • If the Jcombobox is set to be editable, then you can edit the current selection as if it was a text field. • Only one item can be selected at a time. You can retrieve it by calling: getSelectedItem method. • Add the choice items to the end of the list with the addItem method.

  7. Progress Bar • Displays progress of operation • Can be used like a gauge • Usage: • Initialize JProgressBar progressBar = new JProgressBar(); progressBar.setMinimum(0); progressBar.setMaximum(numberSubOperations); • Go progressBar.setValue(progressBar.getMinimum()); for (int i = 0; i < numberSubOperations; i++) { progressBar.setValue(i); performSubOperation(i); }

  8. Intro to The Progress Dialog Dynamic Feedback

  9. Why Dynamic Feedback? • Users interact more smoothly with your application if you keep them informed about the application's state. • Progress animation--an indicator such as a progress bar that shows what percentage of an operation is complete

  10. When? • Use a progress bar whenever users are blocked from interacting with the application for more than 6 seconds.

  11. Use • Users cannot interact with a progress bar. •  Update the progress bar to show the proportion completed at least every 4 seconds. • If you overestimate how much is already done, the progress bar can remain at 99 percent until the task is complete. • If you underestimate how much is already done, fill the remaining portion of the progress bar when the operation completes. • The percentage done should never decrease.

  12. Swing class hierarchy fragment Component AWT Swing Container JComponent Window Panel JWindow Dialog Frame JFrame JDialog JTree JPanel JLabel JTable JApplet

  13. Progress Bar • Displays progress of operation • Can be used like a gauge • Usage: • Initialize JProgressBar progressBar = new JProgressBar(); progressBar.setMinimum(0); progressBar.setMaximum(numberSubOperations); • Go progressBar.setValue(progressBar.getMinimum()); for (int i = 0; i < numberSubOperations; i++) { progressBar.setValue(i); performSubOperation(i); }

  14. JProgressBar Methods

  15. Example: JProgressBar Demo Objective: Write a GUI application that lets you copy files. A progress bar is used to show the progress of the copying operation. CopyFile Run

  16. Basic Controls • Menu related classes

  17. The Components • A subclass of the java.awt.Component class is called a Component • A Component has a size, color, etc. • A Component can normally be painted. • When a component is painted it is visible on the screen.

  18. Component Subclasses • Container extends Component • Button extends Component • Window extends Container • Frame extends a Window • Dialog extends a Window • Panel extends a Container • Applet extends a Panel, etc.

  19. Swing and AWT • Swing is built on JComponent. • JComponent extends Component • JLabel, JButton, JCheckbox, etc all extend JComponent • Why?

  20. Why does JComponent extend Component? • Swing uses LightWeight Components! • AWT uses HeavyWeight components! • What is the difference? • What is so good about a LightWeight Component? • What are the drawbacks?

  21. Do I still need AWT when using Swing? • Yes! • AWT has many useful tools. • Taken together they form a Framework.

  22. Basic components to gather input • JButton • JCheckBox a toggled on/off buttondisplaying state to user. • JRadioButtona toggled on/off buttondisplaying its state to user.

  23. basic components • JComboBox a drop-down list with optional editable text field. The user can key in a value or select a value from drop-down list. • Jlistallows a user to select one or more items from a list. • Jmenupopup list of items from which the user can select. • Jslider lets user select a value by sliding a knob. • JTextField area for entering a single line of input.

  24. Basic components to present information • JLabelcontains text string, an image, or both. • JProgressBarcommunicates progress of some work. • JToolTip describes purpose of another component. • JTreea component that displays hierarchical data in outline form. • JTablea component user to edit and display data in a two-dimensional grid. • JTextArea, JTextPane, JEditorPane • define multi-line areas for displaying, entering, and editing text.

  25. Swing components

  26. Container • Component that can contain other components and containers.

  27. Intermediate components • Used to organize and position other components. • JPanel container of components. • JScrollPane panel with scrollbars. • JSplitPane divides two components graphically. • JTabbedPane lets the user switch between a group of components by clicking on a labeled tab; • JToolBar used for displaying a set of commonly used controls.

  28. Example of component organization • The following creates a JPanel and adds two buttons, labeled “on” and “off.” • Wherever this panel is used, it will present the two buttons. JPanel p = new JPanel(); p.add(new JButton("on")); p.add(new JButton("off"));

  29. Getting the screen size public static Dimension getSize() { Toolkit t = Toolkit.getDefaultToolkit(); return t.getScreenSize(); }

  30. An example of AWT usage public class Screen { public static int getDpi() { Toolkit t = Toolkit.getDefaultToolkit(); return t.getScreenResolution(); }

  31. Top-level container • It’s not contained in any other container. • provide screen area where other components can display themselves. • JApplet, JDialog, JFrame, and JWindow are commonly used as top-level containers.

  32. JFrame • It’s a window with title, border, (optional) menu bar and user-specified components. • It can be moved, resized, iconified. • It is not a subclass of JComponent. • Delegates responsibility of managing user-specified components to a content pane, an instance of JPanel.

  33. Jframe Jframe Jframe internal structure

  34. JFrame • To add a component to a JFrame, add it to the content pane: JFrame f = new JFrame("A Frame"); JButton b = new JButton("Press"); Container cp = f.getContentPane(); cp.add(b)

  35. content pane JFrame Container * Component JFrame components are in its content pane.

  36. Swing has lightweight components Light weight components have no peers Look and feel variations available Swing is SLOW AWT is heavyweight heavyweight comps require peers. Always looks like the platform it runs on. AWT is FAST Swing vs AWT

  37. Simple Output, a message dialog public static void messageDialog(Object o) { JOptionPane.showMessageDialog(null, o); }

  38. Simple Input public class In { public static String getString(Object o) { return JOptionPane.showInputDialog(o); } How do we get an int?

  39. Getting an Int public static int getInt(Object o) { return Integer.parseInt( getString(o)); }

  40. JList • a component that allows a user to select one or more elements from a list String[] data = {"one", "two", ...}; JList list = new JList(data);

  41. JList • requires a ListSelectionListener that implements public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { //selected: list.getSelectedValue() } } • selection mode can be set to single, single interval, or multiple interval • list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

  42. Lists can be dynamic • suppose you want the list to change during execution of the program • use a ListModel (normally DefaultListModel) to contain the data

  43. dynamic jlist • create the list and pass in the ListModel as input • add or remove elements in the ListModel • the GUI will update the list dynamically DefaultListModel listModel = new DefaultListModel(); JList list = new JList(listModel); listModel.addElement(object); listModel.removeElement(i);

  44. Scroll Panes • sometimes the list will be too long to display in the GUI • Solution: display the list inside a JScrollPane • create a scroll pane containing the list • state how many rows you want to display • display the scroll pane, not the list • everything else is as before

  45. jsp JScrollPane sp = new JScrollPane(list); list.setVisibleRowCount(5); add(sp);

  46. Atomic IO • Atomic actions happen all at once. • Event driven call-backs can complicate code. • EmployeeRecord = getEmployeeRecord(); • How is getEmployeeRecord implemented?

  47. JDialog • Used to create custom dialog windows.

  48. A Jdialog • a top-level window. • has an owner, generally a frame. • It delegates component management to a content pane, to which components are added. • It’s displayed by invoking its setVisible method with an argument of true, and is hidden by invoking its setVisible method with an argument of false

  49. JDialog • A typical constructor is specified as follows: • Provides an object to create custom views to get or present data. public JDialog (Frame owner, String title,boolean modal)

  50. Implementing getXXX • get data – • from a file • from the web • from the user • from … • Hiding how the data is obtain encapuslates complexity.

More Related