Component / LayoutManager显示JPanel列表

时间:2012-10-01 14:50:22

标签: java swing drag-and-drop jpanel layout-manager

有没有办法显示JPanels列表并重新排列?创建列表可以使用BoxLayout完成,但是如何交换两个JPanels呢?小组保持互动也很重要。

我会尝试一个例子:

import java.awt.Component;
import java.awt.EventQueue;
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.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class PanelList {

    private static final class Entry extends JPanel {

        public Entry(final int i) {
            super();
            final JButton button = new JButton("Dialog '" + i + "'");
            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(Entry.this, "Hello World '"
                            + i + "'");
                }
            });
            this.add(button);
        }
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("PanelList");
                f.setLayout(new GridLayout(1, 2));
                f.add(new BoxLayoutList());
                f.add(new JListList());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static final class BoxLayoutList extends JPanel {
        private final List<Entry> entries = new ArrayList<>();

        public BoxLayoutList() {
            this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            int i = 0;
            while (i < 10) {
                final Entry entry = new Entry(i);
                this.add(entry);
                this.entries.add(entry);
                i++;
            }
        }
    }

    private static final class JListList extends JPanel {
        private final DefaultListModel<Entry> entries = new DefaultListModel<>();

        public JListList() {
            final JList<Entry> entryComponent = new JList<>(this.entries);
            this.add(entryComponent);
            entryComponent.setCellRenderer(new ListCellRenderer<Entry>() {
                @Override
                public Component getListCellRendererComponent(
                        JList<? extends Entry> list, Entry value, int index,
                        boolean isSelected, boolean cellHasFocus) {
                    return value;
                }
            });
            int i = 0;
            while (i < 10) {
                final Entry entry = new Entry(i);
                this.entries.addElement(entry);
                i++;
            }
            // Example: Swap entries 4 and 2
            final Entry buffer = this.entries.get(4);
            this.entries.set(4, this.entries.get(2));
            this.entries.set(2, buffer);
        }
    }
}

现在BoxLayout是交互式的,但我不知道如何像JList替换中的条目4和2那样交换条目。但在那里,我无法与组件进行交互。

2 个答案:

答案 0 :(得分:3)

使用JList 1 与drag'n'drop 2 重新排序。

  1. How to Use Lists
  2. Drag and Drop and Data Transfer: Intro

答案 1 :(得分:1)

从您的评论看起来我们认为太复杂了 - 如果您想以编程方式交换两个孩子,您只需要

container.add(comp, index)

一个快速片段(最粗略的逻辑,带有硬编码索引:)

final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
for (int i = 0; i < 6; i++) {
    panel.add(new JTextField("was at: " + i, 20));
}

Action action = new AbstractAction("exchange") {

    @Override
    public void actionPerformed(ActionEvent e) {
        JTextField one = (JTextField) panel.getComponent(2);
        JTextField second = (JTextField) panel.getComponent(5);
        second.setText(second.getText() + " -> 2");
        one.setText(one.getText() + " -> 5");
        panel.add(second, 2);
        panel.add(one, 5);
        panel.revalidate();
    }

};
frame.add(new JButton(action), BorderLayout.SOUTH);