Passing objects from JDialog back to root frame - java

Let's say we have a JFrame frame that contains two JPanels, buttonPanel and dataPanel, and in this panel a single JButton button. When clicked, button creates and shows a JDialog dialog in its own window (as usual). Using several JTextFields and a submit button, the JDialog dialog creates a new Object dataObject encapsulating these input data. If we wish our dataPanel JPanel in the main application frame to display this dataObject, how should dataObject be appropriately passed to the JPanel residing in a foreign JFrame?
That was a mouthful even to me while writing it, so I'll attempt to clarify:
JFrame frame
JPanel dataPanel - meant to display data from Objects created in the JDialog
JPanel buttonPanel - contains a button to open the JDialog, into which some information will be entered and with said information our Object dataObject is constructed.
The goal here is to pass this dataObject (and it's constituent fields) to the dataPanel to be displayed. What is the most appropriate way to handle this? I considered keeping the Objects in dataPanel static and then calling a static method from the JDialog to add the new object, but it doesn't seem the proper thing to do.
Some guidance?

Much will depend on the structure of your program, including how the dialog is supposed to behave:
If the JDialog is modal and disappears when the submit button has been pushed, then the solution is easy -- extract the data from the dialog-related class after it returns which will be the line of code right after where you display the dialog. The dialog's submit JButton's listener could simply make the dialog no longer visible.
Otherwise if the JDialog is non-modal and disappears when the submit button has been pushed, then you may wish to attach a Listener to its Window, I believe a WindowListener, and then have your calling code extract the information when the listener indicates that the dialog has been closed or is closing.
Otherwise, if the JDialog is non-modal and is not supposed to become invisible when the submit button has been pressed but you need to update the calling program with new data, then I would have the calling class add a PropertyChangeListener onto the dialog-related class so that the dialog-related class can notify any listeners that submit has been pressed. This code would be in the dialog's submit JButton's listener.
I would give a dialog-related class a public DataObject getDataObject() method that the calling code can call once the dialog returns, allowing the class that displays the dialog to extract the pertinent information when needed.
Whatever you do, there is no reason to use static fields and many reasons not to. I strongly urge you to not even consider this.
For example of a modal dialog that returns:
// caveat: code has not been tested by compilation or running.
JButton myButton = new JButton(new AbstractAction("Show Dialog Button") {
public void actionPerformed(ActionEvent evt) {
MyDialogPanel myDialogPanel = new MyDialogPanel();
JDialog myDialog = new JDialog(myJFrame, "My Dialog",
ModalityType.APPLICATION_MODAL);
myDialog.add(myDialogPanel);
myDialog.pack();
myDialog.setLocationRelativeTo(myJFrame);
myDialog.setVisible(true);
// dialog now returns and we can get the data
// assuming that the wrapper object for your data
// is called "DataObject"
DataObject dataObject = myDialogPanel.getDataObject();
// and perhaps use it. Assume setDataObject is a method
// of the main GUI that displays the data object
setDataObject(dataObject);
}
});

Related

keep Jframe always on focus

My java program has multiple classes
One Class is responsible for creating a Jframe
Another class is responsible for writing text , which will be displayed on the jframe
Then another class responsible for other functionalities and so on.
eg
new Classname.CreatedJFrame();// generate jframe
new Classname.DisplayText(); //generate text which will be displayed on jframe
new Classname.DoSomething(); this will do something else;
These above functions are being looped up.
I am taking input via jframe which will detect which key was pressed via keyboard(0-9) However the cursor does not always stays over Jframe , I have to quickly click jframe and type the input (if delayed again i have to click the jframe area to bring pointer on focus)
Is there any solution by which the focus can always be on the jframe and input can be provided any time.
Thanks in advance

How to access JFrame's method from JDialog?

I have my main JFrame and one more JDialog.
If user click on the button, i want JDialog to call method from this JFrame(which contains some operations on ComboBox in this JFrame).
How can I do that?
I don't want to use MyJFrame form = new MyJFrame(); because it will make a new JFrame which i don't want to do, i want to call method from JFrame which is running currently on my computer.
Thanks.
Assuming the JButton is on the JDialog.
If both are in the same class, why not just do this?
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//I just got clicked
form.doSomething();
}
});
which can be shortened to
button.addActionListener(e -> form.doSomething());
If they aren't, and you're extending JDialog (which I wouldn't recommend) just pass the JFrame in its constructor, then your dialog will have access to it.
It's not possible to add much more without seeing more of your code.
Maybe JOptionPane.showInputDialog() , show a JDialog to take input from User.

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

Automatically close non-modal dialogs after specified time in swing 1.5

In Swing for Java 1.5 I want to create a dialog, which allows the user to do other things in background. I want this dialog to have one button, with which you can close the dialog. If the user doesn't close the dialog within X seconds, it should close itself. In both cases a routine has to be done after the dialog has been closed.
I tried to use Swing Timer with a modal dialog and it works. But, as I noticed above, I need a non-modal dialog. When I set the modal-Property to false, the dialog disappears immediately.
Does someone know, why this happens?
JOptionPane pane = new JOptionPane (text, JOptionPane.WARNING_MESSAGE);
pane.setOptions(new String[]{"Close"});
final JDialog dialog = pane.createDialog(frame, title);
//dialog.setModal(false);
Timer timer = new Timer(time, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
//routine to do after the dialog disappears
for better help sooner post an SSCCE, there no code or descriptions about MultiThreading
don't create final JDialog dialog = pane.createDialog(frame, title); on runtime, create this JDialog one time and re_use that for another action, and / or by removing all childs from ContentPane
override proper event from WindowListener, there you can invoke your custom code before dipose(), setVisible() e.i.
(to point 2nd.) simply to override JDialog#setDefaultCloseOperation to HIDE_ON_CLOSE
all code that invoking a new Top-Level Container on runtime must be wrapped into invokeLater(), especially setVisible(true)
use Application Inactivity by #camickr
In this example, a modeless JDialog containing a direct JOptionPane counts down to zero before closing. A nearby JFrame containing a label remains responsive.
Addendum: As #mKorbel helpfully comments, your class can use a PropertyChangeListener to learn when the dialog's option pane closes. This is a convenient feature of JOptionPane, but you can add your own support, as shown here.

How does composite object communicate with parent object?

I am from system programming background in C and Java programming is Greek and Latin for me.
So my problem is:- I have 2 JFrame Objects
I have a parent Object A
which has child Objects B.
Object B has buttons on it.
If a button is clicked in B I want to update the UI of parent Object.
How can I can communicate to the parent - to update itself?
Do the updates need to be made 'live' while the values are adjusting and the 2nd GUI is open, or can they be delayed until it is closed?
They can be delayed until it is closed.
Use a modal dialog or JOptionPane instead.
How to Make Dialogs
How to Use Modality in Dialogs
Using a modal dialog, whatever code line comes immediately after setting it visible, is blocked from being processed until it is closed. That is where you examine (the return value of the option pane &) the value of the controls you put in the dialog. Note that the idea here is do not extend dialog. Instead just create an instance of one in the main code, create the controls, add them to it, and show it using the main frame as the parent.
Putting 'everything in one class' is not a good design, nor what I am suggesting in general. It is just that there is little cause here for the main GUI not to have references to controls that affect/update it.
Well normally you have a parent JFrame object into which you put your widgets like buttons, panels etc.
You can simply add an anonymous action listener with the button and then call any function of the parent class from inside it.
public class MyFrame extends JFrame{
private Button button 2 = new Button();
public void init(){
b = new Button("Click me");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//CALL ANY FUNCTION OF PARENT CLASS FROM HERE.
updateText();
}
});
}
updateText(){
this.button2.setText("new text");
}
}
You can find more info about it on this page http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
I have found one more way to solve this problem.
I guess it can solve a general delegation problem.
Parent Object composes of Child Object.
Parent Object passes this value to Child Object in constructor.
This way the Child can call a method on the Parent Object and notify any change.
In this case Child Frame calls Parent frame and asks the parent to refresh itself.

Categories

Resources