从JPanel移除多个JComboBox

时间:2019-07-19 11:10:02

标签: java swing jcombobox

我正在努力在正在开发的程序中创建一个简单的动作事件。 该程序,当我右键单击它时必须添加新的JComboBoxes。而且有效。 它必须将这些JcomboBox中的文本添加到字符串数组列表中。而且有效。 现在的问题。也许用户想删除一个JComboBox,所以它将从JComboBox数组中删除,然后从字符串数组中删除文本,最后从屏幕中删除文本。它已从两个阵列中正确删除,但仅从屏幕中删除了一次。这是为什么?我添加了validate和repaint方法,但它只会删除“可视” JComboBox的最后一行,而不会“看到”那里的其他行。我究竟做错了什么? 程序内部进行了一些绘画检查,仅用于调试目的,因此我知道除了从JPanel中删除以外,其他所有方法都有效。

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

public class TestingGround
{
    Toolkit toolkit;
    JFrame frame;
    JPopupMenu menu;

    private ArrayList <JComboBox> projectLineComboBoxList;
    private String [] productLineDefinedList = {"-","A","B","C" ,"D","E","F","G", "H", "I", "J","K","L", "M"};   
    private ArrayList userOptionList;
    private JComboBox productLineBox;
    int countAddClicks;

    public TestingGround()
    {
        frame = new JFrame("Testing ground area");

        centerToScreen();

        menu = new JPopupMenu();
        JMenuItem addRow = new JMenuItem("Add ComboBox");
        JMenuItem removeRow = new JMenuItem("Remove ComboBox");
        JPanel panel = new JPanel();
        JPanel mainGridPanel = new JPanel(); 

        mainGridPanel.setLayout(new GridLayout(0,2));
        mainGridPanel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.add(mainGridPanel);

        userOptionList = new ArrayList();
        projectLineComboBoxList = new ArrayList();

        JButton button = new JButton("save state");
        productLineBox = new JComboBox(productLineDefinedList);
        countAddClicks =0;
        panel.add(button);

        // ACTION LISTENERS
        addRow.addActionListener(new ActionListener(){ // Right click to add JComboBoxes to the screen
                public void actionPerformed(ActionEvent event) {
                    System.out.println("Initial click is: " + countAddClicks);

                    productLineBox = new JComboBox(productLineDefinedList); // add a new JComboBox
                    projectLineComboBoxList.add(countAddClicks,productLineBox); // add this JComboBox to the JComboBox Array List

                    countAddClicks++;  
                    System.out.println("After click is: " + countAddClicks);


                    mainGridPanel.add(productLineBox);   
                    mainGridPanel.repaint();
                    mainGridPanel.revalidate();

                }
            });

            // Problem is right here!
        removeRow.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent event) {
                    countAddClicks--;  
                    System.out.println("After removal click is: " + countAddClicks);
                    projectLineComboBoxList.remove(projectLineComboBoxList.size()-1); // delete from the Array of JComboBoxes
                    userOptionList.remove(userOptionList.size()-1);



                   mainGridPanel.remove(productLineBox); // THIS action removes only the last ComboBox from the screen, and does not "see" the other ones that I want to remove. It should remove one by one from the screen.
                   mainGridPanel.revalidate();      
                   mainGridPanel.repaint();


                }
            });

        button.addActionListener (new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    userOptionList.clear(); 

                    for (int i=0; i<projectLineComboBoxList.size(); i++) {                            
                        JComboBox tempBoxxx = projectLineComboBoxList.get(i);
                        String tzz = tempBoxxx.getSelectedItem().toString();
                        userOptionList.add(tempBoxxx.getSelectedItem().toString()); // Add the text to the String Array List             
                    }

                    System.out.println();
                    System.out.println("The length of the JComboBox Array is: " + projectLineComboBoxList.size() );
                    System.out.println("The content of the String array is: " + userOptionList );
                    System.out.println("The size of the String array is: " + userOptionList.size());
                    System.out.println();

                }

            });

        // Cand dau click din butonul cel mai din dreapta (3) se deschide menium popup 
        frame.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent event) {
                    if (event.getButton() == event.BUTTON3) {
                        menu.show(event.getComponent(), event.getX(),event.getY());
                    }
                } 
            }); 

        menu.add(addRow);
        menu.add(removeRow);

        frame.add(panel);

        frame.setVisible(true);
    }

    public void centerToScreen()
    {
        frame.setSize(700,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("A Popup Menu");

        toolkit = frame.getToolkit();
        Dimension size = toolkit.getScreenSize();
        frame.setLocation((size.width-frame.getWidth())/2, (size.height-frame.getHeight())/2);

    }

    public static void main(String[]args){
        new TestingGround();
    }
}

