在JTable中使用自定义渲染器时出现问题

时间:2012-06-21 16:53:56

标签: java swing

以下是我的单元格渲染器和编辑器:

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中正确显示,但是当我离开此列并单击文本字段时,文本正在消失,文本字段为空白。我的期望是,直到我通过单击浏览按钮更改文本,应显示相同的文本。

1 个答案:

答案 0 :(得分:0)

没有actionListeners或eventListeners。在focusGained和FocusLost时你需要它们。使用java文档,添加并实现适当的侦听器