Results 1 to 8 of 8
- 12-08-2011, 02:22 AM #1
Member
- Join Date
- Dec 2011
- Location
- Kathmandu, Nepal
- Posts
- 15
- Rep Power
- 0
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.
- 12-08-2011, 04:29 AM #3
Member
- Join Date
- Dec 2011
- Location
- Kathmandu, Nepal
- Posts
- 15
- Rep Power
- 0
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:
Java 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(); } }Last edited by Fubarable; 12-08-2011 at 04:55 AM.
- 12-08-2011, 06:22 AM #5
Member
- Join Date
- Dec 2011
- Location
- Kathmandu, Nepal
- Posts
- 15
- Rep Power
- 0
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
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.
Last edited by Fubarable; 12-08-2011 at 07:01 PM.
- 12-09-2011, 01:21 AM #7
Member
- Join Date
- Dec 2011
- Location
- Kathmandu, Nepal
- Posts
- 15
- Rep Power
- 0
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!
-
Similar Threads
-
jtextfield, actionlistener and changing background of parent jpanel
By kpwane in forum AWT / SwingReplies: 4Last Post: 04-19-2011, 05:09 AM -
Is there a better way to embed external (sub) JPanels in existing (parent) Jpanel?
By kdub in forum New To JavaReplies: 1Last Post: 11-10-2010, 10:14 PM -
Can I change the border of a JPanel cell?
By ryuzog in forum New To JavaReplies: 2Last Post: 10-10-2010, 06:05 AM -
Change JPanel background after its been set once
By mevets in forum AWT / SwingReplies: 4Last Post: 04-14-2010, 01:07 AM -
How show a popWindow that have JPanel like parent
By Wolverine in forum AWT / SwingReplies: 4Last Post: 05-23-2009, 08:59 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks