InternalFrame Test : InternalFrame « 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 » InternalFrameScreenshots 
InternalFrame Test
InternalFrame Test
 
/**
 @version 1.00 1999-07-17
 @author Cay Horstmann
 */

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class InternalFrameTest2 {
  public static void main(String[] args) {
    JFrame frame = new DesktopFrame();
    frame.show();
  }
}

class DesktopFrame extends JFrame implements ActionListener,
    VetoableChangeListener {
  public DesktopFrame() {
    setTitle("InternalFrameTest");
    setSize(600400);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    desktop = new JDesktopPane();
    setContentPane(desktop);

    // set up menus

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    openItem = new JMenuItem("Open");
    openItem.addActionListener(this);
    fileMenu.add(openItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(this);
    fileMenu.add(exitItem);
    JMenu windowMenu = new JMenu("Window");
    menuBar.add(windowMenu);
    nextItem = new JMenuItem("Next");
    nextItem.addActionListener(this);
    windowMenu.add(nextItem);
    cascadeItem = new JMenuItem("Cascade");
    cascadeItem.addActionListener(this);
    windowMenu.add(cascadeItem);
    tileItem = new JMenuItem("Tile");
    tileItem.addActionListener(this);
    windowMenu.add(tileItem);
    dragOutlineItem = new JCheckBoxMenuItem("Drag Outline");
    dragOutlineItem.addActionListener(this);
    windowMenu.add(dragOutlineItem);
  }

  public void createInternalFrame(Component c, String t) {
    JInternalFrame iframe = new JInternalFrame(t, true, // resizable
        true, // closable
        true, // maximizable
        true)// iconifiable

    iframe.getContentPane().add(c);
    desktop.add(iframe);

    // iframe.setFrameIcon(new ImageIcon("document.gif"));

    // add listener to confirm frame closing
    iframe.addVetoableChangeListener(this);

    // position frame
    int width = desktop.getWidth() 2;
    int height = desktop.getHeight() 2;
    iframe.reshape(nextFrameX, nextFrameY, width, height);

    iframe.show();

    // select the frame--might be vetoed
    try {
      iframe.setSelected(true);
    catch (PropertyVetoException e) {
    }

    /*
     * if this is the first time, compute distance between cascaded frames
     */

    if (frameDistance == 0)
      frameDistance = iframe.getHeight()
          - iframe.getContentPane().getHeight();

    // compute placement for next frame

    nextFrameX += frameDistance;
    nextFrameY += frameDistance;
    if (nextFrameX + width > desktop.getWidth())
      nextFrameX = 0;
    if (nextFrameY + height > desktop.getHeight())
      nextFrameY = 0;
  }

  public void cascadeWindows() {
    JInternalFrame[] frames = desktop.getAllFrames();
    int x = 0;
    int y = 0;
    int width = desktop.getWidth() 2;
    int height = desktop.getHeight() 2;

    for (int i = 0; i < frames.length; i++) {
      if (!frames[i].isIcon()) {
        try /*
             * try to make maximized frames resizable this might be
             * vetoed
             */
          frames[i].setMaximum(false);
          frames[i].reshape(x, y, width, height);

          x += frameDistance;
          y += frameDistance;
          // wrap around at the desktop edge
          if (x + width > desktop.getWidth())
            x = 0;
          if (y + height > desktop.getHeight())
            y = 0;
        catch (PropertyVetoException e) {
        }
      }
    }
  }

  public void tileWindows() {
    JInternalFrame[] frames = desktop.getAllFrames();

    // count frames that aren't iconized
    int frameCount = 0;
    for (int i = 0; i < frames.length; i++) {
      if (!frames[i].isIcon())
        frameCount++;
    }

    int rows = (intMath.sqrt(frameCount);
    int cols = frameCount / rows;
    int extra = frameCount % rows;
    // number of columns with an extra row

    int width = desktop.getWidth() / cols;
    int height = desktop.getHeight() / rows;
    int r = 0;
    int c = 0;
    for (int i = 0; i < frames.length; i++) {
      if (!frames[i].isIcon()) {
        try {
          frames[i].setMaximum(false);
          frames[i].reshape(c * width, r * height, width, height);
          r++;
          if (r == rows) {
            r = 0;
            c++;
            if (c == cols - extra) { // start adding an extra row
              rows++;
              height = desktop.getHeight() / rows;
            }
          }
        catch (PropertyVetoException e) {
        }
      }
    }
  }

  public void selectNextWindow() {
    JInternalFrame[] frames = desktop.getAllFrames();
    for (int i = 0; i < frames.length; i++) {
      if (frames[i].isSelected()) { /*
                       * find next frame that isn't an icon
                       * and can be selected
                       */
        try {
          int next = i + 1;
          while (next != i && frames[next].isIcon())
            next++;
          if (next == i)
            return;
          // all other frames are icons or veto selection
          frames[next].setSelected(true);
          frames[next].toFront();
          return;
        catch (PropertyVetoException e) {
        }
      }
    }
  }

  public void vetoableChange(PropertyChangeEvent event)
      throws PropertyVetoException {
    JInternalFrame iframe = (JInternalFrameevent.getSource();
    String name = event.getPropertyName();
    Object value = event.getNewValue();

    // we only want to check attempts to close a frame

    if (name.equals("closed"&& value.equals(Boolean.TRUE)) { // ask user
                                   // if it is
                                   // ok to
                                   // close
      int result = JOptionPane.showInternalConfirmDialog(iframe,
          "OK to close?");

      // if the user doesn't agree, veto the close
      if (result == JOptionPane.CANCEL_OPTION)
        throw new PropertyVetoException("User canceled close", event);
    }
  }

  public Component createEditorPane(URL u) { // create an editor pane that
                         // follows hyperlink clicks

    JEditorPane editorPane = new JEditorPane();
    editorPane.setEditable(false);
    editorPane.addHyperlinkListener(new HyperlinkListener() {
      public void hyperlinkUpdate(HyperlinkEvent event) {
        createInternalFrame(createEditorPane(event.getURL()), event
            .getURL().toString());
      }
    });
    try {
      editorPane.setPage(u);
    catch (IOException e) {
      editorPane.setText("Error: " + e);
    }
    return new JScrollPane(editorPane);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == openItem) { // let user select file

      JFileChooser chooser = new JFileChooser();
      chooser.setCurrentDirectory(new File("."));
      chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
          String fname = f.getName().toLowerCase();
          return fname.endsWith(".html"|| fname.endsWith(".htm")
              || f.isDirectory();
        }

        public String getDescription() {
          return "HTML Files";
        }
      });
      int r = chooser.showOpenDialog(this);

      if (r == JFileChooser.APPROVE_OPTION) { // open the file that the
                          // user selected

        String filename = chooser.getSelectedFile().getPath();
        try {
          URL fileUrl = new URL("file:" + filename);
          createInternalFrame(createEditorPane(fileUrl), filename);
        catch (MalformedURLException e) {
        }
      }
    else if (source == exitItem)
      System.exit(0);
    else if (source == nextItem)
      selectNextWindow();
    else if (source == cascadeItem)
      cascadeWindows();
    else if (source == tileItem)
      tileWindows();
    else if (source == dragOutlineItem) {
      desktop.putClientProperty("JDesktopPane.dragMode", dragOutlineItem
          .isSelected() "outline" null);
    }
  }

  private JDesktopPane desktop;

  private JMenuItem openItem;

  private JMenuItem exitItem;

  private JMenuItem nextItem;

  private JMenuItem cascadeItem;

  private JMenuItem tileItem;

  private JMenuItem dragOutlineItem;

  private int nextFrameX;

  private int nextFrameY;

  private int frameDistance;
}
           
         
  
Related examples in the same category
1. A quick demonstration of setting up an internal frame in an applicationA quick demonstration of setting up an internal frame in an application
2. A simple extension of the JInternalFrame class that contains a list
3. JDesktopPane demoJDesktopPane demo
4. Working with Internal Frames within a Desktop
5. Internal Frames DemoInternal Frames Demo
6. JDesktopPane Cascade DemoJDesktopPane Cascade Demo
7. Java X WindowsJava X Windows
8. Desktop Manager DemoDesktop Manager Demo
9. Internal Frame Listener Demo Internal Frame Listener Demo
10. Layered Pane DemoLayered Pane Demo
11. LayeredPane Demo 2: Custom MDILayeredPane Demo 2: Custom MDI
12. LayeredPane Demo 3: Custom MDILayeredPane Demo 3: Custom MDI
13. LayeredPane Demo 4: Custom MDILayeredPane Demo 4: Custom MDI
14. Implements InternalFrameListenerImplements InternalFrameListener
15. InternalFrame demoInternalFrame demo
16. InternalFrameEvent DemoInternalFrameEvent Demo
17. A few interesting things using JInternalFrames, JDesktopPane, and DesktopManagerA few interesting things using JInternalFrames, JDesktopPane, and DesktopManager
18. A quick setting up an Internal Frame in an applicationA quick setting up an Internal Frame in an application
19. Interesting things using JInternalFrames, JDesktopPane, and DesktopManager 2Interesting things using JInternalFrames, JDesktopPane, and DesktopManager 2
20. Make a JInternalFrame a tool window
21. Move JInternalFrame To Front
w__ww__.j_a__va___2__s._c__o__m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.