dcsimg
December 4, 2016
Hot Topics:

Implications of Swing's pluggable look and feel

  • February 12, 1998
  • By Ted M. Young
  • Send Email »
  • More Articles »

/**
 ** ToolBar Demo showing how to float/unfloat a dockable toolbar using the "Basic" ToolBar UI
 **
 ** Note that this code works with the JDK 1.1.x (1.1.5 was actually tested) and Swing 0.7.1.
 **
 ** @Author Ted M. Young <tyoung@javatrain.com> URL: http://www.javatrain.com
 ** @Date January 29, 1997
 **
 ** This code may be freely used and disseminated, no acknowledgements necessary.
 ** No warranties are implied, use at your own risk.
 **
 **/

// import for layout
import java.awt.BorderLayout;

// import for responding to events
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

// the Swing components that we use
import com.sun.java.swing.JFrame;
import com.sun.java.swing.JToolBar;
import com.sun.java.swing.JButton;

// the UIs behind the JToolBar
import com.sun.java.swing.basic.BasicToolBarUI;
import com.sun.java.swing.plaf.ToolBarUI;

public class ToolBarDemo extends JFrame {

   JToolBar m_tb = null;   // our ToolBar

   public ToolBarDemo() {  // constructor

      // set the title for this frame
      super( "Docking and Floating Toolbar Demo" );

      // a button to fill up some space
      JButton btnDontPushMe = new JButton( "Don't Push Me" );
      btnDontPushMe.setEnabled( false );  // we don't need to enable it

      // the 'float me' button
      JButton btnFloat = new JButton( "Float" );
      // add a listener to do something when we click on this button
      btnFloat.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e ) {
            // get the toolbar's user interface
            ToolBarUI tbUI = m_tb.getUI();
            // the BasicToolBarUI supports the idea of floating
            if ( tbUI instanceof BasicToolBarUI ) {
               // set a location other than the very upper-left of the screen
               ( ( BasicToolBarUI ) tbUI).setFloatingLocation( 50, 50 );
               // and float away -- the null is to match the Point parameter which we don't use
               ( ( BasicToolBarUI ) tbUI).setFloating( true, null );
            }
         }
      });

      // same thing as the float button, but this time we turn the floating off
      JButton btnUnfloat = new JButton( "Unfloat" );
      btnUnfloat.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            ToolBarUI tbUI = m_tb.getUI();
            if (tbUI instanceof BasicToolBarUI) {
               ( ( BasicToolBarUI ) tbUI).setFloating(false, null);
            }
         }
      });

      // always nice to provide an exit
      JButton btnExit = new JButton( "Exit" );
      btnExit.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e ) {
            System.exit(0);
         }
      });

      // let's create the toolbar and add the above buttons to it
      m_tb = new JToolBar();

      m_tb.add( btnDontPushMe );
      m_tb.addSeparator();

      m_tb.add( btnFloat );
      m_tb.addSeparator();

      m_tb.add( btnUnfloat );      
      m_tb.addSeparator();

      m_tb.add( btnExit );
      
      // Default layout for the content pane is BorderLayout
      // remember, we need to add to the content pane in a JFrame
      getContentPane().add( m_tb, BorderLayout.NORTH );
   }

   /**
    *  Create the ToolBarDemo frame and display it
    **/
   public static void main( String[] args ) {
   
      JFrame frmTest = new ToolBarDemo();
      frmTest.addWindowListener( new WindowAdapter() {
         public void windowClosing( WindowEvent e ) {
            System.exit( 0 );
         }
       });

       // Set the size of the JFrame and show it
       frmTest.setSize( 400, 200 );
       frmTest.setVisible( true );
      
   }

}






Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel