Mass produce JTables
Asked Answered
C

2

0

I want to make 25 JTables. I generate the table names by doing:

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

How can I go about using these String names in the array as the new JTable names? i.e.

TableNames[i] = new JTable(model){ ...
Caundra answered 16/5, 2012 at 15:11 Comment(1)
Did you mean Table[i] = new JTable(model){ ...?Uropygium
E
2

Instead of an array, consider using a List<JTable> or List<TableModel>. Pass the name to the table's constructor or factory method. The example below uses the Component's name, but a JComponent's client property may be more versatile.

Update: The revised Java 8 example below illustrates how to add new tables dynamically.

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 https://mcmap.net/q/1478579/-mass-produce-jtables
 */
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();
        });
    }
}
Etiolate answered 16/5, 2012 at 16:53 Comment(2)
I am not looking at using lists though. I just need 25 different JTables, numbered 0-25. Obviously I can make them individually, but that is tedious. With an array of produced names, is there not a simpler way to loop the creation of the tables?Caundra
The example makes 25 different JTable instances, numbered 0-24; each is added to a List<JTable> and a JPanel.Etiolate
S
0
TableNames[i] = new JTable(model)

put inside the for loop i agree previous statement make error try this

for(i=0;i<TableNames.length();i++)
{
new JTable(TableName[i]);
}
Stagecraft answered 16/5, 2012 at 15:17 Comment(1)
You get an Incompatible Types error if you put it inside the loop.Caundra

© 2022 - 2024 — McMap. All rights reserved.