obtaining a frame with no close button and only a close button - java

How can i obtain a frame that doesn't have a close, minimize and restore button by default ?
And how can i obtain a frame that only has a close button as in :

myTopLevelContainer#setUndecorated(true);
but only JFrame have got three buttons --> close, minimize and restore buttons, JDialog has only one close button, and JWindow is un-decorated by default, maybe there is your way, notice JWindow required JFrame as parent, JFrame should't be dispayed,

You can't remove the button from a JFrame. You should have to use a JDialog instead.

Related

jtextfield retains value in jdialog after closing jdialog

I'm new to Java and Swing. I created a jframe and I added a menubar and MenuItem in it.
On clicking a menu item, a jdialog should open. Now the jdialog has a jtextfield in it and a jlabel. Now the problem for me is 'when dialog is opened for first time, the textfield is empty and thats correct. Now i close the jdialog and i open it again but now instead of getting an empty textfield in jdialog, i get the data entered previously' which is not what should happen as the jdialogs 'default close operation' property is set to 'dispose'. but that is not happening for me...
I dont know what i'm doing wrong. I have never tried applet/swing before in any other way (consider this as my first demo learning programme)
Second Image here
The JTextField is retaining it's value because it isn't being affected by the JDialog closing, instead it is being hidden as it's parent (the JDialog) is invisible
Setting the dialog to dispose isn't re-initialising the child components, so they keep their values. Some additional information on this behaviour is available here:
JDialog setVisible(false) vs dispose()
JDialog
One way you can prevent / control this is by "informing" the dialog to wipe the textfield as it is closing by adding a WindowEvent and providing the necessary functionality in the windowClosing() method
Netbeans gui-builder will generate this for you with the following:
Right click Dialog
Events
Window
WindowClosing
Providing:
private void jDialog1WindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
In which you can add: textfield.setText(""); to clear the textfield
Another approach is to create your own dialog and setting up the components in the constructor. As creating a new instance will contain the components with their default values, effectively resetting it

How to make a Frame-like component modal to JInternalFrame and block only that JInernalFrame

To make it short - my application's main JFrame has 2 JInternalFrames. In one of them, I want to have a JButton which will open a JDialog (/JOptionPane/JInternalFram). I want to set the JDialog modal to that JInternalFrame - I want to block that JInternalFrame and leave the other one accessible. I couldn't achieve this using JDialog as it only accepts Frame, Window and Dialog as owners (and it's not possible to cast from JInternalFrame to Frame). I tried using JOptionPane which accepts JComponent as a parent, but unfortunately it blocks the whole main JFrame. How can I achieve the desired behavior?

Change JFrame Window buttons

I've created a normal window with 3 buttons (minimize,maximize,close)
But i want to change the Window bar to containe the title and the close button only
I did it before with one line but i forget how to make it
This is my code :
frame.setSize(60, 500);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLocation(10,10);
frame.setResizable(false);
frame.setVisible(true);
I did it before with one line but i forget how to make it
I don't think you did it before because Swing doesn't support this on a JFrame.
Maybe you used a JDialog before? This is the behaviour of a dialog.
However a JDialog should generally only be used as a popup window of a main JFrame. A JDialog does not appear in the task bar and if the dialog loses focus it is not easy to get back unless you tab through all the open windows.

Restrict Access to other JFrame

How do i restrict access to other JFrame?
if I open my main frame and when click the button to display other jframe, the user should not be able to go back to the main frame.
how do I do that?
if i open my main frame and click the add button,
When you click the button you display a modal JDialog. Then until the user closes the dialog they won't be able to access the main frame.
try this method...
this.setEnabled(false);
Your question is not clear to me but as per my understanding I believe You want to open a dialog on button click but when you click the button a new JFrame is displayed and it becomes impossible for you to get back to the original frame.
Use a dialog /pop-up on your button click like JOptionPane.
If you want to open a JFrame on button click, making a HOME button on the newly created/opened JFrame and linking that button to main JFrame might be a good option.Closing the newly created JFrame will display the originally created JFrame anyway.
Use a dialog (JDialog class) instead and make it modal.
Here is some help on how to do this:
http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html
Regards,

Keep JFrame on top of others

I am in a situation where I create a frame FrameB from my main frame FrameA. When the user is working on FrameB I would like it to be on top of FrameA even when the user accidentally clicks on FrameA.
Do you have to use a JFrame?
If you use a JDialog instead of a JFrame and assign FrameA as the owner of the dialog through the constructor it will always remain on top of the frame. (Example: How to set the JFrame as a parent to the JDialog)
Otherwise you can use setAlwaysOnTop() from the window class, but this can be dependent on the operating system/window manager.
You could consider making FrameB a JDialog instead of JFrame, and set it modal.

Categories

Resources