Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Swing] Custom popup menu (Edit to your likings)  (Read 42518 times)
0 Members and 1 Guest are viewing this topic.
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Posted 2014-11-03 22:11:36 »

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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
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);
   }

}

Checkout my new easy to use java networking library: GNetLib
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 287
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #1 - Posted 2014-11-04 17:06:16 »

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.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Reply #2 - Posted 2014-11-04 19:05:07 »

Is there a reason you used a null layout and absolute positioning?
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 Tongue

What will happen if the user resizes the dialog?
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 Tongue

Checkout my new easy to use java networking library: GNetLib
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 287
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #3 - Posted 2014-11-04 19:10:25 »

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 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!

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Reply #4 - Posted 2014-11-04 19:26:35 »

What OS are you on btw?

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!
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 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()?

When I run your code, the dialog is resizable.
Weird, just booted up my IDE and I don't see anyway of resizing it lol.


Checkout my new easy to use java networking library: GNetLib
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Reply #5 - Posted 2014-11-04 19:33:33 »

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.

 

Checkout my new easy to use java networking library: GNetLib
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 287
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #6 - Posted 2014-11-04 19:34:55 »

What OS are you on btw?

I'm on Windows 7.

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 Tongue

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

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

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.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Reply #7 - Posted 2014-11-04 19:36:09 »

Will do ^__^

Thanks again Grin

Checkout my new easy to use java networking library: GNetLib
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (295 views)
2018-11-16 13:46:03

hadezbladez (164 views)
2018-11-16 13:41:33

hadezbladez (295 views)
2018-11-16 13:35:35

hadezbladez (75 views)
2018-11-16 13:32:03

EgonOlsen (2156 views)
2018-06-10 19:43:48

EgonOlsen (2183 views)
2018-06-10 19:43:44

EgonOlsen (1370 views)
2018-06-10 19:43:20

DesertCoockie (1988 views)
2018-05-13 18:23:11

nelsongames (1631 views)
2018-04-24 18:15:36

nelsongames (2279 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!