Results 1 to 2 of 2
  1. #1
    lazaeus is offline Member
    Join Date
    Dec 2010
    Rep Power

    Lightbulb Adding different popup menus to different buttons.

    I am making a game and I am stuck on something. In a loop I want to fill buttons with data from an Vector. Each one should have a different JPopupMenu on right click. If the item is a sword, and I right click on the button, it should say Equip sword and Destroy sword.

    Item Class
    Java Code:
    import java.util.Vector;
    public class Item {
    	protected String weaponName, modifierType, classType;
    	protected int damage, levelReq;
    	protected static Vector<Item> allWeapons = new Vector<Item>();
    	protected static Vector<Item> allItems = new Vector<Item>();
    	//default constructor initializes weapons and items---->
    	Item() {
    	//constructor for weapons creation--------------------->
    	Item(String _Name, String _Modifier, String _class, int _level, int _damage){
    	//initialize all of the games weapons------------------>
    	public void initWeapons(){
    		Item s = new Item("Sword", "Normal", "Swordsman", 1, 3);
    		Item ar = new Item("Bow", "Normal", "Archer", 1, 3);
    		Item ac = new Item("Staff", "Normal","Acolyte", 1, 3);
    		Item s2 = new Item("Straight Sword", "Normal", "Swordsman", 3, 5);
    		Item ultraBlade = new Item("Dark-Chasm Staff", "Flame", "NONE", 30, 60);
    		for ( int i = 0; i < 23; i ++)
    	//set functions--------------------------------------->
    	public void setName(String _Name) {
    		weaponName = _Name;
    	public void setModifierType(String _Modifier){
    		modifierType = _Modifier;
    	public void setDamage(int _damage){
    		damage = _damage;
    	public void setClass(String _class) {
    		classType = _class;
    	public void setLevel(int level) {
    		levelReq = level;
    	//get functions-------------------------------------->
    	public String getName() {
    		return weaponName;
    	public String getModifier(){
    		return modifierType;
    	public String getClassType(){
    		return classType;
    	public int getLevel(){
    		return levelReq;
    	public int	getDamage(){
    		return damage;
    Inventory Class
    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class Inventory {
    	private static int inventorySize = 25;
    	private int capRemaining;
    	private JButton[] inventorySlot;
    	protected static Vector<Item> inventory = new Vector<Item>(inventorySize);
    	public JMenuItem destroy, equip;
    	public JPopupMenu popup;
    	//Constructor for making users inventory-------------------->
    	public Inventory(int _inventorySize){
    		for(int i = 0; i < 25; i ++)
    	//Create buttons-------------------------------------------->
    	public void createButtons(){
    		inventorySlot = new JButton[inventorySize];
    		//Inner class to handle buttons clicked----------------->
    		class InventoryHandler implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				Object source = e.getActionCommand();
    		class popupHandler extends MouseAdapter {
    			public void mousePressed(MouseEvent e) {
    		    public void mouseReleased(MouseEvent e) {
    			private void maybeShowPopup(MouseEvent e) {
    				if (e.isPopupTrigger()) {
    							e.getX(), e.getY());
    		//Fill buttons with Item Data--------------------------->
    		for (int i = 0; i < inventory.size(); i++) {
    			inventorySlot[i] = new JButton(inventory.elementAt(i).weaponName);
    			inventorySlot[i].setToolTipText("Required Level: " 
    					+ inventory.elementAt(i).levelReq
    					+ "\n **Weapon name: " + inventory.elementAt(i).weaponName
    					+ "\n **Weapon modifier: " + inventory.elementAt(i).modifierType
    					+ "\n **Attack: +" + inventory.elementAt(i).damage
    					+ "\n **REQUIRED LEVEL: " + inventory.elementAt(i).levelReq
    					+ "\n **REQUIRED CLASS: " + inventory.elementAt(i).classType);
    			equip = new JMenuItem("Equip " + inventory.elementAt(i).weaponName);
    			destroy = new JMenuItem("Destroy " + inventory.elementAt(i).weaponName);
    			popup = new JPopupMenu();
    			inventorySlot[i].addActionListener(new InventoryHandler());
    			inventorySlot[i].addMouseListener(new popupHandler());
    		//Fill the rest with Empty------------------------------>
    		for (int i = 0; i < getCapRemaining(); i++) {
    			inventorySlot[i] = new JButton("empty");
    	//Set Inventories size-------------------------------------->
    	private void setSize(int _inventorySize){
    		inventorySize = _inventorySize;
    	//Get inventory size---------------------------------------->
    	private int getSize(){
    		return inventorySize;
    	//Get capacity remaining of inventory----------------------->
    	private int getCapRemaining(){
    		return capRemaining = getSize()- inventory.size();
    	//remove an item from the inventory------------------------->
    	private void removeAt(int location){
    Right now I have just stupid items filling the array for testing. I tried setting up a JPopupMenu[] but I couldn't think of a way to do it to return to the popupHandler class... I set the mnemonic as a reference to which button it is in order. I couldn't find a way to use that either. I honestly want to learn how to figure this out for future use. Thank you in advance.

  2. #2
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Ktm, Nepal
    Rep Power


    Try this, it will help
    Java Code:
    public class RightClickPopupMenu implements ActionListener, ItemListener {
     public void createAndShowPopupMenu() {
       JPopupMenu popup = new JPopupMenu(title);
       JMenuItem newMenu = new JMenuItem(" New ");
      MouseListener popupListener = new PopupListener(popup);
    class PopupListener extends MouseAdapter {
     // -- code 
    Last edited by Prajin; 12-27-2010 at 05:39 AM. Reason: Shit, finger slipped and posted only half.

Similar Threads

  1. Adding a KeyListener to a JFrame with buttons.
    By jamhead in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 08:29 PM
  2. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 06:19 AM
  3. Adding popup Menu on animation
    By Peggy in forum Java Applets
    Replies: 2
    Last Post: 12-10-2008, 09:33 AM
  4. Creating popup menus with Swing
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:49 PM
  5. Replies: 0
    Last Post: 12-17-2007, 10:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts