-1

I want to create tables dynamically depending on Input. If input is 5 there will be 5 JTables and then I should be able to identify these 5 tables uniquely so that i can work on these 5 tables individually afterwards.

I am doing

table=new Jtable()

,but I want

table1=new Jtable(),table2=new Jtable(),table3=new Jtable()

and so on thus generate automatically.I upper limit of input is 18.

How do i do this?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Possible duplicate of [Mass produce JTables](http://stackoverflow.com/questions/10621470/mass-produce-jtables) – trashgod Mar 14 '14 at 11:09

2 Answers2

0

Try something like this (untested):

ArrayList<JTable> tables = new ArrayList<JTables>();
for(int i=0; i<5; i++) {
  tables.add(new JTabel());
}
BetaRide
  • 16,207
  • 29
  • 99
  • 177
0

What about this:

ArrayList<JTable> tables = new ArrayList<JTable>();
for (int i = 0; i < 18; i++) {
    tables.add(new JTable());
}

private JComponent createGUI() {
    JPanel pnl = new JPanel();
    for (JTable jTable : tables) {
        pnl.add(jTable);
    }
    return pnl;
}

Update Example:

public class Display extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Display().setVisible(true);
            }
        });
    }

    private ArrayList<JTable> tables = new ArrayList<JTable>(){{
        for (int i = 0; i < 18; i++) {
            add(new JTable(new DefaultTableModel(3,3)));
        }
    }};

    public Display() {
        super("Title");
        this.setSize(832, 594);
        this.setLocationRelativeTo(null); // to center frame on screen
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel content = new JPanel();
        content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
        for (JTable table : tables) {
            content.add( createGUI(table) );
        }

        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(content, BorderLayout.CENTER);
    }

    private JPanel createGUI(JTable table) {
        JPanel pnl = new JPanel(new BorderLayout());
        pnl.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        pnl.add(table.getTableHeader(), BorderLayout.NORTH);
        pnl.add(table, BorderLayout.CENTER);
        return pnl;
    }

}
oliholz
  • 7,447
  • 2
  • 43
  • 82
  • @ oliholz.Thanks for the reply.I am showing you the code,i am going wrong somewhere.The code is as follows: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 832, 594); contentPane = new JPanel(); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); setContentPane(contentPane); contentPane.setLayout(null); – user3215098 Mar 21 '14 at 07:56
  • .I am very thankful for your help.The solution is a close one.Now let me get deeper into my problem maybe then you will understand what i really want.I need 8 tables named as Retailer1,Retailer2 and so on,4 Wholesaler table as Wholesaler1,Wholesaler2,2 distributor table and 1 factory table.Now each table will interact with each other in order to fill the cells.I will give some inputs directly into the cells and for some there will be calculations and the cells will be filled accordingly.Similar to Microsoft Excel.So how can i do that?The tables should have unique names. – user3215098 Mar 26 '14 at 11:58