[英]How do I check which Swing component is being passed as a parameter?
I have a method here (below) that, in itself, works very well. 我在下面有一个方法,它本身可以很好地工作。 The problem is I want to change it's behavior based on which JTable is passed through the parameter.
问题是我想根据通过参数传递的JTable更改其行为。
For example, right now I have two JTables, table1 and table 2. I want to set up an if statement that says if (table == table1){...} else if (table == table2){...} 例如,现在我有两个JTable,分别是table1和table2。我想建立一条if语句,它表示if(table == table1){...} else if(table == table2){...}
The problem is no matter what I try (comparing hash codes, comparing the e.source to the component, the source hashcode to the table1.hashcode() etc.) I can't get it to register when the table that is passed is in fact table1. 问题是无论我如何尝试(比较哈希码,将e.source与组件进行比较,将源哈希码与table1.hashcode()等),当传递的表被保存时,我都无法注册它实际上是表1。
Is there a way to compare components in this way? 有没有办法以这种方式比较组件? (Again, confirming which JTable 'table' (the JTable passed in the parameters) is; 'table1' or 'table2'.
(再次确认哪个JTable'表'(在参数中传递的JTable)是'table1'或'table2'。
Here's my code: 这是我的代码:
public TableCellEditor myCellEditor = new DefaultCellEditor(new JTextField()) {
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
delegate.setValue((editorComponent instanceof JTextField) ? null : value);
myCellEditor.addCellEditorListener(
new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
}
public void editingStopped(ChangeEvent e) {
//if (table == table1){...}
}
});
return editorComponent;
}
You need to know on what table did the even fired? 您需要知道什至在哪张桌子上开了火? Then use
e.getSource();
然后使用
e.getSource();
If you want to see if tableX and tableY are similar then you need to write a Comparator . 如果要查看tableX和tableY是否相似,则需要编写Comparator 。 This example may help a bit.
这个例子可能会有所帮助。 Generally Comparators are handy and quite easy to implement.
通常,比较器非常方便并且易于实现。
Try to avoid the 尽量避免
new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
}
public void editingStopped(ChangeEvent e) {
//if (table == table1){...}
}
});
Instead create 2 CellEditors
and assign your listener to each.. 而是创建2个
CellEditors
并将您的侦听器分配给每个。
myCellEditor1.addCellEditorListener(listener);
myCellEditor2.addCellEditorListener(listener);
//Now by accessing e.getSource you know which celleditor fired!
CellEditorListener listener= new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
}
public void editingStopped(ChangeEvent e) {
if (e.getSource()==myCellEditor1)
else if (e.getSource()==myCellEditor2)
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.