-1

Invoking the ArrayList add(E e) method has not worked for me so far, I would like to know how to fix it and why I am getting the error in the first place.

GUITest.java

//import statements
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUITest implements ActionListener, Runnable {

    private JFrame frmInventorysystem;
    private JPanel frameTop, frameBottom;
    private JComboBox equipList;
    private String category = "";
    private JButton confirmBtn, cancelBtn;
    private JButton uploadBtn;
    private Weapon weapon;
    private ArrayList<Weapon> weapons = new ArrayList<Weapon>();
    private Armor armor;
    private ArrayList<Armor> armors = new ArrayList<Armor>();
    private GUIWeaponCategory weaponGUI = null;
    private GUIArmorCategory armorGUI = null;

    public GUITest() {
        // insert formatting code here
                frmInventorysystem = new JFrame();
        frmInventorysystem.setTitle("InventorySystem");
        frmInventorysystem.setBounds(100, 100, 450, 300);
        frmInventorysystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmInventorysystem.getContentPane().setLayout(new BorderLayout(0, 0));

        frameTop = new JPanel();
        frameTop.setLayout(new FlowLayout());

        /*
         * JFrame inside another JFrame is not possible. JPanels are used instead
         * Creating a flow layout for the bottom frame
         */
        frameBottom = new JPanel();
        frameBottom.setLayout(new FlowLayout());

        // creates comboBox to find out which of the three items player is looking to
        // insert
        // equipList.setModel(new DefaultComboBoxModel(new String[] {"Weapon", "Armor",
        // "Mod"}));
        String[] weaponCategories = { "Weapon", "Armor", "Mod" };
        equipList = new JComboBox(weaponCategories);
        frameTop.add(equipList);
        frmInventorysystem.getContentPane().add(frameTop, BorderLayout.NORTH);

        // Converting BorderLayout.south into a flow layout
        frmInventorysystem.getContentPane().add(frameBottom, BorderLayout.SOUTH);

        confirmBtn = new JButton("Confirm");
        confirmBtn.addActionListener(this);

        // creates new windows to sort equipment when confirmBtn is clicked
        frameBottom.add(confirmBtn);

        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(this);

        frameBottom.add(cancelBtn);

        uploadBtn = new JButton("Upload");

        uploadBtn.addActionListener(this);

        frameBottom.add(uploadBtn);

        frmInventorysystem.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        // creates new windows to sort equipment when confirmBtn is clicked
        if (e.getSource() == confirmBtn) {
            if (equipList.getSelectedItem().equals("Weapon")) {
                weaponGUI = new GUIWeaponCategory();
                SwingUtilities.invokeLater(weaponGUI);
            }
        }
        // Exits when cancelBtn is clicked
        // exit program

        if (e.getSource() == uploadBtn) {
            // uploads data to excel sheet
            if (weaponGUI != null) {
                weapons = weaponGUI.getWeapons();
                System.out.println(weaponGUI.toString());
            }
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        GUITest window = new GUITest();
        SwingUtilities.invokeLater(window);
    }
}

GUIWeaponCategory.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;

public class GUIWeaponCategory implements ActionListener, Runnable {
    private JButton confirmBtn, cancelBtn;
    private JFrame frmGuiweaponcategory;
    private JPanel frameBottom, frameCenter;
    private String weaponType, weaponVariant, activeSkill, handlingSkill, holsteredSkill;
    private String[] weaponTypes, weaponVariants, activeSkills, handlingSkills, holsteredSkills;
    private JComboBox boxWeaponType, boxActiveSkill, boxHandlingSkill, boxHolsteredSkill;
    private JTextPane paneWeaponVariant;
    // creates weapon object
    // thoughts on creating a Weapon data structure?
    private Weapon weapon;
    private ArrayList<Weapon> weapons = new ArrayList<Weapon>();

    public GUIWeaponCategory() {
        // insert code for GUI here
        frmGuiweaponcategory = new JFrame();
        frmGuiweaponcategory.setTitle("GUIWeaponCategory");
        frmGuiweaponcategory.setBounds(100, 100, 450, 300);
        frmGuiweaponcategory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // new JPanel because JFrames can't be put in a JFrame
        // sets frame to be a FlowLayout as well
        frameBottom = new JPanel();
        frameBottom.setLayout(new FlowLayout());

        // comboBox for easy access to weapon category types
        boxWeaponType = new JComboBox();
        weaponTypes = new String[] { "Assault Rifle", "Light Machine Gun", "Marksman Rifle", "Pistol", "Rifle",
                "Shotgun", "Submachine Gun" };
        Arrays.sort(weaponTypes);
        boxWeaponType.setModel(new DefaultComboBoxModel(weaponTypes));
        frmGuiweaponcategory.getContentPane().add(boxWeaponType, BorderLayout.NORTH);

        // BorderLayout.SOUTH is now a FlowLayout
        frmGuiweaponcategory.getContentPane().add(frameBottom, BorderLayout.SOUTH);

        confirmBtn = new JButton("Confirm");
        confirmBtn.addActionListener(this);
        frameBottom.add(confirmBtn);

        // adds cancelBtn and exits when buttonClicked
        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(this);
        cancelBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frmGuiweaponcategory.dispose();
            }
        });
        frameBottom.add(cancelBtn);

        frameCenter = new JPanel();
        frmGuiweaponcategory.getContentPane().add(frameCenter, BorderLayout.CENTER);
        GridBagLayout gbl_frameCenter = new GridBagLayout();
        gbl_frameCenter.columnWidths = new int[] { 50, 50, 50 };
        gbl_frameCenter.rowHeights = new int[] { 50, 50, 50, 50 };
        gbl_frameCenter.columnWeights = new double[] { 0.0, 1.0 };
        gbl_frameCenter.rowWeights = new double[] { 1.0, Double.MIN_VALUE, 0.0 };
        frameCenter.setLayout(gbl_frameCenter);

        JLabel weaponVariant = new JLabel("Weapon Variant");
        GridBagConstraints gbc_weaponVariant = new GridBagConstraints();
        gbc_weaponVariant.insets = new Insets(0, 0, 5, 5);
        gbc_weaponVariant.gridx = 0;
        gbc_weaponVariant.gridy = 0;
        frameCenter.add(weaponVariant, gbc_weaponVariant);

        paneWeaponVariant = new JTextPane();
        GridBagConstraints gbc_paneWeaponVariant = new GridBagConstraints();
        gbc_paneWeaponVariant.fill = GridBagConstraints.HORIZONTAL;
        gbc_paneWeaponVariant.insets = new Insets(0, 0, 5, 5);
        gbc_paneWeaponVariant.gridx = 1;
        gbc_paneWeaponVariant.gridy = 0;
        frameCenter.add(paneWeaponVariant, gbc_paneWeaponVariant);

        // Add label to mark combo box
        JLabel activeLabel = new JLabel("Active Skill");
        GridBagConstraints gbc_activeLabel = new GridBagConstraints();
        gbc_activeLabel.insets = new Insets(0, 0, 5, 5);
        gbc_activeLabel.gridx = 0;
        gbc_activeLabel.gridy = 1;
        frameCenter.add(activeLabel, gbc_activeLabel);

        boxActiveSkill = new JComboBox();
        activeSkills = new String[] { "Boomerang", "BreadBasket", "Close & Personal", "Eyeless", "Fast Hands",
                "Finisher", "First Blood", "Frenzy", "Ignited", "Killer", "Lucky Shot", "Measured", "Naked",
                "Near Sighted", "On Empty", "Optimist", "Outsider", "Perpetuation", "Premeditated", "Preservation",
                "Pummel", "Ranger", "Reformation", "Rifleman", "Sadist", "Salvage", "Spike", "Steady Handed",
                "Strained", "Unhinged", "Unwavering", "Vindictive" };
        Arrays.sort(activeSkills);
        boxActiveSkill.setModel(new DefaultComboBoxModel(activeSkills));
        GridBagConstraints gbc_boxActiveSkill = new GridBagConstraints();
        gbc_boxActiveSkill.insets = new Insets(0, 0, 5, 5);
        gbc_boxActiveSkill.gridx = 1;
        gbc_boxActiveSkill.gridy = 1;
        frameCenter.add(boxActiveSkill, gbc_boxActiveSkill);

        JLabel lblHandlingSkill = new JLabel("Handling/Equipped Skill");
        GridBagConstraints gbc_lblHandlingSkill = new GridBagConstraints();
        gbc_lblHandlingSkill.insets = new Insets(0, 0, 5, 5);
        gbc_lblHandlingSkill.gridx = 0;
        gbc_lblHandlingSkill.gridy = 2;
        frameCenter.add(lblHandlingSkill, gbc_lblHandlingSkill);

        boxHandlingSkill = new JComboBox();
        handlingSkills = new String[] { "Cannon", "In Rhythm", "Protected Deploy", "Protected Reload", "Recharged",
                "Rooted", "Stop, Drop, and Roll", "Zen", "Accurate", "Allegro", "Distance", "Extra", "Jazz Hands",
                "Optimized", "Stable" };
        Arrays.sort(handlingSkills);
        boxHandlingSkill.setModel(new DefaultComboBoxModel(handlingSkills));
        GridBagConstraints gbc_boxHandlingSkill = new GridBagConstraints();
        gbc_boxHandlingSkill.insets = new Insets(0, 0, 5, 5);
        gbc_boxHandlingSkill.gridx = 1;
        gbc_boxHandlingSkill.gridy = 2;
        frameCenter.add(boxHandlingSkill, gbc_boxHandlingSkill);

        JLabel lblHolsteredSkill = new JLabel("Holstered Skill:");
        GridBagConstraints gbc_lblHolsteredSkill = new GridBagConstraints();
        gbc_lblHolsteredSkill.insets = new Insets(0, 0, 0, 5);
        gbc_lblHolsteredSkill.gridx = 0;
        gbc_lblHolsteredSkill.gridy = 3;
        frameCenter.add(lblHolsteredSkill, gbc_lblHolsteredSkill);

        boxHolsteredSkill = new JComboBox();
        holsteredSkills = new String[] { "Double Duty", "Everlasting", "Greased", "Overlap", "Transmission",
                "Wascally" };
        Arrays.sort(holsteredSkills);
        boxHolsteredSkill.setModel(new DefaultComboBoxModel(holsteredSkills));
        GridBagConstraints gbc_boxHolsteredSkill = new GridBagConstraints();
        gbc_boxHolsteredSkill.insets = new Insets(0, 0, 0, 5);
        gbc_boxHolsteredSkill.gridx = 1;
        gbc_boxHolsteredSkill.gridy = 3;
        frameCenter.add(boxHolsteredSkill, gbc_boxHolsteredSkill);

        // sets the JFrame to be visible
        frmGuiweaponcategory.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == confirmBtn) {
            // puts selected items into String variables
            // replace with a method to create a weapon later
            // consider the possibility for adding to a data structure
            createWeapon();
            frmGuiweaponcategory.dispose();
        }

        else if (e.getSource() == cancelBtn) {
            frmGuiweaponcategory.dispose();
        }
    }

    public ArrayList<Weapon> createWeapon() {
        weaponType = boxWeaponType.getSelectedItem().toString();
        weaponVariant = paneWeaponVariant.getText();
        activeSkill = boxWeaponType.getSelectedItem().toString();
        handlingSkill = boxHandlingSkill.getSelectedItem().toString();
        holsteredSkill = boxHolsteredSkill.getSelectedItem().toString();
        weapons.add(new Weapon(weaponType, weaponVariant, activeSkill, handlingSkill, holsteredSkill));
        return weapons;
    }

    public ArrayList<Weapon> getWeapons() {
        return weapons;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
}
public class Weapon {
    String weaponType, weaponVariant, activeSkill, handlingSkill, holsteredSkill;

