Introduction to Swing

Introduction to Swing

Introduction to Swing Mark Allen Weiss Copyright 1999 12/5/99 1 Outline of Topics ● Java Foundation Classes ● Why Use Swing? ● Converting AWT to Swing ● New Swing Components ● Look and Feel Sunday, December 05, 1999 Copyright 1999, M. A. Weiss 2 The Java Foundation Classes (JFC) ● Swing (javax.swing) ● Java 2D: Handle graphics. If you're not a graphics person, this stuff makes little sense ● Drag and Drop ● Accessibility: Set accessible context for components so they can be used by special needs people Sunday, December 05, 1999 Copyright 1999, M. A. Weiss 3 What’s in Java-2D ● Graphics: – Antialiased rendering – Bezier paths – Coordinate Transforms – Compositing – Richer text attributes – Arbitrary fill styles ● Text – Extended font support – Advanced text layout – Antialiased text rendering Sunday, December 05, 1999 Copyright 1999, M. A. Weiss 4 Java-2D Continued ● Images – Flexible in-memory image layouts – Extended image filters, such as convolution Lookup tables, and affine transformation ● Devices – Hooks for supporting arbitrary graphics devices such as printers and screens – Color Management: – ICC profile support – Color conversion from different color spaces – Arbitrary color spaces Monday, December 06, 1999 Copyright 1999, M. A. Weiss 5 Why Swing? ● Better, more flexible GUIs. ● Faster. Uses “lightweight components” ● Automatic keyboard navigation ● Easy scrolling ● Easy tooltips ● Mnemonics ● General attempt to compete with WFC ● Can set custom look-and-feel Monday, December 06, 1999 Copyright 1999, M. A. Weiss 6 Swing Components ● Usually start with ‘J’: ● All components are lightweight (written in Java) except: – JApplet – JFrame – JWindow – JDialog ● AWT components have Swing analogs Monday, December 06, 1999 Copyright 1999, M. A. Weiss 7 AWT to Swing Mappings ● Almost all map by prepending a ‘J’ ● Examples: – Button -> JButton – Panel -> JPanel – List -> JList ● Exceptions: – Checkbox -> JCheckBox (note case change) – Choice -> JComboBox Monday, December 06, 1999 Copyright 1999, M. A. Weiss 8 Some New Components ● JTree – Creates a tree, whose nodes can be expanded. – Vast, complicated class ● JTable – Used to display tables (for instance, those obtained from databases) – Another enormous class Monday, December 06, 1999 Copyright 1999, M. A. Weiss 9 Big Difference Between Swing & AWT ● Components cannot be added to a heavyweight component directly. Use getContentPane() or setContentPane(). ● Example: JFrame f = new JFrame("Swing Frame"); JPanel p = new JPanel( ); p.add( new JButton( "Quit" ) ); f.setContentPane( p ); --- OR (NOT PREFERRED) --- Container c = f.getContentPane( ); c.add( new JButton( "Quit" ) ); Monday, December 06, 1999 Copyright 1999, M. A. Weiss 10 Buttons, Checkboxes, etc ● Abstract class AbstractButton covers all buttons, checkboxes, radio groups, etc. ● Concrete classes include JButton, BasicArrowButton, JToggleButton, JCheckBox, JRadioButton. ● Can add images to buttons. ● Can set mnemonics (so alt-keys work) ImageIcon icon=new ImageIcon("quit.gif"); JButton b = new JButton("Quit", icon); b.setMnemonic('q'); Monday, December 06, 1999 Copyright 1999, M. A. Weiss 11 Tooltips ● Use setToolTipText to install a tooltip. ● Works for any JComponent. Jbutton b = new Jbutton( "Quit" ); b.setToolTipText("Press to quit"); Monday, December 06, 1999 Copyright 1999, M. A. Weiss 12 Borders ● Use setBorder to set borders for a JComponent. ● Available borders include – TitledBorder – EtchedBorder – LineBorder – MatteBorder – BevelBorder – SoftBevelBorder – CompoundBorder ● In package javax.swing.border Monday, December 06, 1999 Copyright 1999, M. A. Weiss 13 Popup Menus and Dialogs ● JPopupMenu – Appears when you right-click on a component ● JOptionPane – Contains static methods that pop up a modal dialog. Commonly used methods are: showMessageDialog( ) showConfirmDialog( ) showInputDialog( ) Monday, December 06, 1999 Copyright 1999, M. A. Weiss 14 Sliders ● Sliders are implemented with the JSlider class. ● Important methods: JSlider( int orient, int low, int high, int val ); void setValue( int val ); int getValue( ); void setPaintTicks( boolean makeTicks ); void setMajorTickSpacing( int n ); void setMinorTickSpacing( int n ); ● ChangeListener interface handles slider events. Must implement stateChanged method. Note: this interface is in package javax.swing.event. Monday, December 06, 1999 Copyright 1999, M. A. Weiss 15 Progress Bars ● JProgressBar displays data in relative fashion from empty (default value=0) to full (default value=100). ● Interesting methods double getPercentComplete( ); int getValue( ); void setValue( int val ); Monday, December 06, 1999 Copyright 1999, M. A. Weiss 16 Scrolling ● Use a JScrollPane to wrap any Component. ● Scrolling will be automatically done. ● Example: JPanel p = new JPanel( ); JList list = new JList( ); for( int i = 0; i < 100; i++ ) list.addItem( "" + i ); p.add( new JScrollPane( list ) ); Monday, December 06, 1999 Copyright 1999, M. A. Weiss 17 Look-and-feel ● Used to give your GUIs a custom look. Currently there are three options: – Metal (platform independent) – Windows – Motif (X-windows, for Unix boxes) ● Use static method setLookandFeel in UIManager to set a custom look and feel. static String motifClassName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; try { UIManager.setLookAndFeel(motifClassName); } catch( UnsupportedLookAndFeelException exc ) { System.out.println( "Unsupported Look and Feel" ); } Monday, December 06, 1999 Copyright 1999, M. A. Weiss 18 Other Stuff (There's Lots) ● JFileChooser: supports file selection ● JPasswordField: hides input ● Swing 1.1.1 Beta 2 makes many components HTML-aware. Can do simple HTML formatting and display ● JTextPane: text editor that supports formatting, images, word wrap ● Springs and struts ● Automatic double-buffering ● General attempts to be competitive with WFC Monday, December 06, 1999 Copyright 1999, M. A. Weiss 19 Summary ● Most old AWT is easily translated: – Add J in front of the class names – Remember to have JFrame call setContentPane ● Easy-to-use new stuff includes tooltips, mneumonics, borders, JOptionPane. ● Methods use the set/get conventions. ● Swing is easily an entire book. Probably two. Monday, December 06, 1999 Copyright 1999, M. A. Weiss 20.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us