以下是我的单元格渲染器和编辑器:
package org.lims.register.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class EmpNamePanel extends JPanel {
private static final long serialVersionUID = 934032676749762710L;
private JTextField empNameTF;
private JButton browseB;
public EmpNamePanel(){
super(null);
empNameTF=new JTextField();
empNameTF.setEditable(false);
empNameTF.setBounds(0, 0, 150, 30);
add(empNameTF);
browseB=new JButton("Browse");
browseB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
empNameTF.setText("Murali");
}
});
browseB.setBounds(150,0, 100, 30);
add(browseB);
}
/**
* @return the empNameTF
*/
public JTextField getEmpNameTF() {
return empNameTF;
}
/**
* @return the browseB
*/
public JButton getBrowseB() {
return browseB;
}
}
package org.lims.register.gui.model;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import org.lims.register.gui.EmpNamePanel;
public class EmpNamePanelRenderer extends EmpNamePanel implements TableCellRenderer{
private static final long serialVersionUID = 5637984468556275113L;
/* (non-Javadoc)
* @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
System.out.println("value ;"+value);
getEmpNameTF().setText((String)value);
return this;
}
}
package org.lims.register.gui.model;
import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import org.lims.register.gui.EmpNamePanel;
public class EmpNamePanelEditor extends AbstractCellEditor implements TableCellEditor{
private static final long serialVersionUID = -5575645761840609401L;
private EmpNamePanel enp;
/* (non-Javadoc)
* @see javax.swing.CellEditor#getCellEditorValue()
*/
@Override
public Object getCellEditorValue() {
String value=enp.getEmpNameTF().getText();
return value;
}
/* (non-Javadoc)
* @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
*/
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
enp=new EmpNamePanel();
return enp;
}
}
我正在使用这个渲染器和编辑器。一切正常。当我单击浏览按钮时,文本显示在文本字段中,当我专注于表格中的其他列时,文本在 textField 中正确显示,但是当我离开此列并且当我返回文本字段时,文本正在消失,文本字段为空白。我的期望是,在我通过单击浏览按钮更改文本之前,应该显示相同的文本。