title: Creating row headers for JTable at run time
body: <p><img src="https://i.stack.imgur.com/NFE6T.png" alt="enter image description here"></p>
  
  <p>I have just started working with <code>JTable</code>. This is my table example. The add row button adds rows to the table. I want to create row headers for this table. How can I achieve this? </p>
  
  <p>Can any one help me please? </p>
  
  <p>The code for the sample table is:</p>
  
  <pre><code>package test;
  
  import javax.swing.*;
  
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;
  
  import javax.swing.table.*;
  
  import test.InsertRows.CellEditor;
  
  public class SampleTable extends JFrame {
  
  JTable table;
  JPanel panel;
  DefaultTableModel dataModel;
  
  public SampleTable () {
      super("My Table Example");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      onInit();
  } 
  
  void onInit()
  {
      JButton b=new JButton("Add Row");
      b.addActionListener(new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent e)
          {
              // TODO Auto-generated method stub
              insertNewRow();
  
          }
  
  
      });
  
      panel = new JPanel(new BorderLayout());
  
      String[] columnNames = {
              "Name",
              "OID",
              "Index",
              "Value",
      };
      Object[][] data = {
              {"sysLocation","1.3.6.1.2.1.1.6","0",""},
                  {"sysContact","1.3.6.1.2.1.1.4","0",""},
                      {"sysDescr","1.3.6.1.2.1.1.1","0",""}
  
      };
  
      dataModel = new DefaultTableModel();
      for (int col = 0; col &lt; columnNames.length; col++) {
          dataModel.addColumn(columnNames[col]);
      }
      for (int row = 0; row &lt; 3; row++) {
          dataModel.addRow(data[row]);
      }
  
      table = new JTable(dataModel);
      table.setDefaultEditor(Object.class, new CellEditor(this, table));
      table.setPreferredScrollableViewportSize(new Dimension(600, 120));
      table.setFillsViewportHeight(true);
  
      //Create the scroll pane and add the table to it.
      JScrollPane scrollPane = new JScrollPane(table);
  
      //Add the scroll pane to this panel.
      panel.add(scrollPane,BorderLayout.CENTER);
      panel.add(b,BorderLayout.SOUTH);
      panel.setOpaque(true); //content panes must be opaque
      setContentPane(panel);
  
      //Display the window.
      pack();
      setVisible(true);
  }
  public static void main(String[] args) {
      SampleTable myFrame = new SampleTable();
  } 
  private void insertNewRow()
  {
  
      if(vaidCheck(table)){
          dataModel.insertRow(table.getRowCount(),
                  new Object[] {"", "", "", ""}
                  );
          dataModel.fireTableRowsInserted(
                  dataModel.getRowCount(),
                  dataModel.getRowCount()
          );
      }
      else{
          JOptionPane.showMessageDialog(null,"Field is empty");                                                                            
      }
  
  }
  public boolean vaidCheck(JTable table)
  {
        if(table.getCellEditor()!=null){
            table.getCellEditor().stopCellEditing();
        }
        for(int row=0;row&lt; table.getRowCount();row++){
            for (int col=0;col&lt;table.getColumnCount();col++){
                String om=table.getValueAt(row,col).toString();
                if(om.trim().length()==0){
                    return false;
                }
            }
        }
        return true;
   }
  
   public class CellEditor extends DefaultCellEditor
    {
  
          /**
           * 
           */
          private static final long serialVersionUID = 1L;
          private JTable m_Table = null;
          public CellEditor(JFrame parentFrame, JTable table) { 
              super(new JTextField());
              super.setClickCountToStart(1);
              m_Table = table;
          }
          @Override
          public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, int column)
          {
              if(column == 0){
                   Object[] objectArray = {"Nikhil","Nijil"};
                      JComboBox comboBox = new JComboBox(objectArray);
                      comboBox.setEditable(true);
                      comboBox.setSelectedItem(value);
  
                      ItemListener itemListener = new ItemListener() {
                          public void itemStateChanged(ItemEvent e) {
                              if(e.getStateChange() == ItemEvent.SELECTED) {
                                  if(null != m_Table.getCellEditor()){ 
                                      m_Table.getCellEditor().stopCellEditing();
                                  }
  
                                  m_Table.setValueAt(e.getItem(), row, 0);
                              }
                          }
                      };
                      comboBox.addItemListener(itemListener);
                      return comboBox;
              }
              if(column == 2){
  
              }
              JTextField textField = (JTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
              return textField;
  
          }
    }
  } 
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Jb7GV.png" alt="enter image description here"></p>