A simple extension of the JInternalFrame class that contains a list : InternalFrame « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » InternalFrameScreenshots 
A simple extension of the JInternalFrame class that contains a list

/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// SiteFrame.java
//A simple extension of the JInternalFrame class that contains a list
//object. Elements of the list represent HTML pages for a web site.
//

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SiteFrame extends JInternalFrame {

  JList nameList;

  SiteManager parent;

  // Hardcode the pages of our "site" to keep things simple
  String[] pages = "index.html""page1.html""page2.html" };

  public SiteFrame(String name, SiteManager sm) {
    super("Site: " + name, true, true, true);
    parent = sm;
    setBounds(5050250100);

    nameList = new JList(pages);
    nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    nameList.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent lse) {
        // We know this is the list, so pop up the page.
        if (!lse.getValueIsAdjusting()) {
          parent.addPageFrame((StringnameList.getSelectedValue());
        }
      }
    });
    Container contentPane = getContentPane();
    contentPane.add(nameList, BorderLayout.CENTER);
  }
}

//A sample Swing application that manages several internal frames. This
//is the main class for working with the SiteFrame and PageFrame classes.
//

class SiteManager extends JFrame {

  JLayeredPane desktop;

  Vector popups = new Vector();

  public SiteManager() {
    super("Web Site Manager");
    setSize(450250);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();

    JToolBar jtb = new JToolBar();
    jtb.add(new CutAction(this));
    jtb.add(new CopyAction(this));
    jtb.add(new PasteAction(this));
    contentPane.add(jtb, BorderLayout.NORTH);

    // Add our LayeredPane object for the internal frames.
    desktop = new JDesktopPane();
    contentPane.add(desktop, BorderLayout.CENTER);
    addSiteFrame("Sample");
  }

  public static void main(String args[]) {
    SiteManager mgr = new SiteManager();
    mgr.setVisible(true);
  }

  // Methods to create our internal frames
  public void addSiteFrame(String name) {
    SiteFrame sf = new SiteFrame(name, this);
    popups.addElement(sf);
    desktop.add(sf, new Integer(2))// Keep sites on top for now
    sf.setVisible(true);
  }

  public void addPageFrame(String name) {
    PageFrame pf = new PageFrame(name, this);
    desktop.add(pf, new Integer(1));
    pf.setVisible(true);
    pf.setIconifiable(true);
    popups.addElement(pf);
  }

  public JInternalFrame getCurrentFrame() {
    for (int i = 0; i < popups.size(); i++) {
      JInternalFrame currentFrame = (JInternalFramepopups.elementAt(i);
      if (currentFrame.isSelected()) {
        return currentFrame;
      }
    }
    return null;
  }
}

class PasteAction extends AbstractAction {
  SiteManager manager;

  public PasteAction(SiteManager sm) {
    super(""new ImageIcon("paste.gif"));
    manager = sm;
  }

  public void actionPerformed(ActionEvent ae) {
    JInternalFrame currentFrame = manager.getCurrentFrame();
    if (currentFrame == null) {
      return;
    }
    // cannot cut or paste sites
    if (currentFrame instanceof SiteFrame) {
      return;
    }
    ((PageFramecurrentFrame).pasteText();
  }
}

class PageFrame extends JInternalFrame implements ActionListener {

  SiteManager parent;

  String filename;

  JTextArea ta;

  public PageFrame(String name, SiteManager sm) {
    super("Page: " + name, true, true, true, true);
    parent = sm;
    setBounds(5050300150);

    Container contentPane = getContentPane();

    // Create a text area to display the contents of our file in
    // and stick it in a scrollable pane so we can see everything
    ta = new JTextArea();
    JScrollPane jsp = new JScrollPane(ta);
    contentPane.add(jsp, BorderLayout.CENTER);

    JMenuBar jmb = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem saveItem = new JMenuItem("Save");
    saveItem.addActionListener(this);
    fileMenu.add(saveItem);
    jmb.add(fileMenu);
    setJMenuBar(jmb);

    filename = name;
    loadContent();
  }

  public void actionPerformed(ActionEvent ae) {
    // Can only be the save menu
    saveContent();
  }

  public void loadContent() {
    try {
      FileReader fr = new FileReader(filename);
      ta.read(fr, null);
      fr.close();
    catch (Exception e) {
      System.err.println("Could not load page: " + filename);
    }
  }

  public void saveContent() {
    try {
      FileWriter fw = new FileWriter(filename);
      ta.write(fw);
      fw.close();
    catch (Exception e) {
      System.err.println("Could not save page: " + filename);
    }
  }

  public void cutText() {
    ta.cut();
  }

  public void copyText() {
    ta.copy();
  }

  public void pasteText() {
    ta.paste();
  }
}

class CutAction extends AbstractAction {
  SiteManager manager;

  public CutAction(SiteManager sm) {
    super(""new ImageIcon("cut.gif"));
    manager = sm;
  }

  public void actionPerformed(ActionEvent ae) {
    JInternalFrame currentFrame = manager.getCurrentFrame();
    if (currentFrame == null) {
      return;
    }
    // cannot cut or paste sites
    if (currentFrame instanceof SiteFrame) {
      return;
    }
    ((PageFramecurrentFrame).cutText();
  }
}

class CopyAction extends AbstractAction {
  SiteManager manager;

  public CopyAction(SiteManager sm) {
    super(""new ImageIcon("copy.gif"));
    manager = sm;
  }

  public void actionPerformed(ActionEvent ae) {
    JInternalFrame currentFrame = manager.getCurrentFrame();
    if (currentFrame == null) {
      return;
    }
    // can't cut or paste sites
    if (currentFrame instanceof SiteFrame) {
      return;
    }
    ((PageFramecurrentFrame).copyText();
  }
}


           
       
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. JDesktopPane demoJDesktopPane demo
3. Working with Internal Frames within a Desktop
4. Internal Frames DemoInternal Frames Demo
5. InternalFrame TestInternalFrame Test
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
w_w__w___._j__av_a___2s__.___c___o___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.