arrayList中的item从面板中删除但不是从实际的arrayList中删除?

时间:2015-12-17 10:14:31

标签: java arraylist jcombobox

之前我编写了一个程序,可以添加用户,然后在用户名旁边创建一个带有复选框的列表,然后您就可以从中选择并按删除按钮删除用户名和旁边的复选框。 现在,在构建并组合我的实际程序之后,我意识到名称实际上并没有从实际列表中删除,而只是从面板中删除(因此它只是变得不可见)。我怎样才能使它实际上从arraylist而不仅仅是面板中删除?还有一种方法可以使它不接受相同的用户名吗?

我的完整程序代码位于:GITHUB

在这里,当我运行MenuWindow并点击Manage Users时,我的ManageUsersGUI窗口弹出,它允许我添加和删除用户。当我关闭它并按下View Available Chores时,它会打开另一个窗口,在该窗口中,它允许我查看用户的杂项,我可以从JComboBox中进行选择。但它显示了所有相同的重复名称和我删除的名称。如果我返回并按“管理用户”按钮,则会再次弹出窗口,并且之前没有用户列表。它再次为空,如果添加相同的名称,它将允许您再次添加,当您删除它时实际上不会删除。

这是我的ManageUsersGUI代码:

class IndexController extends Zend_Controller_Action {

    public function indexAction() {


        $param = $this->getRequest()->getPost('id');
        if ($param != NULL) {
            $_custObj = new Application_Model_DbTable_Shuffle();
            $this->view->items = $_custObj->getData($param);
        }
    }

}

更新#2:添加了要从arrayList中删除的代码

$(document).ready(function () {

    $('.libox').click(function () {
        var contentPanelId = $(this).attr("id");
        alert(contentPanelId);
        ajaxFunction(contentPanelId);
              function ajaxFunction(contentPanelId) {
            var ajaxRequest;
            try {
                ajaxRequest = new XMLHttpRequest();
            }
            catch (e) {
                alert("upgrade your browser");
                return false;
            }

            ajaxRequest.onreadystatechange = function () {
                if (ajaxRequest.readyState === 4) {
                }
            };

            ajaxRequest.open("/default/index", true);
            ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            ajaxRequest.send("id=" + contentPanelId);
        }
        location.href = "/";
    });
});

现在给我这个长期错误:

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

public class ManageUsersGUI extends JFrame {
    public ArrayList<User> users;
    private JLabel title;
    private JLabel addNewUserLabel;
    private JTextField addNewUserTextField;
    private JLabel deleteUsersLabel;
    private JButton addButton;
    private JButton deleteButton;
    private JPanel namePanel;
    final int WINDOW_WIDTH = 500;
    final int WINDOW_HEIGHT = 400;


    public ManageUsersGUI() {
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        //construct components
        title = new JLabel ("Manage Users");
        title.setFont(new Font("Serif", Font.PLAIN, 28));

        addNewUserLabel = new JLabel ("Add new User here:");
        addNewUserTextField = new JTextField (0);
        deleteUsersLabel = new JLabel ("Select which User(s) you would like to delete:");
        addButton = new JButton ("Add");
        deleteButton = new JButton ("Delete");
        namePanel = new JPanel();
        namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));

        //set components properties
        addNewUserTextField.setToolTipText ("Enter name and click on Add button.");
        addButton.setToolTipText ("Click here to Add new user.");
        deleteButton.setToolTipText ("Click here to delete User(s) selected.");

        //adjust size and set layout
        setPreferredSize (new Dimension (500, 600));
        setLayout (null);

        //add components
        add (title);
        add (addNewUserLabel);
        add (addNewUserTextField);
        add (deleteUsersLabel);
        add (namePanel);
        add (addButton);
        add (deleteButton);

        //set component bounds (only needed by Absolute Positioning)
        title.setBounds (170, 10, 300, 100);
        addNewUserLabel.setBounds (65, 90, 120, 25);
        addNewUserTextField.setBounds (200, 90, 125, 25);
        deleteUsersLabel.setBounds (135, 120, 281, 25);
        addButton.setBounds (350, 90, 90, 25);
        namePanel.setBounds(225, 270, 140, 0);
        deleteButton.setBounds (200, 300, 100, 25);

        addButton.addActionListener(new AddButtonListener());

        deleteButton.addActionListener(new DeleteButtonListener());

        setVisible(true);
    }      

    private class AddButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String text = addNewUserTextField.getText();
            ChoreScoreData.getUserList().add(new User(text));

            // Display the changes.
            JOptionPane.showMessageDialog(null, text + " has been added.");

            JCheckBox nameCheckBox = new JCheckBox();
            nameCheckBox.setText(addNewUserTextField.getText());
            namePanel.add(nameCheckBox);
            namePanel.setBounds(225, 140, 140, namePanel.getHeight() + 25);
            deleteButton.setBounds(200, deleteButton.getY() + 25, 100, 25);
            JFrame frame = (JFrame) getRootPane().getParent();
            frame.setSize(frame.getWidth(), frame.getHeight() + 25);
            frame.pack(); 
        }
    }

    private class DeleteButtonListener implements ActionListener {
         @Override
         public void actionPerformed(ActionEvent e) {
            for(Component component : namePanel.getComponents()) {
               if(component instanceof JCheckBox) {
                  if(((JCheckBox)component).isSelected())
                     namePanel.remove(component);
                     for (User user: users) {
                       users.remove(user);
                     }
            }
            namePanel.revalidate();
            namePanel.repaint();
         }   
    }

    public static void main (String[] args) {
        JFrame frame = new JFrame ("AddUsersPanel1");
        frame.setTitle("Manage Users");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new ManageUsersGUI());
        frame.pack();
        frame.setVisible (true);
    }
}

1 个答案:

答案 0 :(得分:3)

没有任何东西被从arraylist中删除,因为你没有从一个集会上删除任何东西。我可以看到你从摇摆组件中删除组件但从未从arraylist中删除

编辑:您发布了从arraylist中删除的代码,您要从正在迭代的集合中删除,这总是坏消息。 您应该使用迭代器,如下所示:

for (Iterator it = users.iterator(); it.hasNext();) {
    it.next();
    it.remove();
}

再次编辑:由于您似乎要删除arraylist中的所有内容,为什么不简单地使用

users.clear();