Function returning the JTable by taking parameter as a Component's Name

北战南征 提交于 2019-12-11 03:45:32

问题


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.


回答1:


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.

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();
            }
        });
    }
}



回答2:


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




回答3:


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;
       }
  }  


来源:https://stackoverflow.com/questions/16607311/function-returning-the-jtable-by-taking-parameter-as-a-components-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!