Change JPanel text of Parent JPanel from JDialog
I am working on an Java Desktop Application. I am displaying multiple JPanels in a JFrame. A Jpanel includes collection of components like buttons and other JPanels. When the button of that JPanel is clicked it opens a JDialog. Now my problem is when I perform button click on that JDialog how can I update the texts of that particular parent JPanel in JFrame. Need an urgent help plz.
Re: Change JPanel text of Parent JPanel from JDialog
If the JDialog is a modal dialog, then you know when the user is done using it and when the code flow returns to the parent window -- the code at the line immediately following myDialog.setVisible(true) will be called. It is here that you'll want to the main GUI to query the dialog to get its state.
If this doesn't make sense, please ask and I can try to clarify it further.
An unrelated request: Please out of respect to those of us who may help you here keep your urgency to yourself. We're volunteers and no one likes to feel pressured, least of all a volunteer. Thanks in advance for your understanding and cooperating with this.
Re: Change JPanel text of Parent JPanel from JDialog
Thank you so much for you quick and kind concern. I want it in more detail. So, please let me clarify my question first.
I have a frame where Jpanels with Jpanel and Jbutton inside are created dynamically.
And in that Jpanel when I click a button in any Jpanel it opens a Jdialog supplying an 'id' parameter.Now when i click a button in that JDialog, it should be closed and change the text of Jpanel to '0' which is inside a Jpanel. I think I made it clear here.
I would be so grateful to you, if you tell me the solution procedurewise. Thank you a lot again.
Re: Change JPanel text of Parent JPanel from JDialog
Question: do you know how to make a JDialog modal, so that it will always display in a modal fashion? One way to do this is to use one of the JDialog constructors that accepts a boolean parameter allowing you to make that JDialog modal.
In your JButton's ActionListener, you will eventually call setVisible(true) on your JDialog. The line immediately after that will be called when the user has finished dealing with the dialog and again it is here that you would query the dialog for its state. I usually give my dialog classes public getter methods to allow me to do this, for instance give it a public String getId() method that would return the String in the dialog's id JTextField. If the field is called idField, then the dialog's getId() method would return idField.getText();.
So the code in the JButton's ActionListener would call the dialog's getId() method and then place the data returned in whatever field you desire in the main JFrame.
hang on....
For example:
Code:
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GetInfoFromDialogTest {
private static void createAndShowGui() {
JFrame frame = new JFrame("Get Info From Dialog");
MainPanel mainPanel = new MainPanel(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
// main JPanel held by JFrame
class MainPanel extends JPanel {
private JTextField mainIdField = new JTextField(10);
private JButton showDialogBtn = new JButton("Show Dialog");
private JFrame frame;
private JDialog dialog;
private DialogPanel dialogPanel;
public MainPanel(final JFrame frame) {
setPreferredSize(new Dimension(500, 400));
mainIdField.setEditable(false);
mainIdField.setFocusable(false);
this.frame = frame;
showDialogBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showDialogActionPerformed(e);
}
});
add(mainIdField);
add(showDialogBtn);
}
private void showDialogActionPerformed(ActionEvent e) {
if (dialog == null || dialogPanel == null) {
dialogPanel = new DialogPanel();
dialog = new JDialog(frame, "Dialog", true); // make a modal dialog in
// a lazy fashion
dialog.add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
}
// display dialog
dialog.setVisible(true);
// this is called after the dialog has been hidden
String id = dialogPanel.getId();
// put id into field on main JPanel
mainIdField.setText(id);
}
}
class DialogPanel extends JPanel {
private JTextField idField = new JTextField(10);
private JButton hideDialogBtn = new JButton("Done");
public DialogPanel() {
hideDialogBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// hide dialog
Window win = SwingUtilities.getWindowAncestor(DialogPanel.this);
win.setVisible(false);
}
});
add(idField);
add(hideDialogBtn);
}
public String getId() {
return idField.getText();
}
}
Re: Change JPanel text of Parent JPanel from JDialog
Thank you so much for the solution. I implemented it on my project and works exactly as I have wanted. But while implementing this solution in my project it has a problem. Actually, the dialog I am going to display is a Jdialog not a DialogPanel. So it would be so great if you just simplify it for using in case of only JDialog. Because I am getting real big problem in JDialogPanel and JDialog.
I just need to open a Jdialog on Button click and set the value. Without using DialogPanel. Thank you.
Re: Change JPanel text of Parent JPanel from JDialog
Quote:
Originally Posted by
bikashlama
Thank you so much for the solution. I implemented it on my project and works exactly as I have wanted. But while implementing this solution in my project it has a problem. Actually, the dialog I am going to display is a Jdialog not a DialogPanel. So it would be so great if you just simplify it for using in case of only JDialog. Because I am getting real big problem in JDialogPanel and JDialog.
I just need to open a Jdialog on Button click and set the value. Without using DialogPanel. Thank you.
Please understand that this code was not posted with the goal of you using it in your final project but just as an example of how to get information from dialogs. You will need to try to understand the concepts within it, ask for clarification of any concepts that confuse you and then write your own code for your project. We are not here to do your work for you, but will be happy to try to help you to learn to do this yourself.
Re: Change JPanel text of Parent JPanel from JDialog
Hey! I solved it and it's working fine. Actually I was confused about Modal dialog and Modeless dialog. Thank you again!
Re: Change JPanel text of Parent JPanel from JDialog
You're welcome, and glad you've got it working -- and that you did most on your own.