1 个答案:

答案 0 :(得分:1)

我已经尝试过您的代码,但总是在行中出现异常

userOptionList.remove(userOptionList.size()-1);

因为给定列表始终为空。所以我删除了它。

要解决删除问题,您只需将最后删除的组合框保存在变量productLineBox中。因此,您需要替换行

projectLineComboBoxList.remove(projectLineComboBoxList.size()-1); // delete from the Array of JComboBoxes

按行

productLineBox = projectLineComboBoxList.remove(projectLineComboBoxList.size()-1); // delete from the Array of JComboBoxes

在这种情况下,所有功能都可以正常工作,您可以删除所有添加的组合框。

这是我重构的完整代码

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class TestingGround {

    private static final String[] PRODUCT_LINE_LIST = {"-", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"};

    private final List<JComboBox<String>> projectLineComboBoxList;

    private final List<String> userOptionList;

    private int countAddClicks;

    public TestingGround() {
        JFrame frame = new JFrame("Testing ground area");

        centerToScreen(frame);

        JPopupMenu menu = new JPopupMenu();
        JMenuItem addRow = new JMenuItem("Add ComboBox");
        JMenuItem removeRow = new JMenuItem("Remove ComboBox");
        JPanel panel = new JPanel();
        JPanel mainGridPanel = new JPanel();

        mainGridPanel.setLayout(new GridLayout(0, 2));
        mainGridPanel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.add(mainGridPanel);

        userOptionList = new ArrayList<>();
        projectLineComboBoxList = new ArrayList<>();

        JButton button = new JButton("save state");
        countAddClicks = 0;
        panel.add(button);

        // ACTION LISTENERS
        addRow.addActionListener(new ActionListener() { // Right click to add JComboBoxes to the screen
            @Override
            public void actionPerformed(ActionEvent event) {
                System.out.println("Initial click is: " + countAddClicks);

                JComboBox<String> productLineBox = new JComboBox<>(PRODUCT_LINE_LIST); // add a new JComboBox
                projectLineComboBoxList.add(countAddClicks, productLineBox); // add this JComboBox to the JComboBox Array List

                countAddClicks++;
                System.out.println("After click is: " + countAddClicks);

                mainGridPanel.add(productLineBox);
                mainGridPanel.repaint();
                mainGridPanel.revalidate();

            }
        });

        // Problem is right here!
        removeRow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                countAddClicks--;
                System.out.println("After removal click is: " + countAddClicks);
                // delete from the Array of JComboBoxes
                JComboBox<String> productLineBox = projectLineComboBoxList.remove(projectLineComboBoxList.size() - 1);
                // userOptionList.remove(userOptionList.size() - 1);

                mainGridPanel.remove(productLineBox); // THIS action removes only the last ComboBox from the screen, and does not "see" the
                                                      // other ones that I want to remove. It should remove one by one from the screen.
                mainGridPanel.revalidate();
                mainGridPanel.repaint();

            }
        });

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                userOptionList.clear();

                for (int i = 0; i < projectLineComboBoxList.size(); i++) {
                    JComboBox<String> tempBoxxx = projectLineComboBoxList.get(i);
                    String tzz = tempBoxxx.getSelectedItem().toString();
                    userOptionList.add(tzz); // Add the text to the String Array List
                }

                System.out.println();
                System.out.println("The length of the JComboBox Array is: " + projectLineComboBoxList.size());
                System.out.println("The content of the String array is: " + userOptionList);
                System.out.println("The size of the String array is: " + userOptionList.size());
                System.out.println();

            }

        });

        ((JComponent) frame.getContentPane()).setComponentPopupMenu(menu);

        menu.add(addRow);
        menu.add(removeRow);

        frame.add(panel);

        frame.setVisible(true);
    }

    private void centerToScreen(JFrame frame) {
        frame.setSize(700, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("A Popup Menu");
        frame.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestingGround::new);
    }
}