Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I stop closing an JInternalFrame?

Posted on 2001-08-31
7
Medium Priority
?
1,073 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

963 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