FrameDemo2.java shows off the window decoration features added in 1.4 : Frame « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » FrameScreenshots 
FrameDemo2.java shows off the window decoration features added in 1.4
   
/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
/*
 * FrameDemo2.java shows off the window decoration features added in 1.4, plus
 * some window positioning code and (optionally) setIconImage. It uses the file
 * images/FD.jpg.
 */
public class FrameDemo2 extends WindowAdapter implements ActionListener {
  private Point lastLocation = null;
  private int maxX = 500;
  private int maxY = 500;

  // the main frame's default button
  private static JButton defaultButton = null;

  // constants for action commands
  protected final static String NO_DECORATIONS = "no_dec";
  protected final static String LF_DECORATIONS = "laf_dec";
  protected final static String WS_DECORATIONS = "ws_dec";
  protected final static String CREATE_WINDOW = "new_win";
  protected final static String DEFAULT_ICON = "def_icon";
  protected final static String FILE_ICON = "file_icon";
  protected final static String PAINT_ICON = "paint_icon";

  // true if the next frame created should have no window decorations
  protected boolean noDecorations = false;

  // true if the next frame created should have setIconImage called
  protected boolean specifyIcon = false;

  // true if the next frame created should have a custom painted icon
  protected boolean createIcon = false;

  // Perform some initialization.
  public FrameDemo2() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    maxX = screenSize.width - 50;
    maxY = screenSize.height - 50;
  }

  // Create a new MyFrame object and show it.
  public void showNewWindow() {
    JFrame frame = new MyFrame();

    // Take care of the no window decorations case.
    // NOTE: Unless you really need the functionality
    // provided by JFrame, you would usually use a
    // Window or JWindow instead of an undecorated JFrame.
    if (noDecorations) {
      frame.setUndecorated(true);
    }

    // Set window location.
    if (lastLocation != null) {
      // Move the window over and down 40 pixels.
      lastLocation.translate(4040);
      if ((lastLocation.x > maxX|| (lastLocation.y > maxY)) {
        lastLocation.setLocation(00);
      }
      frame.setLocation(lastLocation);
    else {
      lastLocation = frame.getLocation();
    }

    // Calling setIconImage sets the icon displayed when the window
    // is minimized. Most window systems (or look and feels, if
    // decorations are provided by the look and feel) also use this
    // icon in the window decorations.
    if (specifyIcon) {
      if (createIcon) {
        frame.setIconImage(createFDImage())// create an icon from scratch
      else {
        frame.setIconImage(getFDImage())// get the icon from a file
      }
    }

    // Show window.
    frame.setSize(new Dimension(170100));
    frame.setVisible(true);
  }

  // Create the window-creation controls that go in the main window.
  protected JComponent createOptionControls() {
    JLabel label1 = new JLabel(
        "Decoration options for subsequently created frames:");
    ButtonGroup bg1 = new ButtonGroup();
    JLabel label2 = new JLabel("Icon options:");
    ButtonGroup bg2 = new ButtonGroup();

    // Create the buttons
    JRadioButton rb1 = new JRadioButton();
    rb1.setText("Look and feel decorated");
    rb1.setActionCommand(LF_DECORATIONS);
    rb1.addActionListener(this);
    rb1.setSelected(true);
    bg1.add(rb1);
    //
    JRadioButton rb2 = new JRadioButton();
    rb2.setText("Window system decorated");
    rb2.setActionCommand(WS_DECORATIONS);
    rb2.addActionListener(this);
    bg1.add(rb2);
    //
    JRadioButton rb3 = new JRadioButton();
    rb3.setText("No decorations");
    rb3.setActionCommand(NO_DECORATIONS);
    rb3.addActionListener(this);
    bg1.add(rb3);
    //
    //
    JRadioButton rb4 = new JRadioButton();
    rb4.setText("Default icon");
    rb4.setActionCommand(DEFAULT_ICON);
    rb4.addActionListener(this);
    rb4.setSelected(true);
    bg2.add(rb4);
    //
    JRadioButton rb5 = new JRadioButton();
    rb5.setText("Icon from a JPEG file");
    rb5.setActionCommand(FILE_ICON);
    rb5.addActionListener(this);
    bg2.add(rb5);
    //
    JRadioButton rb6 = new JRadioButton();
    rb6.setText("Painted icon");
    rb6.setActionCommand(PAINT_ICON);
    rb6.addActionListener(this);
    bg2.add(rb6);

    // Add everything to a container.
    Box box = Box.createVerticalBox();
    box.add(label1);
    box.add(Box.createVerticalStrut(5))// spacer
    box.add(rb1);
    box.add(rb2);
    box.add(rb3);
    //
    box.add(Box.createVerticalStrut(15))// spacer
    box.add(label2);
    box.add(Box.createVerticalStrut(5))// spacer
    box.add(rb4);
    box.add(rb5);
    box.add(rb6);

    // Add some breathing room.
    box.setBorder(BorderFactory.createEmptyBorder(10101010));

    return box;
  }

  // Create the button that goes in the main window.
  protected JComponent createButtonPane() {
    JButton button = new JButton("New window");
    button.setActionCommand(CREATE_WINDOW);
    button.addActionListener(this);
    defaultButton = button; // Used later to make this the frame's default
                            // button.

    // Center the button in a panel with some space around it.
    JPanel pane = new JPanel()// use default FlowLayout
    pane.setBorder(BorderFactory.createEmptyBorder(5555));
    pane.add(button);

    return pane;
  }

  // Handle action events from all the buttons.
  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();

    // Handle the New window button.
    if (CREATE_WINDOW.equals(command)) {
      showNewWindow();

      // Handle the first group of radio buttons.
    else if (NO_DECORATIONS.equals(command)) {
      noDecorations = true;
      JFrame.setDefaultLookAndFeelDecorated(false);
    else if (WS_DECORATIONS.equals(command)) {
      noDecorations = false;
      JFrame.setDefaultLookAndFeelDecorated(false);
    else if (LF_DECORATIONS.equals(command)) {
      noDecorations = false;
      JFrame.setDefaultLookAndFeelDecorated(true);

      // Handle the second group of radio buttons.
    else if (DEFAULT_ICON.equals(command)) {
      specifyIcon = false;
    else if (FILE_ICON.equals(command)) {
      specifyIcon = true;
      createIcon = false;
    else if (PAINT_ICON.equals(command)) {
      specifyIcon = true;
      createIcon = true;
    }
  }

  // Creates an icon-worthy Image from scratch.
  protected static Image createFDImage() {
    // Create a 16x16 pixel image.
    BufferedImage bi = new BufferedImage(1616, BufferedImage.TYPE_INT_RGB);

    // Draw into it.
    Graphics g = bi.getGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(001515);
    g.setColor(Color.RED);
    g.fillOval(5366);

    // Clean up.
    g.dispose();

    // Return it.
    return bi;
  }

  // Returns an Image or null.
  protected static Image getFDImage() {
    java.net.URL imgURL = FrameDemo2.class.getResource("images/FD.jpg");
    if (imgURL != null) {
      return new ImageIcon(imgURL).getImage();
    else {
      return null;
    }
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    // Use the Java look and feel.
    try {
      UIManager
          .setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    catch (Exception e) {
    }

    // Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);

    // Instantiate the controlling class.
    JFrame frame = new JFrame("FrameDemo2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    FrameDemo2 demo = new FrameDemo2();

    // Add components to it.
    Container contentPane = frame.getContentPane();
    contentPane.add(demo.createOptionControls(), BorderLayout.CENTER);
    contentPane.add(demo.createButtonPane(), BorderLayout.PAGE_END);
    frame.getRootPane().setDefaultButton(defaultButton);

    // Display the window.
    frame.pack();
    frame.setLocationRelativeTo(null)// center it
    frame.setVisible(true);
  }

  // Start the demo.
  public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  class MyFrame extends JFrame implements ActionListener {

    // Create a frame with a button.
    public MyFrame() {
      super("A window");
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);

      // This button lets you close even an undecorated window.
      JButton button = new JButton("Close window");
      button.addActionListener(this);

      // Place the button near the bottom of the window.
      Container contentPane = getContentPane();
      contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
      contentPane.add(Box.createVerticalGlue())// takes all extra space
      contentPane.add(button);
      button.setAlignmentX(Component.CENTER_ALIGNMENT)// horizontally centered
      contentPane.add(Box.createVerticalStrut(5))// spacer
    }

    // Make the button do the same thing as the default close operation
    // (DISPOSE_ON_CLOSE).
    public void actionPerformed(ActionEvent e) {
      setVisible(false);
      dispose();
    }
  }
}

   
    
    
  
Related examples in the same category
1. The Swing Version of the Hello, World! Program
2. Using anonymous inner classesUsing anonymous inner classes
3. No direct interaction with JRootPaneNo direct interaction with JRootPane
4. A demonstration of the WindowListener approach to closing JFramesA demonstration of the WindowListener approach to closing JFrames
5. How to center a frame or dialogHow to center a frame or dialog
6. React to frame close actionReact to frame close action
7. Create a frameCreate a frame
8. Draw on frame with keyboardDraw on frame with keyboard
9. Frame dialog data exchangeFrame dialog data exchange
10. Frame with componentsFrame with components
11. Default button for frame: press Enter to activateDefault button for frame: press Enter to activate
12. Frame Icon drawn by yourselfFrame Icon drawn by yourself
13. Create a JFrame class in a thread-safe way
14. Demo to show a way of having More Choices or Less ChoicesDemo to show a way of having More Choices or Less Choices
15. Frame Icon from gifFrame Icon from gif
16. FrameDemo:invoked from the event-dispatching thread FrameDemo:invoked from the event-dispatching thread
17. Screen edge snapping
18. Creating a Borderless Window
19. Disabling the Close Button on a JFrame
20. Exiting an Application When a JFrame Is Closed
21. Use Component listener to ensure frame visibilities
22. Drag and move a frame from its content area
23. Setting the Icon for a Frame
24. Removing the Title Bar of a Frame
25. Setting the Bounds for a Maximized Frame
26. Iconifies a frame; the maximized bits are not affected.
27. Deiconifies a frame; the maximized bits are not affected.
28. Hiding a Frame When Its Close Button Is Clicked
29. Exiting an Application When a Frame Is Closed
30. Getting All Created Frames in an Application
31. Maximizes a frame; the iconified bit is not affected
32. Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size.
33. Determining When a Frame or Window Is Opened or Closed
34. Determining When a Frame or Window Is Iconized or Maximized
35. Determining When a Component Has Been Made Visible, Moved, or Resized
36. Get the JFrame of a component
37. Disable JFrame close button
38. Handle JFrame window events
39. Define the default close operation of a JFrame to EXIT_ON_CLOSE, application will exit by calling System.exit()
40. Center a JFrame
41. Creating Frames with a background image
42. Preventing a Window from Gaining the Focus
43. Create undecorated frame
44. Make a JFrame always visible
45. Maximize a JFrame
46. Close a JFrame under condition
47. Creating a Titleless and Borderless JFrame
48. JFrame that closes when someone presses the ESC key
49. Show the given frame as modal to the specified owner
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.