Results 1 to 2 of 2
- 12-27-2010, 04:10 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
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
Inventory ClassJava 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() { initWeapons(); } //constructor for weapons creation---------------------> Item(String _Name, String _Modifier, String _class, int _level, int _damage){ setName(_Name); setModifierType(_Modifier); setClass(_class); setLevel(_level); setDamage(_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 ++) allWeapons.add(ultraBlade); allWeapons.add(s); allWeapons.add(ar); } //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; } }
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.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){ setSize(_inventorySize); for(int i = 0; i < 25; i ++) inventory.add(Item.allWeapons.elementAt(i)); createButtons(); } //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(); System.out.println(source); } } class popupHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(), 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); GUI.mInventoryPanel.add(inventorySlot[i]); equip = new JMenuItem("Equip " + inventory.elementAt(i).weaponName); destroy = new JMenuItem("Destroy " + inventory.elementAt(i).weaponName); popup = new JPopupMenu(); popup.add(equip); popup.add(destroy); inventorySlot[i].addActionListener(new InventoryHandler()); inventorySlot[i].setMnemonic(i); inventorySlot[i].addMouseListener(new popupHandler()); } //Fill the rest with Empty------------------------------> for (int i = 0; i < getCapRemaining(); i++) { inventorySlot[i] = new JButton("empty"); GUI.mInventoryPanel.add(inventorySlot[i]); } } //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){ inventory.remove(location); } }
- 12-27-2010, 04:34 AM #2
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 "); popup.add(newMenu); MouseListener popupListener = new PopupListener(popup); mainFrame.tabbedPane.addMouseListener(popupListener); } } class PopupListener extends MouseAdapter { // -- code }Last edited by Prajin; 12-27-2010 at 04:39 AM. Reason: Shit, finger slipped and posted only half.
Similar Threads
-
Adding a KeyListener to a JFrame with buttons.
By jamhead in forum AWT / SwingReplies: 1Last Post: 12-11-2010, 07:29 PM -
Help: Displaying Images AND Adding Buttons
By Rhez in forum New To JavaReplies: 2Last Post: 08-05-2010, 06:19 AM -
Adding popup Menu on animation
By Peggy in forum Java AppletsReplies: 2Last Post: 12-10-2008, 08:33 AM -
Creating popup menus with Swing
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:49 PM -
Adding custom buttons - JOptionPane.showOptionDialog(...)
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:46 AM


LinkBack URL
About LinkBacks

Bookmarks