2
public JTable getTable(String Component_name)
    {
        JTable table=new JTable();
    //in this function i want to search all the JTables that have been created on runtime! 
    //and then i want to return one JTable by the name "Component_Name"//

        return table;
    }

This is how i want my function to be ; i came up with a solution of creating a new class of Components having private JTable table and private String name, But still problem comes up at searching JTables by name.

Asd
  • 103
  • 2
  • 12

3 Answers3

2

Combining the suggestions of @Alican Ozgoren, @mKorbel and this answer, you can construct a Map<String, NamedModel> for rapid access to a TableModel by name.

public TableModel getTable(String name) {
    return map.get(name);
}

The example below replaces the model of a single JTable, while the other example displays multiple tables.

Test image

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 * @see https://stackoverflow.com/a/16611982/230513
 * @see https://stackoverflow.com/a/10623134/230513
 */
public class Test {

    private static final int N = 25;
    private DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
    private JComboBox combo = new JComboBox(dcbm);
    private JTable table = new JTable(1, 1);
    private Map<String, NamedModel> map = new HashMap<String, NamedModel>();

    public TableModel getTable(String name) {
        return map.get(name);
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 0; i < N; i++) {
            String name = "Table " + String.valueOf(i);
            NamedModel model = new NamedModel(name);
            map.put(name, model);
            dcbm.addElement(model);
        }
        combo.setSelectedIndex(-1);
        combo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TableModel model = (TableModel) combo.getSelectedItem();
                table.setModel(model);
            }
        });
        f.add(combo, BorderLayout.NORTH);
        f.add(table);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class NamedModel extends DefaultTableModel {

        private String name;

        public NamedModel(String name) {
            super(1, 1);
            this.name = name;
        }

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

        @Override
        public String toString() {
            return name;
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

This is how i want my function to be ; i came up with a solution of creating a new class of Components having private JTable table and private String name, But still problem comes up at searching JTables by name.

agree, very good desing, make me sence in the case ...

public JTable getTable(String Component_name)

could be

public JTable getTable(myTableModel, arrays implemented in JTables API)

not to declaring (overloading there) Components name, this could be inside methods returns JTable, still I can't found reason for that

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • how do i search then ? can you please reform my above function ? – Asd May 17 '13 at 11:20
  • still I miss this requirement, this JTable is always accesisible in current components hierarchy from current JVM, whats reson to hold its name, for why reason, I'm hope that you didn't create a bunch of JTable on runtime, that one to replace the another one, then answer is about re_use one JTable, with only one TableModel – mKorbel May 17 '13 at 11:23
  • i have an interface that onclick "Create" Button puts JTables on the JFrame; and on mentioning two JTable's name it draws line between them – Asd May 17 '13 at 11:29
  • 1
    then reason posting and [SSCCE](http://sscce.org/), [can be based on](http://stackoverflow.com/search?q=user%3A714968+[jtable]+TableSelectionGood) – mKorbel May 17 '13 at 11:37
0

Sucessful answer !!

I created private ArrayList List=new ArrayList();

and in my ActionListener::

 AddTableMenuItem.addActionListener(new ActionListener() 
    {
                //This method will be called whenever you click the button. 
            public void actionPerformed(ActionEvent e){
               Table table=new Table();

              JTable.setName("Table"+count);

              AddT(table);                               // Table List 
    }

where

  public ArrayList<JTable> getTable()
  {
        return List;
  }

 public void AddT(JTable tl)
 {
     if(tl.getName()!=null)
     {getTable().add(tl);}
     else return;
 }

Now i have a List of all JTables that now i can fetch by names implementation as follows::

JTable one= new JTable();
  JTable two= new JTable();
  ComboBoxModel combo = comboBox.getModel();

 for(JPanel t: getTable())
  {  
       if(combo.getSelectedItem().equals(t.getName()))
       {
          one=t;
       }
  }  
Asd
  • 103
  • 2
  • 12
  • Consider `List list = new ArrayList()`; also, use conventional Java variable names; code to the `List` interface; use generic types consistently. – trashgod May 22 '13 at 08:53