1 / 12

CS202 Java Object Oriented Programming GUI Programming – More GUI Components

CS202 Java Object Oriented Programming GUI Programming – More GUI Components. Chengyu Sun California State University, Los Angeles. Important stuff Menu bar, menu, and menu items Toolbar Scroll Pane Dialog Box FileChooser. Optional stuff Tabbed Pane Popup Menu ChangeListener

bgillum
Download Presentation

CS202 Java Object Oriented Programming GUI Programming – More GUI Components

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. CS202 Java Object Oriented ProgrammingGUI Programming – More GUI Components Chengyu Sun California State University, Los Angeles

  2. Important stuff Menu bar, menu, and menu items Toolbar Scroll Pane Dialog Box FileChooser Optional stuff Tabbed Pane Popup Menu ChangeListener KeyListener … JEdit – A Simple Text Editor

  3. Swing Components HOWTO • http://java.sun.com/docs/books/tutorial/uiswing/components/componentlist.html

  4. Top-level Container Layout Content Pane Title Menu

  5. Menu Bar JMenuBar() add( JMenu ) Menu JMenu( String ) add( JMenuItem ) Menu Bar and Menu JMenuBar mb = new JMenuBar(); JMenu fileMenu = new JMenu (“File”); mb.add( fileMenu ); setMenuBar( mb ); // add a menu bar fileMenu.setMnemonic( KeyEvent.VK_F ); // add a hotkey

  6. Menu Items • JMenuItem( String ) • JMenuItem( String, int ) JMenuItem miOpen = new JMenuItem( “Open...”, KeyEvent.VK_O ); JMenuItem miSave = new JMenuItem( “Save...”, KeyEvent.VK_S ); JMenuItem miExit = new JMenuItem( “Exit” ); fileMenu.add( miOpen ); // add a menu item fileMenu.add( miSave ); fileMenu.addSeparator(); // add a menu separator fileMenu.add( miExit );

  7. Tool Bar and Tool Tip • JToolBar( String title ) • setToolTipText( String ) in JComponent JToolBar tb = new JToolBar( “Standard” ); Container cp = getContentPane(); cp.add( tb, BorderLayout.NORTH ); JButton ob = new JButton( "o" ); ob.setTipTipText( “Open File” );

  8. Scroll Pane • Provides a scrollable view of a component • JScrollPane( Component ) Container cp = getContentPane(); JTextArea ta = new JTextArea(); cp.add( (new JScrollPane(ta)), BorderLayout.CENTER );

  9. File Chooser • JFileChooser() • showDialog( Component, String ) • showOpenDialog( Component ) • showSaveDailog( Component ) • getSelectedFile() JFileChooser fc = new JFileChooser(); int status = fc.showOpenDialog( this ); if( status == JFileChooser.APPROVE_OPTION ) { File f = fc.getSelectedFile(); }

  10. Dialog Boxes • JColorChooser, JFileChooser • Standard dialog boxes using JOptionPane • showConfirmDialog • showInputDialog • showMessageDialog • showOptionDialog • JDialog for more complex and/or non-modal dialog boxes

  11. Tabbed Pane • JTabbedPane() • addTab( String, Component ) • getSelectedIndex() • addChangeListener( ChangeListener ) JTabbedPane tp = new JTabbedPane(); JTextArea ta1 = new JTextArea(); JTextArea ta2 = new JTextArea(); tp.addTab( “file1”, new JScrollPane(ta1) ); tp.addTab( “file2”, new JScrollPane(ta2) ); getContentPane().add( tp, BorderLayout.CENTER );

  12. Pop-up Menu • JPopupMenu() • add( JMenuItem ) • addSeparator() • show( Component, int, int ) • isPopupTrigger() in MouseEvent class • should be checked in mousePressed() and mouseReleased() • but not mouseClicked()

More Related