1

我找到了 DualJList示例代码,但我需要在Jlist. 如何在a中添加两列Jlist

我尝试使用ListCellRenderer,但未能向模型中添加元素。

这是带有图像的网页中显示的代码。

4

1 回答 1

12

你的意思是这样的?

双持桌

我会先阅读如何使用表格

主框架...

public class DualTableFrame extends JFrame {

    private JTable leftTable;
    private JTable rightTable;
    private JButton addButton;
    private JButton removeButton;

    public DualTableFrame() {

        setTitle("Dual wielding tables");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLayout(new GridBagLayout());

        leftTable = new JTable(new SimpleColorTableModel());
        rightTable = new JTable(new SimpleColorTableModel());

        setupTable(leftTable);
        setupTable(rightTable);

        populate((SimpleColorTableModel) leftTable.getModel());

        addButton = new JButton("Add >>");
        removeButton = new JButton("<< Remove");

        JPanel pnlActions = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        pnlActions.add(addButton, gbc);

        gbc.gridy++;
        pnlActions.add(removeButton, gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.33;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        add(new JLabel("Available Choices"), gbc);
        gbc.gridx++;
        add(new JPanel(), gbc);
        gbc.gridx++;
        add(new JLabel("Your Choices"), gbc);

        gbc.gridy++;
        gbc.gridx = 0;
        gbc.weighty++;
        gbc.fill = GridBagConstraints.BOTH;

        add(new JScrollPane(leftTable), gbc);
        gbc.gridx++;
        add(pnlActions, gbc);
        gbc.gridx++;
        add(new JScrollPane(rightTable), gbc);

        setSize(400, 600);

        addButton.setEnabled(false);
        removeButton.setEnabled(false);

        leftTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                int count = leftTable.getSelectedRowCount();
                addButton.setEnabled(count > 0);

            }
        });
        rightTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                int count = rightTable.getSelectedRowCount();
                removeButton.setEnabled(count > 0);

            }
        });

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

                moveSelectedRow(leftTable, rightTable);

            }
        });

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

                moveSelectedRow(rightTable, leftTable);

            }
        });

    }

    protected void moveSelectedRow(JTable from, JTable to) {

        SimpleColorTableModel fromModel = (SimpleColorTableModel) from.getModel();
        SimpleColorTableModel toModel = (SimpleColorTableModel) to.getModel();

        for (int index : from.getSelectedRows()) {

            Vector rowValue = (Vector) fromModel.getDataVector().get(index);

            toModel.addRow(rowValue);

        }

        int selectedRow = -1;
        while ((selectedRow = from.getSelectedRow()) != -1) {

            fromModel.removeRow(selectedRow);

        }

        from.clearSelection();

    }

    protected void populate(SimpleColorTableModel model) {

        model.addRow(new Object[]{"Black", Color.BLACK});
        model.addRow(new Object[]{"Blue", Color.BLUE});
        model.addRow(new Object[]{"Cyan", Color.CYAN});
        model.addRow(new Object[]{"Dark Gray", Color.DARK_GRAY});
        model.addRow(new Object[]{"Gray", Color.GRAY});
        model.addRow(new Object[]{"Green", Color.GREEN});
        model.addRow(new Object[]{"Light Gary", Color.LIGHT_GRAY});
        model.addRow(new Object[]{"Mangenta", Color.MAGENTA});
        model.addRow(new Object[]{"Orange", Color.ORANGE});
        model.addRow(new Object[]{"Pink", Color.PINK});
        model.addRow(new Object[]{"Red", Color.RED});
        model.addRow(new Object[]{"White", Color.WHITE});
        model.addRow(new Object[]{"Yellow", Color.YELLOW});

    }

    protected void setupTable(JTable table) {

        table.setFillsViewportHeight(true);

        table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());

    }
}

表格单元格渲染器

public class ColorTableCellRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        setText(null);
        if (value instanceof Color) {

            setOpaque(true);
            setBackground((Color)value);

        }

        return this;

    }

}

表模型

public class SimpleColorTableModel extends DefaultTableModel {

    public SimpleColorTableModel() {

        addColumn("Name");
        addColumn("Color");

    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {

        Class clazz = String.class;

        switch (columnIndex) {

            case 1:
                clazz = Color.class;
                break;

        }

        return clazz;

    }

}
于 2012-08-11T05:59:59.120 回答