GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Popup lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Popup lists" Watch "Popup lists" New topic
Author

Popup lists

Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I'm in the middle of writing a checkers board game in Java but I need to display a drop down menu to allow people to try out special positions etc or play through famous games. I'm having problems getting the menu to appear where I want it to and suspect it's because I'm not using a layout manager - my game doesn't use them. I just want the menu to popup when the user requests it, the user would scroll through some options and then let him click an item to make something happen.

What am I missing?

The relevant code is:


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
Why are you developing an AWT application?

Why don't you use Swing so you can take advantage of newer features like: Bringing Up a Popup Menu.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Actually I'm not developing an AWT application - I'm developing a game applet and I just need a a simple popup list to allow the user to scroll through some textual choices and pick an item. I did a google on popup lists in Java and came up with what I had before. I had a look at the link you provided but I don't really want a menu bar in the game as it would look rather poor - is there anyway of using these swing lists to simply popup a menu bar in the middle of the game screen? A link to any example code?

PS it's just text in the popup
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
I'm developing a game applet


I should have said "AWT applet". You can also create "Swing applets".

You are using List (which is AWT) instead of JList (which is Swing). However as I suggest you should not be using either, but instead you should be using a JPopupManu.

I don't really want a menu bar in the game


I didn't suggest you use a menu bar. I suggested you use a JPopupMenu.

A link to any example code?


I gave you a link. You can download the code from the tutorial. You can also look at the "table of contents" for other useful topics. There is also a section on making an applet.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I think my main issue is I don't have any Panel, JPanel etc as the existing game doesn't use these. All the examples naturally expect there to be these containers to attach listeners to etc. I'm failing at trying to adapt the code in the link (and others) to the code I have. Is this the only way to go about things? Could I just use drawString to write the options to screen and then have something to click to move up and down the list - I'm quite confident I can do this but just didn't think it would look very pretty.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
I think my main issue is I don't have any Panel, JPanel etc as the existing game doesn't use these.


Well, then now is a good time to learn to develop a Swing applet properly so you can take advantage of all the Swing features without reinventing the wheel yourself.

Again, the Swing tutorial shows you the proper way to do custom painting.

I have no idea what your code looks like if you say you aren't using any Panels or anything like that so it is rather hard to give advice.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Rob Camick wrote:Well, then now is a good time to learn to develop a Swing applet properly so you can take advantage of all the Swing features without reinventing the wheel yourself.

Again, the Swing tutorial shows you the proper way to do custom painting.

As I understand it Swing is used for the management and placement of swing components ie buttons etc. This is fine for standard UI but for games you are usually managing the movement and placement of images yourself. I was under the impression that Swing was used mainly for forms but I'd be very interested in seeing any Swing based games that you've created. The link was very interesting but I can't immediately see how it applies in that I have no buttons to attach listeners to or panels to display lists in.

Rob Camick wrote:I have no idea what your code looks like if you say you aren't using any Panels or anything like that so it is rather hard to give advice.
It's quite simple to have java without swing - you just have graphic images and display directly to these while using double buffering etc to keep the screen movement smooth while the AI runs in separate threads. I would now like to add a popup menu to this game but, if it requires a complete rewrite of the program and results in the game moving away from a realistic board towards a standard java form then I'll probably decline.

Is there a way of just creating a standard popup list that I can add to an existing non-swing java program? Can I pop the window up from anywhere in the program and return what's pressed? If I have generate an attempt at the code (I'm pretty certain I won't be able to get it to work as I've tried many different methods) would someone be able to point out what I need to change or will I just get a blanket comment saying it should of been written in Swing from the onset?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
As I understand it Swing is used for the management and placement of swing components ie buttons etc


Swing and AWT are used to create GUI's. A game is still a GUI. All Swing and AWT do is give you tools to create a GUI. The "tools" are things like components and event listeners etc. Swing is new and its tools are more complete, provide more functionality and are easier to customize.

you just have graphic images and display directly to these while using double buffering etc


Yes but these images still have to be drawn somewhere. Generally these images are drawn to a Canvas (AWT) or a JComponent/JPanel (Swing).

Is there a way of just creating a standard popup list that I can add to an existing non-swing java program?


If you aren't using Swing then you are using AWT and I don't use AWT so I don't know what features are supported.

That is why I suggested Swing. Yu can still do custom painting. I gave you the link that shows you how to do this. You still do painting with the Graphics object but it is easier because you don't have to worry about double buffering since Swing is double buffered by default. In addition you can easily add a popup menu.

All you have to do is download the example code from the tutorial for a quick look to get an idea how it is done.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Am I missing something here, because AWT also has a PopupMenu that you can just add to your existing AWT Applet.
Using a MouseListener/Adapter you could simply call PopupMenu#show() using the data on the MouseEvent; originating component and x/y coordinates.
No need to bring Swing into the mix as far as I can see.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
> No need to bring Swing into the mix as far as I can see.

The reason was to spend time learning Swing rather than spending the time to learn AWT. People don't develop AWT applications any more.

The poster is spending time learning old painting concepts.

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Swing has some very nice features that make it very appealing compared to AWT. That being said there is no need to completely start over with Swing if AWT supports everything you need. You should try and make a list of features you want added to the game and check to see if AWT can support those before going any further. If you find that AWT doesn't have everything you need you might consider switching to Swing, otherwise I'd stick with AWT.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Thanks for your help guys but I can't get it to work. No doubt it's how things are organised in my existing program but such is life. I think I'll try a different approach without menus but thanks for your time and comments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Popup lists