Reputation: 686
I am trying to create a gui using swing. I have a main frame and I want it to only show the x button at the top right corner. I would also like to make this button impossible to press if certain conditions are met. I did some search on google and on java's website but I couldn't find any functions related to these two tasks. Any small pieces of code or links to tutorials or apis are welcome.
I want the button to be there. I don't want the window to close (which I can do with the setDefaultCloseOperation) and also I'm trying to find a way to be able to make the button look greyed out and not allow it to execute the animation of getting pressed in when I click on it. I want it to be there but grey and totally non responsive.
Upvotes: 0
Views: 2274
Reputation: 109815
maybe myFrame.setResizable(false);
and all of Events for this Button you can hande this way
EDIT:
window's decorations coming from Native OS, and then you have this four options as follows
full decorated (JFrame) minimize/maximize/close Button
only close Button JDialog and JFrame#setResiziable(false)
undecorated, then there missed minimize/maximize/close Button(a) and toolBar (that came from Native OS) too
undecorated Top-level Container with JPanel (with GradientPaint simulated real ToolBar) contains JButton with Char X (could be enabled once time or another time disabled as you want)
Upvotes: 3
Reputation: 2595
You can use a JDialog it just have got the X button
And with dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
you can disable the close button.
With dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
you can enable the close button
Upvotes: 2
Reputation: 691635
You didn't search very far. See http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29. Passing DO_NOTHING_ON_CLOSE
to this method will forbid the frame to be closed via the default close button.
Upvotes: 0
Reputation: 3534
You can specify what you want to happen when close button is pressed on your Frame. If you want it to do nothing, then:
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Upvotes: 0