troubleshooting Question

JInternalFrame(s) won't display in Java app

Avatar of christophm
christophm asked on
Java
2 Comments1 Solution313 ViewsLast Modified:
I cannot get the following program to execute correctly.

This program is essentially Listing 4.2, from "Pure JFC Swing", SAMS,
1999, author - Dr. Satyaraj Pantham.  I have made some very minor
changes (mostly just variable names).

So...., this program is supposed to:
1. be a Java application
2. present a frame (Pg043LayerDemo)
3. create a 3x3 grid of buttons in a panel
4. display the panel at the content pane Border.South
5. clicking the various buttons creates JInternalFrame's
and adds them to the various layers (DEFAULT, PALETTE, etc.)

Everything works except #5.

I am using the plain/old/DOS JAVA compiler from SUN
(V1.3.x) so I have little tracing/debugging/IDE help.  

I have established that the method that creates the JInternalFrame's,
'addInternalFrame(int iIndex)' is entered and exited normally
('normally' = no crashes).

I have further demonstrated that I can add a 'Label' control to the
JFrame (LayeredPane's Content Pane) in 'addInternalFrame(int iIndex)'.

THE QUESTION
Sooo...., "Whycome my JInternalFrames are not showing????"

thanks all -   chris-m
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

//---------------------------------------------------------------------------
class Pg043LayerDemo extends JFrame implements ActionListener {
    JButton             button;
    JLayeredPane        layeredPane;
   
    static int          iFrameCount = 0;
   
    static final int    xOffSet = 25;  // amount to X-offset panes
    static final int    yOffSet = 25;  // amount to Y-offset panes
   
    int                 numFrames = 1;
   
    // define arrays to hold layered frame constants and their names
        Integer[]           iLayerConstants =
                                {JLayeredPane.DEFAULT_LAYER,
                                 JLayeredPane.PALETTE_LAYER,
                                 JLayeredPane.MODAL_LAYER,
                                 JLayeredPane.POPUP_LAYER,
                                 JLayeredPane.DRAG_LAYER,};

        String[]            stLayerNames =
                                {"Default Layer",
                                 "Palette Layer",
                                 "Modal Layer",
                                 "Popup Layer",
                                 "Drag Layer"};
                                 
        Vector              vectorFrames = new Vector();
   
   
    //-----------------------------------------------------------------------
    public Pg043LayerDemo (String title) {
        super(title);

        JPanel panel = new JPanel(new GridLayout(3,3));
       
        panel.setBorder(BorderFactory.createTitledBorder
                           ("Click the Specific button to Add " +
                            "Frames or Clear Frames"));

        for (int iIndex=0;  iIndex<=stLayerNames.length;  iIndex++) {
           
            if (iIndex < stLayerNames.length) {
                button = new JButton("Add to " + stLayerNames[iIndex]);
                button.setActionCommand(stLayerNames[iIndex]);
            }
            else {
                button = new JButton("Clear the Frames");
                button.setActionCommand("Clear");
                button.setForeground(Color.red);
            }
               
            button.addActionListener(this);
            panel.add(button);
        }

        getContentPane().add(panel, BorderLayout.SOUTH);

        layeredPane = getLayeredPane();
       
        addWindowListener(new WindowEventHandler());
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setSize(400, 400);
        show();
    }

    //-----------------------------------------------------------------------
    public void actionPerformed(ActionEvent evt) {
       
        JButton tempButton = (JButton)evt.getSource();
       
        // default layer
        if (tempButton.getActionCommand() == stLayerNames[0])
            addInternalFrame(0);
        else  
        // palette layer
        if (tempButton.getActionCommand() == stLayerNames[1])
            addInternalFrame(1);
        else  
        // modal layer
        if (tempButton.getActionCommand() == stLayerNames[2])
            addInternalFrame(2);
        else  
        // popup layer
        if (tempButton.getActionCommand() == stLayerNames[3])
            addInternalFrame(3);
        else  
        // drag layer
        if (tempButton.getActionCommand() == stLayerNames[4])
            addInternalFrame(4);
        else
        // clear frames
        if (tempButton.getActionCommand() == "Clear") {
           
            for (Enumeration enum = vectorFrames.elements(); enum.hasMoreElements();  ) {
                ((JInternalFrame)enum.nextElement()).dispose();
            }
               
            iFrameCount = 0;
        }

    }    


    //-----------------------------------------------------------------------
    public void addInternalFrame(int iIndex) {

        JInternalFrame jifFrame = new JInternalFrame(
                                "In " + stLayerNames[iIndex],
                                true,   // can be resized
                                true,   // can be closed
                                true,   // can be maximized
                                true);  // can be iconified (Is that a word!?!);
                               
       // ????. register the new frame in a vector
       vectorFrames.addElement(jifFrame);
       
       // ????. setup the new (internal) frame stuff
       jifFrame.setLocation(xOffSet*(iFrameCount), yOffSet*(iFrameCount++));
       jifFrame.setSize(200, 150);
       jifFrame.setDefaultCloseOperation(jifFrame.DO_NOTHING_ON_CLOSE);
       
//jifFrame.setFrameIcon(new ImageIcon("D:\\Javasource\\Flags\\BigGifs\\bFlag.Gif"));
       
        layeredPane.add(jifFrame, iLayerConstants[iIndex]);
    }


    //-----------------------------------------------------------------------
    class WindowEventHandler extends WindowAdapter {
       
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    }


    //-----------------------------------------------------------------------
    public static void main(String[] args) {
        Pg043LayerDemo frame = new Pg043LayerDemo("Pg043-Layered Panes 01");
    }

}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros