Solved

How can I stop closing an JInternalFrame?

Posted on 2001-08-31
7
1,052 Views
Last Modified: 2011-04-14
A have an JInternalFrame in my application (under Windows L&F). Whwn I press Ctrl+F4 the internal frame is closing. How can I stop this?
 
 
Thanks
0
Comment
Question by:cosmin_neag070600
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:daitt
ID: 6444578
You can setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE ) to stop users from closing JInternalFrame. Your program need to handle that.
0
 

Author Comment

by:cosmin_neag070600
ID: 6444610
I'm not so stupid. I've alredy tried this but didn't work. I have also tried to override the following methods: processKeyEvent, processComponentKeyEvent.

0
 
LVL 3

Expert Comment

by:daitt
ID: 6444792
I'm sorry I didn't check that. I tried to add KeyListener to the JInternalFrame.getContentPane() and consume the event when the user press Ctrl+F4 and it works.
Sorry for the last comment.
daitt
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:cosmin_neag070600
ID: 6445160
Sorry, but doesn't work. If you have a short example that works please send me (cneag@yahoo.com). Thanks.
0
 
LVL 3

Expert Comment

by:daitt
ID: 6446909
I can post it here. I compiled the files with JDK1.4 (beta).

import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;

import java.awt.event.*;
import java.awt.*;

public class InternalFrameDemo extends JFrame {
    JDesktopPane desktop;

    public InternalFrameDemo() {
        super("InternalFrameDemo");
        //Set up the GUI.
        desktop = new JDesktopPane(); //a specialized layered pane
        createFrame(); //Create first window
        setContentPane(desktop);          
    }

    protected void createFrame() {
        MyInternalFrame frame = new MyInternalFrame();
          frame.setVisible(true); //necessary as of kestrel
        desktop.add(frame);
         
          frame.getContentPane().addKeyListener(new KeyAdapter()
          {
               public void keyPressed(KeyEvent e)
               {
                    if (e.getKeyCode() == e.VK_F4 && (e.getModifiers() & e.CTRL_DOWN_MASK) != 0)
                    {
                         System.out.println("Consumed a keyPressed");
                         e.consume();
                    }
               }
               public void keyReleased(KeyEvent e)
               {
                    if (e.getKeyCode() == e.VK_F4 && (e.getModifiers() & e.CTRL_DOWN_MASK) != 0)
                    {
                         System.out.println("Consumed a keyReleased");
                         e.consume();
                    }
               }
               public void keyTyped(KeyEvent e)
               {
                    if (e.getKeyCode() == e.VK_F4 && (e.getModifiers() & e.CTRL_DOWN_MASK) != 0)
                    {
                         System.out.println("Consumed a keyTyped");
                         e.consume();
                    }
               }    
          });
         
    }

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


class MyInternalFrame extends JInternalFrame{
    public MyInternalFrame() {
        super("Internal Frame", true,true,true,true);
        setSize(300,300);
          setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
    }
}
0
 

Author Comment

by:cosmin_neag070600
ID: 6450043
Your code is OK but your JInternalFrame is empty.Please add a common JButton on contentpane and you'll see that the listeners will not responding. Replace your MyInternalFrame class with the following class:

class MyInternalFrame extends JInternalFrame{
   public MyInternalFrame() {
       super("Internal Frame", true,true,true,true);
       getContentPane().add(new JButton("Wrong"));
       setSize(300,300);
         setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
   }
}

Note: My internal frame window containes more containers and I don't wish to add a key listener to each of them.  
0
 
LVL 3

Accepted Solution

by:
daitt earned 200 total points
ID: 6479294
It seems to be a bug and it is fixed in JDK1.4.
http://forums.java.sun.com/thread.jsp?forum=31&thread=150063
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 53
spring jars download 1 53
Convert from a json string array to a Java object 3 65
Java class and jar 3 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question