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;
}
这就是我想要的功能;我想出了一个创建一个具有私有JTable表和私有String名称的新类组件的解决方案,但是在按名称搜索JTable时仍然会出现问题。
答案 0 :(得分:2)
结合@Alican Ozgoren,@ mKorbel和answer的建议,您可以构建Map<String, NamedModel>
,以便按名称快速访问TableModel
。
public TableModel getTable(String name) {
return map.get(name);
}
以下示例替换单个JTable
的模型,而另一个example显示多个表。
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();
}
});
}
}
答案 1 :(得分:1)
这就是我想要的功能;我想出了一个解决方案 创建一个具有私有JTable表的新组件 私有字符串名称,但在搜索JTable时仍然会遇到问题 按名称。
同意,非常好的设计,让我在这种情况下... ...
public JTable getTable(String Component_name)
可能是
public JTable getTable(myTableModel, arrays implemented in JTables API)
不要声明(重载)组件名称,这可能是内部方法返回JTable,但我仍然找不到原因
答案 2 :(得分:0)
成功回答!!
我创造了 private ArrayList List = new ArrayList();
和我的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
}
其中
public ArrayList<JTable> getTable()
{
return List;
}
public void AddT(JTable tl)
{
if(tl.getName()!=null)
{getTable().add(tl);}
else return;
}
现在我有一个所有JTable的列表,现在我可以通过名称实现获取如下::
JTable one= new JTable();
JTable two= new JTable();
ComboBoxModel combo = comboBox.getModel();
for(JPanel t: getTable())
{
if(combo.getSelectedItem().equals(t.getName()))
{
one=t;
}
}