1

Can someone tell me how to set my X button in main application window visible to false and how to set Alt + F4 function not available or just disable it?


Update

I added it in:

public ZalumView(SingleFrameApplication app) { 
    super(app); 
    initComponents(); 
    mainFrame = this.getFrame(); 
    mainFrame.setTitle("Zalum - zarzadzanie zasobami ludzkimi"); 
    mainFrame.pack(); 
    mainFrame.setResizable(false); 
    mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Community
  • 1
  • 1
Bulit
  • 975
  • 6
  • 15
  • 26
  • 3
    please don't invent words, that looks simply silly in a technical forum ;-) – kleopatra Feb 29 '12 at 16:33
  • @kleopatra sorry for that this is just because English is not my capital language so this is a little hard to tell me here about my problems with app but this is too good forum to resign from it just because of that but I'm keep trying to make my question understandable for you – Bulit Feb 29 '12 at 16:46
  • So how exactly do you expect the user to know how to close your main application window? – adelphus Feb 29 '12 at 17:01
  • AFAIK isn't possible to prevent Alt + F4, came from Native OS – mKorbel Feb 29 '12 at 17:15
  • @adelphus I got tab called "Exit" where user got button Exit and then he's asked about save or not – Bulit Feb 29 '12 at 17:18
  • 3
    @Bulit Then you have a terrible design. The user should be free to close the application in a standard way - you should design your app to intercept the close request and prompt for saving. That's how every other standard application works and it's what the user will expect. – adelphus Feb 29 '12 at 17:21
  • 1
    @adelphus I have to do this becouse I didn't get any good aswer with my problem about getting to moment BEFORE event ClosingAplication with X I got way to this event exactly and I got code to there to prompt user for saving but what is user resign from exiting??(app is in Closing event u can't turn back from this call) This is my second problem from other topic here. – Bulit Feb 29 '12 at 17:26

2 Answers2

7

"A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated."—Frame

Addendum: You can send a WINDOW_CLOSING event and bind that Action to your desired Keystroke, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    If this isn't what you're looking for, please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Feb 29 '12 at 16:35
  • with this I turn off titlebar too and I just want to delete that X button // and still got that Alt + F4 event – Bulit Feb 29 '12 at 16:53
  • Took me a bit long, was lost in that Subway example link, was playing with that for the last half an hour, Wonderful Simulation Link that was :=) – nIcE cOw Feb 29 '12 at 18:28
7

For setting X button to invisible is very much described by @trashgod and For disabling your ALT + F4 thing, you can simply write frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

or you can addWindowListener(...) to your JFrame like this :

Code snippet to show what i am saying :

import java.awt.event.*;
import javax.swing.*;

public class FrameTest
{
    private WindowAdapter windowAction;
    private JFrame frame;

    public FrameTest()
    {
        frame = new JFrame("FRAME TEST");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        windowAction = new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            }
        };

        frame.addWindowListener(windowAction);

        frame.setSize(100, 100);
        frame.setVisible(true);     
    }
    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new FrameTest();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • This not work I add mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); and still can close my app with Alt + F4 – Bulit Feb 29 '12 at 16:57
  • @Bulit : How are you writing this, as told by me ? It cann't be, are you sure mainFrame is really the object of your JFrame. It works at my end, ALT + F4 gets disabled by this. – nIcE cOw Feb 29 '12 at 16:58
  • Yup and I add it in public ZalumView(SingleFrameApplication app) { super(app); initComponents(); mainFrame = this.getFrame(); mainFrame.setTitle("Zalum - zarządzanie zasobami ludzkimi"); mainFrame.pack(); mainFrame.setResizable(false); mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); (BTW why that four space don't work here for code(for me) – Bulit Feb 29 '12 at 17:05
  • @Bulit : While writing comments those 4 space things won't work, only small fragments of codes can be written enclosed between Tilde like `MY CODE IS IN GREY` – nIcE cOw Feb 29 '12 at 17:25
  • I wrote exacly that how u show this above but I look at this again maybe i'm doing something wrong but thx for this answer – Bulit Feb 29 '12 at 17:28
  • @Bulit : Your Welcome and Keep Smiling :-) – nIcE cOw Feb 29 '12 at 17:45
  • *"why that four space don't work here for code"* It only works in questions and answers, not comments. In Q&A, there needs to be a blank line before the start of the code block. Your question can be edited at any time. In future, put the updated code in the question, and inform people in a comment. I edited that code into the question, please look it over & tidy it up if need be. – Andrew Thompson Feb 29 '12 at 18:13