批量生产JTable

时间:2012-05-16 15:11:54

标签: java swing jtable names tablemodel

我想制作25个JTable。我通过执行以下方式生成表名:

for(int i=0; i < 26; i++)
{
    TableNames[i] = "Table" + i + "";
    ...

如何在数组中使用这些String名称作为新的JTable名称?即

TableNames[i] = new JTable(model){ ...

2 个答案:

答案 0 :(得分:2)

请考虑使用List<JTable>List<TableModel>,而不是数组。将名称传递给表的构造函数或工厂方法。下面的示例使用Component的名称,但JComponent的客户端属性可能更通用。

更新:下面修订的Java 8示例说明了如何动态添加新表。

image

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;

/**
 * @see http://stackoverflow.com/a/10623134/230513
 */
public class NamedTableTest extends JPanel {

    private static final int N = 25;
    private final List<JTable> list = new ArrayList<>();

    public NamedTableTest() {
        super(new GridLayout(0, 1));
        for (int i = 0; i < N; i++) {
            list.add(new NamedTable("Table " + String.valueOf(i)));
            this.add(list.get(i));
        }
    }

    private static class NamedTable extends JTable {

        public NamedTable(final String name) {
            super(new DefaultTableModel(1, 1) {

                @Override
                public Object getValueAt(int row, int col) {
                    return name + ", " + row + ", " + col;
                }
            });
            this.setName(name);
        }

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                System.out.println(NamedTable.this.getName());
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("NamedTable");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JScrollPane jsp = new JScrollPane(this);
        f.add(jsp);
        f.add(new JButton(new AbstractAction("Add") {

            @Override
            public void actionPerformed(ActionEvent e) {
                list.add(new NamedTable("Table " + String.valueOf(list.size())));
                NamedTableTest.this.add(list.get(list.size() - 1));
                NamedTableTest.this.validate();
                EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        JScrollBar sb = jsp.getVerticalScrollBar();
                        sb.setValue(sb.getMaximum());
                    }
                });
            }
        }), BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new NamedTableTest().display();
        });
    }
}

答案 1 :(得分:0)

TableNames[i] = new JTable(model)

放入for循环 我同意以前的陈述有误 试试这个

for(i=0;i<TableNames.length();i++)
{
new JTable(TableName[i]);
}