Link to home
Start Free TrialLog in
Avatar of no1nincompoop
no1nincompoop

asked on

Internal Frames in Java Swing

hi

I was learning Swing and I tried the following example from a book.

// File 1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test2 extends Frame implements ActionListener {
   JButton openButton;
   JLayeredPane desktop;
   JInternalFrame internalFrame;
   public Test2() {
      super("Internal Frame Test");
      setSize(500,400);
      openButton = new JButton("Open");
      Panel p = new Panel();
      p.add(openButton);
      add(p, BorderLayout.SOUTH);
      addWindowListener(new BasicWindowMonitor());
      openButton.addActionListener(this);
      desktop = new JDesktopPane();
      desktop.setOpaque(true);
      add(desktop, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
   System.out.println("TEST1");
   if ((internalFrame == null) || (internalFrame.isClosed())) {
      try {
         internalFrame = new JInternalFrame("Internal Frame",false,false,false, true);
         internalFrame.setBounds(50, 50, 200, 100);
         desktop.add(internalFrame, new Integer(1));
      }
   catch(Exception xxx)
   {
      System.out.println("Exception"+xxx);
   }
   System.out.println("TEST");
   }
}
public static void main(String args[]) {
   Test2 sif = new Test2();
   sif.setVisible(true);
}
}


// File 2
import java.awt.event.*;
import java.awt.Window;

public class BasicWindowMonitor extends WindowAdapter {
     public void windowClosing(WindowEvent e) {
          Window w = e.getWindow();
          w.setVisible(false);
          w.dispose();
          System.exit(0);
     }  
}

The files are compiled and executed successfully.
As written in the program when I clicke the "Open" button it should open an internal frame but it is not opening any internal frames.

I have j2sdk1.4.2 and JRE 1.4.2 installed
Processor AMD Athelon XP
OS Windows XP

Any idea about this??
Avatar of Mayank S
Mayank S
Flag of India image

>> desktop.add(internalFrame, new Integer(1));

Try calling desktop.revalidate () after that.
Avatar of no1nincompoop
no1nincompoop

ASKER

hi mayankeagle,

Thanks atleast I got a comment from you in this big group...
I tried what you said  ? it didn't work :-)

Regards
Muke
How about just a call to this.repaint () or this.revalidate () after that?
hi

got the solution.. Please close this question....
Answer is  to add  internalFrame.setVisible(true); in the actionPerformed event after setting the boundary for the internal frame..

This site was a bit useful...
http://java.sun.com/developer/JDCTechTips/2001/tt1220.html

Regards
Muke
Oh yes, didn't notice that you weren't calling setVisible () sorry :) it has to be called for every top-level container that you wanna display.
hi cud anyone pls close this question.
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial