Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6086435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:45:18+00:00 2026-05-23T11:45:18+00:00

Usually in a Swing application if I want to display JDialogs to the user,

  • 0

Usually in a Swing application if I want to display JDialogs to the user, I create them once and then reuse them.
Usually the main JFrame has references to these JDialogs.
Sometimes though I need to display a JDialog from parts of code outside the JFrame class.
Usually I make the JDialog member variables (of JFrame) public and either they are static or pass a reference of the JFrame arround to be able to access the JDialog.
It works but it seems messy to me.
I was wondering is there a standard design approach for something like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:45:19+00:00Added an answer on May 23, 2026 at 11:45 am

    My own philosophy is to make almost all fields private, including the dialog and only expose that which needs exposing, no more and no less. For instance if I have a JDialog called from another class and I get information from that dialog, I expose only the information via public getter methods, not the dialog itself.

    Edit 1
    Sorry for some gross over-kill and much longer than expected code, but this is kind of what I meant in an MVC sort of way and created in a great hurry:

    import java.awt.*;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class DialogEg {
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Dialog Eg");
    
          MainPanel mainPanel = new MainPanel();
          MainControl mainControl = new MainControl(frame);
          mainControl.setMainPanel(mainPanel);
          mainPanel.setControl(mainControl);
    
          frame.getContentPane().add(mainPanel.getMainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class MainPanel {
       private JPanel mainPanel = new JPanel();
       private JButton showDlgBtn = new JButton("Show Dialog");
       private JTextField field = new JTextField(10);
       private MainControl mainControl;
    
       public MainPanel() {
          mainPanel.add(showDlgBtn);
          mainPanel.add(field);
          field.setEditable(false);
    
          showDlgBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (mainControl != null) {
                   mainControl.showDialog();
                }
             }
          }); 
       }
    
       public void setControl(MainControl mainControl) {
          this.mainControl = mainControl;
       }
    
       public void setFieldText(String text) {
          field.setText(text);
       }
    
       public JPanel getMainPanel() {
          return mainPanel;
       }
    }
    
    class DialogPanel {
       private static final String[] COMBO_DATA = {"", "one", "two", "three", "four"};
       private JPanel dialogPanel = new JPanel();
       private JComboBox combo = new JComboBox(COMBO_DATA);
    
       public DialogPanel() {
          dialogPanel.add(combo);
          combo.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                Window window = SwingUtilities.getWindowAncestor(dialogPanel);
                window.dispose();
             }
          });
       }
    
       public JPanel getDialogPanel() {
          return dialogPanel;
       }
    
       public String getSelectedText() {
          return combo.getSelectedItem() == null ? "" : combo.getSelectedItem().toString();
       }
    }
    
    class MainControl {
       private DialogPanel dlgPanel = new DialogPanel();
       private MainPanel mainPanel;
       private JFrame frame;
    
       public MainControl(JFrame frame) {
          this.frame = frame;
       }
    
       public void setMainPanel(MainPanel mainPanel) {
          this.mainPanel = mainPanel;
       }
    
       public void showDialog() {
          if (mainPanel != null) {
             JDialog dialog = new JDialog(frame, "Dialog", ModalityType.APPLICATION_MODAL);
             dialog.getContentPane().add(dlgPanel.getDialogPanel());
             dialog.pack();
             dialog.setLocationRelativeTo(null);
             dialog.setVisible(true);
    
             String text = dlgPanel.getSelectedText();
             if (text != null) {
                mainPanel.setFieldText(text);
             }
          }
       }
    
    }
    

    The key here is that the MainPanel has no knowledge about the DialogPanel and visa-versa since it’s all connected by the MainControl through the marvels of MVC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing Swing application, and everything works fine usually. But I have an GUI
Usually, when I initialize the fonts I want to use in my SWING applications,
Usually i write my where statements as WHERE key=@0 then add a param. Now
Usually, when a trigger runs, we check what kind of a profile the user
Say I have a swing GUI, and I want to listen MouseEvents . Who
Usually, Django outputs a nice HTML traceback page once something goes wrong. This is
Usually wordpress structure for category is as below. http://ourdomain/category/categoryname/subcategoryname but I want my structure
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
Typically when I'm creating a Swing (or any UI) application, I have various Actions
We have a very large Java Swing desktop application comprising of a great deal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.