Search code examples
swingjava-8jdialog

Minimize a resizable JDialog


I'm working on an swing application with a main window (which extends JFrame) from which several child windows can be opened (more than 1 contemporarily).
These windows are all non-modal and resizable.
So far, I implemented these 'child' windows as a JFrame. However, I get a new icon on my Windows taskbar for each opened Window.
I therefore tried to implement these windows as a JDialog with type ModalityType.MODELESS.
Looks OK except that a JDialog has no minimize button.

Is there a way to resolve this?
I.e., I need to create non-modal and resizable child windows that can be minimized.
JInternalFrame is not an option since the main frame is not just a container with a JDesktopPane and child windows should be able to cross the borders of the main window.

For those interested:
Child windows register and unregister themselves on the main window when being opened/closed.
The main window has a menu with a 'Windows' item and child windows are added/removed from that menu upon registration/unregistration.
The user can switch between the various windows by selecting an item within this menu.


Solution

  • I am offering two suggestions.

    • A. Don't use the close button to get rid of the contents.
    • B. Set the type of child jframes to be utility.

    I think having the JDialog close button destroy data is setting your users up for data loss. I would instead use the close to just hide the window, and then have controls inside of the dialog to cancel/finish/restart.

    import java.awt.*;
    
    public class DiFrame{
        static JDialog log;
        static JFrame ame;
        public static void main(String[] args){
            JFrame frame = new JFrame("Father of two");
            
            JButton one  = new JButton("dialog");
            
            one.addActionListener( evt->{
                if(log==null){
                    log = new JDialog(frame, "dialog child", false);
                    log.add(new JTextArea("fresh start"));
                    log.pack();
                    log.setVisible(true);
                } else{
                    log.setVisible(true);
                }
            });
            
            JButton two = new JButton("frame");
            
            two.addActionListener( evt->{
                if(ame==null){
                    ame = new JFrame("frame child");
                    ame.add( new JTextArea("fresh start") );
                    ame.setType(Window.Type.UTILITY);
                    ame.pack();
                    ame.setVisible(true);
                } else{
                    ame.setVisible(true);
                }
            });
            frame.add(one, BorderLayout.NORTH);
            frame.add(two, BorderLayout.SOUTH);
    
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
      
    }
    

    Click the dialog button and it shows the dialog. Then the text area can be modified. When the dialog is closed it can be re-opened.

    Click the frame button and a jframe is shown. ( I actually cannot check if this shows up as a new application because it doesn't on my computer anyways. )