动态上/下移动项目

时间:2014-08-06 15:31:49

标签: java swing

我有一个窗格并向窗格添加项目。目前我已经在我的程序中实现了删除功能,但是我还希望通过向上或向下移动元素来移动元素。但是,我对这个算法很感兴趣。这是一个有效的例子:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class minimumExample extends JFrame {

    private JButton addItem;

    private JComboBox itemBox;

    private String[] itemSelect = { "test1", "test2" };

    private JPanel addUpperPane;

    private JPanel addLowerPane;

    private List<CheckItem> displayedItems = new ArrayList<CheckItem>();

    private JButton upButton;

    private JButton downButton;

    private JButton deleteButton;

    public void createControlPane() {

        addUpperPane = new JPanel();
        addLowerPane = new JPanel(new GridLayout(0, 1));

        addItem = new JButton("Add item");
        upButton = new JButton("Up");
        downButton = new JButton("Down");
        deleteButton = new JButton("Delete");

        itemBox = new JComboBox(itemSelect);

        addItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if(itemBox.getSelectedItem().toString().equals("test1")) {
                    ButtonComp butt = new ButtonComp();
                    butt.init();
                    displayedItems.add(butt);
                    validate();
                    repaint();
                }

                if(itemBox.getSelectedItem().toString().equals("test2")) {
                    LabelComp label = new LabelComp();
                    label.label();
                    displayedItems.add(label);
                    validate();
                    repaint();
                }


                for (int i = 0; i < displayedItems.size(); i++) {
                    addLowerPane.add(displayedItems.get(i).getComponent());
                    validate();
                    repaint();
                }
            }
        });


        deleteButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                System.out.println("delete Item");

                Iterator<CheckItem> it = displayedItems.iterator();
                while (it.hasNext()) {
                    CheckItem next = it.next();
                    if (next.isSelected()) {
                        addLowerPane.remove(next.getComponent());
                        it.remove();
                        continue;
                    }
                }

                validate();
                repaint();

            }
        });

        upButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                System.out.println("move item up");

                //how to move items up

                validate();
                repaint();

            }
        });

        addUpperPane.add(itemBox, BorderLayout.EAST);
        addUpperPane.add(addItem, BorderLayout.WEST);
        addUpperPane.add(new JLabel(" | "), BorderLayout.WEST);
        addUpperPane.add(upButton, BorderLayout.WEST);
        addUpperPane.add(downButton, BorderLayout.WEST);
        addUpperPane.add(deleteButton, BorderLayout.WEST);
        addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));

        //put everything together

        add(addUpperPane, BorderLayout.NORTH);
        add(addLowerPane, BorderLayout.SOUTH);

        repaint();

    }

    private void makeLayout() {

        setTitle("Test App");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));

        createControlPane();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }


    /**
     * starts the GUI
     */
    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) throws IOException {
        minimumExample ex = new minimumExample();
        ex.start();
    }

    public abstract class CheckItem {

        protected JCheckBox check;

        public boolean isSelected() {
            return check.isSelected();
        }

        public abstract Component getComponent();

    }

    public class ButtonComp extends CheckItem {
        JPanel panel = new JPanel();

        public void init() {
            JButton button = new JButton("Test1");
            check = new JCheckBox();
            panel.add(button);
            panel.add(check);
        }

        @Override
        public Component getComponent() {
            return panel;
        }
    }

    public class LabelComp extends CheckItem {

        JPanel panel = new JPanel();

        public void label() {
            JLabel label = new JLabel("Test2");
            check = new JCheckBox();
            panel.add(label);
            panel.add(check);
        }

        @Override
        public Component getComponent() {
            return panel;
        }
    }
}

我研究过可能的解决方案可能是使用Collections.swap。关于如何实现这一点的任何建议,我真的被卡住了......

感谢您的回答! 任何建议如何t

1 个答案:

答案 0 :(得分:0)

您需要的第一件事是获取对要向上或向下移动的组件的引用。您可能需要保留对每次更改每次上移/下移时可以获取的选择的当前所选组件的引用。

之后,我觉得你快到了。

浏览您的displayedItems并在列表中找到该组件的位置。如果你正在向上移动它(并且它上面有一个组件),请使用Collections.swap将它的位置切换到它之前的组件。

然后,您将从addLowerPane中删除所有组件,并再次运行此部分代码。 (因为你重用它,你可能想要创建它自己的方法)

for (int i = 0; i < displayedItems.size(); i++) {
    addLowerPane.add(displayedItems.get(i).getComponent());
    validate();
    repaint();
}