Open multiple windows from the same application without accumulating icons in the taskbar

Asked

Viewed 1,013 times

2

I wonder how I could create frames in java, without opening so many windows in the windows taskbar.

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

  • "Better way" makes the question opinionated.

  • Or what is a way to not open so many windows in the windows bar? Dialogs?

  • I edited, see if you didn’t change your line of reasoning.

  • I get it, thank you.

1 answer

1


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

1- only if the Modalitytype is not Modeless, which leaves the other windows free for change.

In the code below, taken from the other question, there is an executable example of how the Jdialogs, and 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);
        }
    }
}
  • Let me ask you another question, When I press Alt + tab, exit and return how can I know which dialog was the focus when they are not modal, this is the biggest problem I face, when I do not use frames and dialogs. what happens is that when I turn the focus goes to the frame and not to the dialog

  • @Ulissesgimenes well that is already a very different doubt of the question, but as said in the question, that can be controlled by the hierarchy, execute the code and you will see an example, the dialog01 belongs to the frame, the dialog02 belongs to dialog1, so you control the order.

  • The same problem happens in it, the only way and that if it is not modal, that is not true step, the focus goes back to the frame. You think I’d better ask another subject-related question?

  • @Ulissesgimenes if you want to open multiple windows, why do you want the focus to be on the first one? It doesn’t make much sense in that.

  • In fact when I go out and come back I would like the focus to go right back to the window that was the focus, for example: I was in Dialog02, I pressed alt + tab to do something in windows, when coming back again with alt + tab I would like the focus to go to the dialog02 and not to the frame, would you understand my question?

  • The way I got the system was already like this and does not use dialogs with true modal, for example I am in customer registration and would need information from another window if it is modal has no way to access another window to get some information.

  • @Ulissesgimenes if you turn off the modal, the chances of picking up outdated information between windows will be great. And modals always stay above the parent windows. Soon, the lower window will never be above. What you can do is open the dialogs next to the frame, so they are not overlapping.

  • What happens is that the fool of my boss not to say worse because it is a steep, thinks he knows something and keeps copying another system, and keeps filling me with patience. Because there’s this SIAGRI guy here who does it, only he’s done it in Delphi, and he does it that I mentioned. Only there’s no way of knowing how he does it because I don’t have access to the source code. but I’ll leave that question for now.. thanks for your patience..

  • @Ulissesgimenes try the way I told you before, putting the windows side by side, are windows too big? I am trying to change the example by putting the windows side by side here, when finished, update the answer.

  • @Ulissesgimenes unfortunately the link does not open here on the network where I am. But you have already managed to make the dialogs open side by side, without having to drag?

  • @Ulissesgimenes well, seems to be a more complex doubt than the question. It is good to ask another question by adding a [mcve] of your code so that it is possible to simulate the problem and mark this as solved, maybe in another question, you get more suggestions to help you in this other problem :)

  • Thank you for your answers

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.