Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I stop closing an JInternalFrame?

Posted on 2001-08-31
7
Medium Priority
?
1,069 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

661 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