javajtabledynamic-controls

Dyanmic JTable creation, how do I know which table is it?


I have this code that I need to run depending on a number. I thought it was easy and YES it was but then this brick wall hit me...

how do I know which table am I clicking at or which table to put items into???

private void createTablesForBuilding() {
    int buildingcoutns = 3;

    for (int i = 0; i < buildingcoutns; i++) {
        JTable jt = new JTable();

        tables.add(jt);
        tables.get(i).setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String[]{
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }
        ));

        tables.get(i).getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (jt.getSelectedRow() > -1) {
                    System.out.print("what now???");
                }
            }
        });

        JScrollPane js = new JScrollPane();
        js.setViewportView(tables.get(i));

        JPanel jp = new JPanel();
        jp.add(js);

        Buildings_Panel.add(jp);
    }
}

Solution

  • got it!. I created a separate class and assigned a variable inside it where I can put a an ID to my table. this is what I did.

    private void createTablesForBuilding() {
        int buildingcoutns = 5;
    
        for (int i = 0; i < buildingcoutns; i++) {
            myTables tbls = new myTables();
            tbls.setTableID(i);
    
            tables.add(tbls);
    
            JTable thistable = tables.get(i).table;
    
            thistable.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null}
                    },
                    new String[]{
                        "Title 1", "Title 2", "Title 3", "Title 4"
                    }
            ));
    
            tables.get(i).table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (thistable.getSelectedRow() > -1) {
                        System.out.println("tableID is:"+tbls.getTableID());
                    }
                }
            });
    
            Buildings_Panel.add(tables.get(i));
        }
    }
    

    and here is myTables

    public class myTables extends javax.swing.JPanel {
    
    int tableID = 0;
    
    public myTables() {
        initComponents();
    }
    
    public void setTableID(int i){
        tableID = i;
    }
    public int getTableID(){
        return tableID;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
    
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
    
        setLayout(new java.awt.CardLayout());
    
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(table);
    
        add(jScrollPane1, "card2");
    }// </editor-fold>                        
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTable table;
    // End of variables declaration                   
    

    }