0

The following are my cell renderer and editor:

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

I am using this renderer and editor. Everything is working fine. When I click on the browse button the text is being displayed in the textfield and when I focus on other column in the table the text is displayed properly in the textField but when I move away from this column and when I click back on textfield the text is disappearing and the text field is being blank. My expectation is until I change the text by clicking the browse button the same text should be displayed.

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
Murray9654
  • 27
  • 6
  • 3
    Please edit your question to include an [sscce](http://sscce.org/) like [this one](http://stackoverflow.com/a/10067560/230513). – trashgod Jun 21 '12 at 17:00
  • 1
    and Renderer is for JComboBox, JList, JTable or JTable, I miss those in your code ... – mKorbel Jun 21 '12 at 17:13

1 Answers1

0

there are no actionListeners or eventListeners. you need them when focusGained and FocusLost. Use the java docs and, add and implement the appropriate listeners

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89