如何在java中使用for循环或嵌套for循环填充2D数组

时间:2016-03-01 04:39:03

标签: java arraylist multidimensional-array

我有一个2D数组,它的类型是对象。我想填充这个2D数组。但我不知道这个。

这就是我所知道的。

public TableRowHeaderTest(List<Employee> list){
    Object rowData1[][] = new Object[list.size()][];

    Object columnNames1[] ={"HEADER 1", "HEADER 2", "HEADER 3", "HEADER 4", "HEADER 5", "HEADER 6", "HEADER 7", "HEADER 8", "HEADER 9", "HEADER 10", "HEADER 11", "HEADER 12", "HEADER 13"};        

    JTable table1 = new TableWithRowHeader(rowData1, columnNames1);
    table1.getColumnModel().getColumn(0).setPreferredWidth(120);
    JScrollPane scrollPane1 = new JScrollPane(table1);
    scrollPane1.setColumnHeaderView(null);

    JFrame frame = new JFrame("Testing");
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(jPanel, BorderLayout.NORTH);
    frame.add(scrollPane1);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

    public class TableWithRowHeader extends JTable{

    private TableRowHeader rowHeader;

    public TableWithRowHeader(final Object[][] rowData, final Object[] columnNames)
    {
        super(rowData, columnNames);
        rowHeader = new TableRowHeader(this);
    }

    @Override
    protected void configureEnclosingScrollPane()
    {
        // This is required as it calls a private method...
        super.configureEnclosingScrollPane();

        Container parent = SwingUtilities.getUnwrappedParent(this);
        if (parent instanceof JViewport)
        {

            JViewport port = (JViewport) parent;
            Container gp = port.getParent();
            if (gp instanceof JScrollPane)
            {

                JScrollPane scrollPane = (JScrollPane) gp;
                JViewport viewport = scrollPane.getViewport();
                if (viewport == null || SwingUtilities.getUnwrappedView(viewport) != this)
                {
                    return;
                }
                scrollPane.setColumnHeaderView(null);
                scrollPane.setRowHeaderView(rowHeader);
            }
        }
    }

}

public class TableRowHeader extends JTableHeader{

    private JTable table;

    public TableRowHeader(JTable table)
    {
        super(table.getColumnModel());
        this.table = table;

        table.getColumnModel().addColumnModelListener(new TableColumnModelListener()
        {

            @Override
            public void columnAdded(TableColumnModelEvent e)
            {
                repaint();
            }

            @Override
            public void columnRemoved(TableColumnModelEvent e)
            {
                repaint();
            }

            @Override
            public void columnMoved(TableColumnModelEvent e)
            {
                repaint();
            }

            @Override
            public void columnMarginChanged(ChangeEvent e)
            {
                repaint();
            }

            @Override
            public void columnSelectionChanged(ListSelectionEvent e)
            {
                // Don't care about this, want to highlight the row...
            }
        });
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
        {
            @Override
            public void valueChanged(ListSelectionEvent e)
            {
                repaint();
            }
        });
    }

    public JTable getTable()
    {
        return table;
    }

    @Override
    public Dimension getPreferredSize()
    {
        Dimension size = new Dimension();
        JTable table = getTable();
        if (table != null)
        {
            TableColumnModel model = table.getColumnModel();
            if (model != null)
            {

                for (int index = 0; index < model.getColumnCount(); index++)
                {

                    TableColumn column = model.getColumn(index);
                    TableCellRenderer renderer = column.getHeaderRenderer();
                    if (renderer == null)
                    {

                        renderer = getDefaultRenderer();

                    }
                    Component comp = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, index);
                    size.width = Math.max(comp.getPreferredSize().width, size.width);
                    size.height += table.getRowHeight(index);

                }

            }
        }
        return size;

    }

    /**
     * Overridden to avoid propagating a invalidate up the tree when the
     * cell renderer child is configured.
     */
    @Override
    public void invalidate()
    {
    }

    /**
     * If the specified component is already a child of this then we don't
     * bother doing anything - stacking order doesn't matter for cell
     * renderer components (CellRendererPane doesn't paint anyway).
     */
    @Override
    protected void addImpl(Component x, Object constraints, int index)
    {
        if (x.getParent() == this)
        {
        } else
        {
            super.addImpl(x, constraints, index);
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {

    Graphics2D g2d = (Graphics2D) g.create();

        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());

        JTable table = getTable();
        if (table != null)
        {

            int width = getWidth();

            TableColumnModel model = table.getColumnModel();
            if (model != null)
            {

                for (int index = 0; index < model.getColumnCount(); index++)
                {

                    TableColumn column = model.getColumn(index);
                    TableCellRenderer renderer = column.getHeaderRenderer();
                    if (renderer == null)
                    {

                        renderer = getDefaultRenderer();

                    }

                    boolean selected = table.getSelectedRow() == index;

                    Component comp = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), selected, false, 0, index);

                    add(comp);
                    comp.validate();

                    int height = table.getRowHeight(index) - 1;
                    comp.setBounds(0, 0, width, height);
                    comp.paint(g2d);
                    comp.setBounds(-width, -height, 0, 0);

                    g2d.setColor(table.getGridColor());
                    g2d.drawLine(0, height, width, height);

                    g2d.translate(0, height + 1);

                }

            }
        }
        g2d.dispose();
        removeAll();
    }

}

所以,我想为上述目的填充这个2D数组。

有任何想法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情:

int[][] values = { { 1, 2 }, { 3, 4 } };

尝试使用字符串值。