    public Weapon(String weaponType, String weaponVariant, String activeSkill, String handlingSkill, String holsteredSkill )
    {
        this.weaponType = weaponType;
        this.weaponVariant = weaponVariant;
        this.activeSkill = activeSkill;
        this.handlingSkill = handlingSkill;
        this.holsteredSkill = holsteredSkill;
        }
    public String toString()
    {
        return weaponType + " " + weaponVariant + " " + activeSkill + " " + handlingSkill + " " + holsteredSkill;
    }
}

So far the code has no issues passing one weapon object from one class to another.

But however, changing the code to include Arraylists made it so that the program on stores only 1 weapon object in the Arraylist.

I would like to be able to pass the entire arraylist from GUIWeaponCategory to GUITest when the button is clicked.

PredictableBeaco
  • 97
  • 1
  • 1
  • 8
  • 2
    Your code cannot be compiled and of course run. Please consider creating a [mcve] in order to get more help. – George Z. Jun 25 '19 at 16:07
  • *"So far the code has no issues passing one weapon object from one class to another."* Well, it HAS issues, you're defining variables outside the scope of a class, so your code won't compile. Also, I can't see where you're passing the `weapon` object from one class to the other, so we cannot test. Post a proper [mcve] that we can copy-paste and see the same issue as you. Try with a single `String` then an `ArrayList` of `Strings` then use your own `Weapon` objects – Frakcool Jun 25 '19 at 16:24
  • @FrakCool. Thanks. Will give that a shot first – PredictableBeaco Jun 25 '19 at 16:27
  • Also see [The use of multiple JFrames, good / bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (Bad) – Frakcool Jun 25 '19 at 16:31
  • Thanks, will keep that in mind – PredictableBeaco Jun 25 '19 at 16:45

1 Answers1

0

Try moving your instantiation of the Array list to the constructor in one class, then refer to that class' specific getter/setter of that weapon list. Your code looks like it creates a new weapon list in multiple classes, which would explain why you are having issues with it containing one value (because they are all separate instances).

private ArrayList<Weapon> weapons;

        public GUIWeaponCategory() {
        //insert code for GUI here
           weapons = new ArrayList<Weapon>()
        }