Solved

How can I stop closing an JInternalFrame?

Posted on 2001-08-31
7
1,035 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now