1.4k likes | 1.63k Views
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.
E N D
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
Other basic classes • AWT classes defined in the package java.awt • Color, ( an immutable class) • Point, • Dimension, • Font
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.
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); }
Intro to The Progress Dialog Dynamic Feedback
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
When? • Use a progress bar whenever users are blocked from interacting with the application for more than 6 seconds.
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.
Swing class hierarchy fragment Component AWT Swing Container JComponent Window Panel JWindow Dialog Frame JFrame JDialog JTree JPanel JLabel JTable JApplet
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); }
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
Basic Controls • Menu related classes
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.
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.
Swing and AWT • Swing is built on JComponent. • JComponent extends Component • JLabel, JButton, JCheckbox, etc all extend JComponent • Why?
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?
Do I still need AWT when using Swing? • Yes! • AWT has many useful tools. • Taken together they form a Framework.
Basic components to gather input • JButton • JCheckBox a toggled on/off buttondisplaying state to user. • JRadioButtona toggled on/off buttondisplaying its state to user.
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.
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.
Container • Component that can contain other components and containers.
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.
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"));
Getting the screen size public static Dimension getSize() { Toolkit t = Toolkit.getDefaultToolkit(); return t.getScreenSize(); }
An example of AWT usage public class Screen { public static int getDpi() { Toolkit t = Toolkit.getDefaultToolkit(); return t.getScreenResolution(); }
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.
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.
Jframe Jframe Jframe internal structure
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)
content pane JFrame Container * Component JFrame components are in its content pane.
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
Simple Output, a message dialog public static void messageDialog(Object o) { JOptionPane.showMessageDialog(null, o); }
Simple Input public class In { public static String getString(Object o) { return JOptionPane.showInputDialog(o); } How do we get an int?
Getting an Int public static int getInt(Object o) { return Integer.parseInt( getString(o)); }
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);
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);
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
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);
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
jsp JScrollPane sp = new JScrollPane(list); list.setVisibleRowCount(5); add(sp);
Atomic IO • Atomic actions happen all at once. • Event driven call-backs can complicate code. • EmployeeRecord = getEmployeeRecord(); • How is getEmployeeRecord implemented?
JDialog • Used to create custom dialog windows.
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
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)
Implementing getXXX • get data – • from a file • from the web • from the user • from … • Hiding how the data is obtain encapuslates complexity.