2 Replies Latest reply: Jun 25, 2012 11:07 AM by Jörg RSS

    Problem with JTable custom cell renderer's

    Murray9654
      Hi My requirement is the the table should contain 2 columns. first column will contain a JCombobox and the second column will contain a JPanel. The combobox contains employee departments. The user will select the employee department from the combobox. The panel that is in the second column contains a textfield and a button. when user clicks on the button based on the department selected in that particular row the employees related to that department will be populated. the user will select the employee and the employee name will be displayed in the textfield which is not editable.

      For this i have written the following renderer and editor for the JTable
      package org.lims.register.gui;
      
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.util.ResourceBundle;
      
      import javax.swing.JButton;
      import javax.swing.JPanel;
      import javax.swing.JTextField;
      
      import org.lims.employee.gui.SelectEmpDialog;
      import org.lims.main.Lims;
      import org.lims.util.Util;
      
      
      public class EmpNamePanel extends JPanel {     
           
           private static final long serialVersionUID = 934032676749762710L;
           private ResourceBundle resources=Util.getResources();
           
           private JTextField empNameTF;
           private JButton browseB;
           public static String departmrnt;
           private EmpNamePanel enp;
           
           public EmpNamePanel(){
                super(null);
                enp=this;
                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){
                          SelectEmpDialog sed=new SelectEmpDialog(Lims.getFrame(),
                                    resources.getString("register.dialog.selectemp.title"),
                                    true,enp);
                          
                     }
                });
                browseB.setBounds(150,0, 100, 30);
                add(browseB);
           }
      
           
           /**
            * @return the empNameTF
            */
           public JTextField getEmpNameTF() {
                return empNameTF;
           }
      
           /**
            * @return the browseB
            */
           public JButton getBrowseB() {
                return browseB;
           }
           
           
      }
      The above is the JPanel component that i have written which is common for both Editor and renderer.
      package org.lims.register.gui.model;
      
      import java.awt.Component;
      
      import javax.swing.AbstractCellEditor;
      import javax.swing.JTable;
      import javax.swing.table.DefaultTableModel;
      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;
           
           public EmpNamePanelEditor(){
                enp=new EmpNamePanel();
                
           }
      
           /* (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) {
                
                DefaultTableModel dtm=(DefaultTableModel)table.getModel();
                String dept=(String)dtm.getValueAt(row, 0);
                EmpNamePanel.departmrnt=dept;
                return enp;
           }
           
           
      
      }
      The above is the cell editor that i have written for JTable.
      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());
              }
                
                getEmpNameTF().setText((String)value);
                return this;
           }
      }
      The above is the cell renderer compomponent for JTable.

      The Problem is in the first row i have chosen a employee from the popup values but when i add the second row to the table the same employee is appearing in the second row as well. The requirement is i have to choose a different employee based on the department selected in the second row. if i select a different employee in the second row the same employee name is appearing in the first row's Jpanel textfield. I am not able to understand what the problem is. Kindly help me in this issue.

      Edited by: Muralidhar on Jun 24, 2012 9:51 AM