[Swing] Custom popup menu (Edit to your likings)

Hey JGO, after making a thread in the newbie board on how to do this I ended up making it myself and I’d like to share it, hopefully somebody finds this useful :slight_smile:

Here’s a picture of what the code will produce: (Mind you I added a custom LookAndFeel)

And here is the code for it: (LookAndFeel code removed)


import java.awt.event.*;

import javax.swing.*;

public class CommandsPopupMenu {

   // main method for testing....
   public static void main(String[] args) {
      // Make our popup show!
      new CommandsPopupMenu("Spaceoids Command Executor");
   }

   // Instance of our custom JDialog popup menu
   private JDialog dialog;

   /**
    * Constructor
    * 
    * @param title
    *            The title of the popup
    */
   public CommandsPopupMenu(String title) {

      // Create the JDialog popup
      dialog = new JDialog(new JFrame(), title);

      // Make sure the pop-up isn't resizable
      dialog.setResizable(false);

      // Cannot call EXIT_ON_CLOSE so we dispose() ourselves:
      dialog.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent evt) {
            dialog.dispose();
         }
      });

      // Create the JPanel which will hold the components:
      int panelWidth = 310;
      int panelHeight = 200;
      JPanel panel = new JPanel(null);
      panel.setPreferredSize(new java.awt.Dimension(panelWidth, panelHeight));

      // Create the components:
      JButton sendButton = new JButton("Send");
      JButton cancelButton = new JButton("Cancel");
      JTextField cmdField = new JTextField(20);
      JLabel cmdFieldHeader = new JLabel("<html><b>Enter a command to execute:</b></html>");
      JLabel cmdListHeader = new JLabel("<html><b><span style='font-size:12'>COMMAND LIST:</span></b></html>");

      // Add all of our components to the panel:
      panel.add(cmdFieldHeader);
      panel.add(cmdField);
      panel.add(sendButton);
      panel.add(cancelButton);
      panel.add(cmdListHeader);

      // Position all the components on the panel:
      int startingX = (panelWidth / 2) - 200 / 2;
      int startingY = 0;
      cmdFieldHeader.setBounds(startingX, startingY, 200, 45);
      cmdField.setBounds(startingX, startingY + 35, 200, 25);
      sendButton.setBounds(startingX - 2, 70, 100, 30);
      cancelButton.setBounds(startingX + 102, 70, 100, 30);
      cmdListHeader.setBounds(startingX + 50, 70, 200, 80);

      // Finally add our panel that contains all our components:
      dialog.add(panel);

      // Prep the popup to be shown
      dialog.pack();
      dialog.setAlwaysOnTop(true);
      dialog.setLocationRelativeTo(null);
      dialog.setVisible(true);
   }

}

Is there a reason you used a null layout and absolute positioning? What will happen if the user resizes the dialog? You might consider using an actual layout instead.

Null layout so I can hard-code the positions in, as this was a small 1-2 hour project for a mate of mine :stuck_out_tongue:

User shouldn’t be able to resize it at the moment lol, does a JOptionPane have the ability to resize?
I don’t think so but I could be wrong :stuck_out_tongue:

When I run your code, the dialog is resizable. I would personally recommend using an actual layout to make it easier both on the programmer and the user, but it is of course up to you!

What OS are you on btw?

This GUI isn’t relative to my networking project GNetLib (If that’s what you were insinuating) by the way, so I’ll let the user who wanted me to code this little project finish it for himself or edit to his likings :stuck_out_tongue:
I made this for a JGO user since he didn’t understand how the server/client packet stuff works etc with my library.

Question: Is it frowned upon to set a containers layout to null than add/position the components via setBounds()?

Weird, just booted up my IDE and I don’t see anyway of resizing it lol.

Wow my bad, you’re right.

I forgot to call setResizable in the shared code/this thread but I didn’t in the source lol.

My bad, thanks for the help kev ^___^
I didn’t include disabling in the shared code/this thread.

I’m on Windows 7.

Hey that’s cool. I didn’t know anything about your other projects. Just figured I’d offer some constructive criticism.

[quote=“TehJavaDev,post:5,topic:51824”]
Yes, absolutely. That’s a pretty bad habit to get into, and any novices checking out this post should be aware that using layouts is the way to go. Unless you have a very good reason to use a null layout (and “I don’t feel like learning how to use layouts” is not a good reason), using a real layout will save you tons of headaches down the road, so it’s a good habit to get into.

Will do :slight_smile:

Thanks again ;D