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
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);
}
}