Search code examples
javauser-interfaceawtpopupmenu

Java AWT - Add a popupMenu to a Panel / Frame without Layout


I need to create a GUI which has to provide a PopupMenu. I managed to do this but it only appears if I set the Layout null - In this case there is the problem, that none of my features appears only the popupmenu. I am not allowed to use Java.swing or something else.

How do I manage to let the popupMenu appear without setting my Layout null?

I hope you understand my problem, here you can check my code:

public class ServerStart {

    public static void main(String[] args) {
        ServerInterface serverInterface = new ServerInterface();
        serverInterface.setVisible(true);
    }
}

class ServerInterface extends Frame implements Runnable {

    private int portNr;
    private MenuBar menuBar;
    private String fileName;
    private TextField portText;
    private PopupMenu popupMenu;
    private Thread interfaceThread;
    private ServerThread serverThread;
    private Panel mainPanel, displayPanel;
    private Menu menuServer, menuFile, menuDatabase, menuProgram;
    private MenuItem start, stop, create, choose, connect, beenden, popupstart,
            popupstop, popupclose;
    private Label title, port, textClient, textStatus, clientNr, serverStatus;
    public static int client = 0;
    ServerSocket serverSocket;
    ServerInterface serverInterface;

    public ServerInterface() {
        super("ADRELI 4 GUI - SERVER");
        this.setBounds(325, 50, 475, 355);
       // this.setLayout(null);
        this.setLayout(new BorderLayout());
        this.setResizable(false);
        this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                DialogWindow dialogWindow = new DialogWindow(
                        new ServerInterface(), "Programm beenden",
                        "Wollen Sie das Programm wirklich beenden?",
                        "OK", "Abbrechen");
                if (dialogWindow.getAnswer()) {
                    System.exit(0);
                }
            }
        }
        );

        mainPanel = new Panel();
        mainPanel.setBackground(Color.darkGray);
        mainPanel.setLayout(null);
        this.add(mainPanel);

        title = new Label("SERVER");
        title.setBackground(Color.white);
        title.setBounds(20, 20, 425, 100);
        title.setAlignment(Label.CENTER);
        title.setFont(new Font("Arial", Font.BOLD, 25));
        mainPanel.add(title);

        menuBar = new MenuBar();
        menuServer = new Menu("Server...");
        start = new MenuItem("Start");
        stop = new MenuItem("Stop");
        stop.setEnabled(false);
        menuBar.add(menuServer);
        menuServer.add(start);
        menuServer.add(stop);
        this.setMenuBar(menuBar);

        menuFile = new Menu("Datei...");
        create = new MenuItem("Anlegen");
        choose = new MenuItem("Auswaehlen");
        menuFile.add(create);
        menuFile.add(choose);
        menuBar.add(menuFile);

        menuDatabase = new Menu("Datenbank...");
        connect = new MenuItem("Verbinden");
        menuDatabase.add(connect);
        menuBar.add(menuDatabase);

        menuProgram = new Menu("Programm...");
        beenden = new MenuItem("Beenden");
        menuProgram.add(beenden);
        menuBar.add(menuProgram);

        popupMenu = new PopupMenu();
        popupstart = new MenuItem("Start");
        popupstop = new MenuItem("Stop");
        popupstop.setEnabled(false);
        popupclose = new MenuItem("Beenden");
        popupMenu.add(popupstart);
        popupMenu.add(popupstop);
        popupMenu.addSeparator();
        popupMenu.add(popupclose);
        this.add(popupMenu);

        displayPanel = new Panel();
        displayPanel.setBounds(20, 140, 425, 150);
        displayPanel.setLayout(null);
        displayPanel.setBackground(Color.lightGray);

        textStatus = new Label("Serverstatus:");
        textStatus.setBounds(20, 20, 300, 30);
        textStatus.setBackground(Color.white);
        textStatus.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(textStatus);

        textClient = new Label("Aktive Clients:");
        textClient.setBounds(20, 60, 300, 30);
        textClient.setBackground(Color.white);
        textClient.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(textClient);

        port = new Label("Portnummer:");
        port.setBounds(20, 100, 300, 30);
        port.setBackground(Color.white);
        port.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(port);

        serverStatus = new Label();
        serverStatus.setBackground(Color.red);
        serverStatus.setBounds(355, 20, 50, 30);
        displayPanel.add(serverStatus);

        clientNr = new Label(" 0 ");
        clientNr.setBackground(Color.white);
        clientNr.setBounds(355, 60, 50, 30);
        clientNr.setAlignment(Label.CENTER);
        clientNr.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(clientNr);

        portText = new TextField("56789");
        portText.setBackground(Color.white);
        portText.setBounds(355, 100, 50, 30);
        portText.setFont(new Font("Arial", Font.BOLD, 15));
        displayPanel.add(portText);

        start.addActionListener(new ItemClicked());
        stop.addActionListener(new ItemClicked());
        choose.addActionListener(new ItemClicked());
        create.addActionListener(new ItemClicked());
        connect.addActionListener(new ItemClicked());
        beenden.addActionListener(new ItemClicked());
        popupstart.addActionListener(new ItemClicked());
        popupstop.addActionListener(new ItemClicked());
        popupclose.addActionListener(new ItemClicked());
        mainPanel.add(displayPanel);
    }

    @Override
    public void processMouseEvent(MouseEvent me) {
        if (me.isPopupTrigger()) {
            popupMenu.show(me.getComponent(), me.getX(), me.getY());
        }
        super.processMouseEvent(me);
    }

Solution

  • Remove this.add(popupMenu); and it should work fine with a non-null layout. A popup menu is designed to display over other components, so you don't add it to another component in this case.