Chapter 13 • Swing and AWT – AWT is too primitive for modern work – It is also possible to use WFC – Windows Foundation Classes, but our author doesn’t talk about that, and you loose platform-independence. • SWT Simple Widgets Toolkit – an open-source toolkit often used in Eclipse. Swing Goodies • Read-only text fields – use setEditable(False) to turn off editiability – How is it different from a label? Just appearance. • Lists – JList takes an array of objects (usually Strings) as its argument. – Allows the user to select from a list of items. List Selection Modes • Selection modes – listName.SetSelectionMode(…) • The modes are: – ListSelectionModel.SINGLE_SELECTION User can only select one. – ListSelectionModel.INTERVAL_SELECTION User can select an interval with a shift-click – ListSelectionModel.MULTIPLE_INTERVAL_SELECTION Use can use control-clicks, I presume List Events • Must implement ListSelectionListener interface • Must have a method named valueChanged, which takes an argument of ListSelectionEvent type. • .getSelectedValue () often cast to (String) • .getSelectedIndex() is -1 if no selection Other List considerations • May want a border – Use .setBorder and BorderFactory… • Scroll Bars (Not the most convenient interface) 1. Use .setVisibleRowCount(int n) 2. JScrollPane name = new JScrollPane(nameList) 3. Add the JScrollPane to your panel or other container. • Void setListData(Object [] data) Getting the results of other selection modes • Object[] getSelectedValues() gets selections • Int[] getSelectedIndices() gets indices of selections. Not indexes – is this Java or Latin? Chapter 13 part 2 • Combo Boxes – This lets a user select an item from a drop-down list. – The constructor takes an array of objects, usually strings. Dealing with Combo Box results • You can, if you want, respond to events • Usually, you wait until some other button is pressed and retrieve the user’s selection. • object getSelectedItem() often cast to (String) • int getSelectedIndex() can be more useful Editable Combo Boxes • For “Have it your way” user interfaces • nameBox.setEditable(true) • This makes getSelectedIndex() a bit less useful (If the user puts something in, it returns -1) Images in labels and buttons • For labels, use ImageIcon class to get objects: ImageIcon myImage = new ImageIcon(thePath); JLabel myLabel = new JLabel(myImage); (or) JLabel myLabel = new JLabel(“Rock On!”); myLabel.setIcon(MyImage); • For Buttons, same drill with JButton. Mnemonics • Mnemonics are also called “keyboard shortcuts” or “hot keys”. • Uses Alt-letter (Apple-Letter??) • Jbutton exitButton = new Jbutton(“Exit”); exitButton.setMnemonic(KeyEvent.VK_X); • Constants take form KeyEvent.VK_letter • If the letter chosen is in the component’s text, the first occurrence of that letter will appear underlined. Tool Tips • Another nice recent user interface innovation. • As the user “hovers” the mouse over a control, a pop-up text “tool tip” explains in a little more detail what that control does. • myControl.setToolTipText(String myString); Chapter 13 part 3 • Choosers (We’ve been using file choosers for some time) • JFileChooser constructors: – No-arg starts the choice process in “My Documents” or equivalent folder – 1-arg (String) sets the default folder in which to start JFileChooser methods • int showOpenDialog(Component parent) – Use null unless you really want to specify where the dialog box initially appears • int showSaveDialog(component parent) – Return variable is one of: • JFileChooser.CANCEL_OPTION user hit ‘cancel’ • JFileChooser.APPROVE_OPTION user picked a file/folder • JFileChooser.ERROR_OPTION something ‘went south’ • void setFileSelectionMode(mode) – JFileChooser.DIRECTORIES_ONLY – JFileChooser.FILES_AND_DIRECTORIES – JFileChooser.FILES_ONLY (default) Menus • • • • • • • Menu bar Menu Menu item Check box menu item Radio button menu item Submenu (fly-out) Separator bar (visual/logical grouping) Menu Classes • • • • • JMenuItem (action event when clicked) JCheckBoxMenuItem isSelected() method JRadioButtonMenuItem also has isSelected() JMenu contains above classes JMenuBar contains Jmenu classes JTextArea • Two constructors: – JTextArea(int rows, int columns) – JTextArea(String text, int rows, int columns) • Methods: – Void setText(String toDisplay) – String getText() – Void setLineWrap(boolean wrapIt) – Void setFont(Font myFont) • Frequently embedded in JScrollPane Controlling Scrolling • setHorizontalScrollbarPolicy(…) – JScrollPane.HORIZONTAL.SCROLLBAR_AS_NEEDED – JScrollPane.HORIZONTAL.SCROLLBAR_NEVER – JScrollPane.HORIZONTAL.SCROLLBAR_ALWAYS • setVerticalScrollbarPolicy(…) – JScrollPane.VERTICAL.SCROLLBAR_AS_NEEDED – JScrollPane.VERTICAL.SCROLLBAR_NEVER – JScrollPane.VERTICAL.SCROLLBAR_ALWAYS • Window Builder makes this a lot easier! Chapter 13 part 4 • Sliders – A component that allows the user to adjust a number graphically within a range of values. • JSlider constructor JSlider(int orientation, int minvalue, int maxvalue, int initialvalue) • Use – as usual, you can hook events (use a method named stateChanged, or you use getValue() method. • Hooking events is nice for interactive graphics displays. Sliders • setMajorTickSpacing(int howFar) get numbers but only if you setPaintLabels(true) • setMinorTickSpacing(int howFar) just tics but only if you setPaintTicks(true) Look and Feel • Call UIManager.setLookAndFeel with a fully qualified path to a class. • Then invoke SwingUtilities.updateComponentTreeUI(this); • The author shows three example LookAndFeel classes: Metal, Motif and Windows. • Regretfully, this doesn’t give access to Windows chooser goodies.