Java jFrame toBack() and focusable - java

I got a Jframe which has a JTextField and a JButton which opens a popup. The popup has to be always in front of the JFrame.
Is it possible to write on the JTextField while the popup is still in front of the JFrame?

Yes you can still modify the contents inside an object.

Related

How to set JTextField as a selected component

I am developing an application which have a JTextField, JButton and JTextArea in same JFrame. How do I set JTextField selected when JFrame starts.
How do I set JTextField selected when JFrame starts.
I assume you mean you want the text field to have focus so you can start typing into it. ("Selected", in Swing terminology, means the text contained in the text field will be highlighted.)
The first component on the frame (top/left) will gain focus automatically.
If your text field isn't the first component then you can request focus with code like:
frame.setVisible(true);
textField.requestFocusInWindow();
The key is that you need to make the focus request AFTER the frame is visible.

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

Using a button to add a jPanel over a JFrame

I've been looking for a while and I can't find an helpful answer for my problem.
I'm using the Netbeans GUI Builder but right now I'm stuck: my intention is to use a button to make visible a jPanel object.
I've tried to directly insert this jPanel inside the JFrame (leaving it not visible until the button is pressed). The thing is, I could access the jPanel without the need of pressing the button: the panel wasn't visible but I could access the textfield if I tried to click over the blank space the panel is supposed to appear onto.
So now I have a separated class jPanel1 but I don't know how to add it to the JFrame.
This is the code I'm using for my button
private void modifyActionPerformed(java.awt.event.ActionEvent evt) {
JPanel newpanel=new modifyUserData(this.thisUser);
this.getContentPane().add(newpanel);
newpanel.setVisible(true);
}
but when I run it and press the button I get an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
Any ideas?

Display JFrame inside JPanel in java

Is this possible to add a JFrame into a JPanel in Java.
i m using a java program which is giving output as a frame which i wanted to display inside another program's JPanel on click of a button.
How can i do this?
Read about JInternalFrame. I think it's the way to go here.
http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
You could call getContentPane() on the JFrame to extract its main contents as a JPanel (usually) and without the menu bar and decorations, and display that as a JPanel though best would probably be to update the original program so that it produces a JPanel and not a JFrame.

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,

Categories

Resources