Open several windows of the same application without accumulating icons in the taskbar

2

I would like to know how I could create java frames, without opening so many windows in the windows taskbar.

For example when I go to the menu and click on customer registration, address book, address type register, 3 windows are opened, it is the same system. How to make only one application icon appear on the taskbar, even with multiple windows open?

    
asked by anonymous 26.07.2016 / 12:57

1 answer

1

As I answered something similar in this question , when you have multiple windows in the same application, the most recommended is to use JDialog for windows Secondary, and JFrame only for the main window, because through JDialog you can have more control of which window will be in focus, the exchange of data between windows is facilitated because, while JDialog is open, the other windows are locked 1 for selection, awaiting the end of the operation on it. Of course this requires some hierarchy control between the windows.

1- Only if the ModalityType is not Modeless, which leaves the other windows free to change.

In the code below, taken from the other question, you have an executable example of how JDialogs works, and the control between them.

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.event.*;
import javax.swing.*;

public class ModalTeste extends JFrame {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new ModalTeste().start());
    }

    private JFrame getInstance() {
        return this;
    }

    //start frames
    private void start() {
        setTitle("Frame principal");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnFrame = new JButton("Abrir Dialog");
        btnFrame.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //passando a instancia do Frame para referencia do modal
                Dialog01 d1 = new Dialog01(getInstance());
                d1.start();
            }
        });
        setLayout(new BorderLayout());
        add(new JLabel("Este é o frame principal"), BorderLayout.CENTER);
        add(btnFrame, BorderLayout.PAGE_END);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    class Dialog01 extends JDialog {

        //precisa passar a janela mae como parametro para servir
        //de referencia ao modal
        public Dialog01(JFrame owner) {
            //recebe a janela mae, o titulo(opcional) e se é modal
            super(owner, "Dialog 01", true);
        }

        private JDialog getInstance() {
            return this;
        }

        private void start() {
            JButton btn2 = new JButton("Abrir outro dialog");
            btn2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //aqui está sendo passada a instancia do Dialog01
                    //como referencia do modal da dialog02
                    Dialog02 d2 = new Dialog02(getInstance());
                    d2.start();
                }
            });
            setLayout(new BorderLayout());
            add(new JLabel("Esta é a primeira janela modal"), BorderLayout.CENTER);
            add(btn2,BorderLayout.PAGE_END);
            setSize(200, 200);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setLocationRelativeTo(getParent());
            setVisible(true);
        }

    }

    class Dialog02 extends JDialog {

        //repare que o Jdialog pode receber um JFrame ou
        //outro JDialog como argumento
        public Dialog02(Dialog owner) {
            //recebe a janela mae, o titulo e se é modal
            super(owner, "Dialog 02", true);
        }

        private void start() {

            add(new JLabel("Esta é a segunda janela modal"));
            setSize(200, 200);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setLocationRelativeTo(getParent());
            setVisible(true);
        }
    }
}
    
26.07.2016 / 13:06