Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Change JPanel text of Parent JPanel from JDialog

  1. #1
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Question 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  5. #5
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Change JPanel text of Parent JPanel from JDialog

    Quote Originally Posted by bikashlama View Post
    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.
    Last edited by Fubarable; 12-08-2011 at 07:01 PM.
    DarrylBurke likes this.

  7. #7
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Post 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!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

Similar Threads

  1. Replies: 4
    Last Post: 04-19-2011, 05:09 AM
  2. Replies: 1
    Last Post: 11-10-2010, 10:14 PM
  3. Can I change the border of a JPanel cell?
    By ryuzog in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 06:05 AM
  4. Change JPanel background after its been set once
    By mevets in forum AWT / Swing
    Replies: 4
    Last Post: 04-14-2010, 01:07 AM
  5. How show a popWindow that have JPanel like parent
    By Wolverine in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2009, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •