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
Solved

How can I stop closing an JInternalFrame?

Posted on 2001-08-31
7
1,046 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 168
null output 3 35
What's wrong with this code? 4 26
Html Table Looping (part 2) 5 27
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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

809 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