Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:28:06+00:00 2026-06-13T18:28:06+00:00

I have just started working with JTable . This is my table example. The

  • 0

enter image description here

I have just started working with JTable. 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?

Can any one help me please?

The code for the sample table is:

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 < columnNames.length; col++) {
        dataModel.addColumn(columnNames[col]);
    }
    for (int row = 0; row < 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< table.getRowCount();row++){
          for (int col=0;col<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;

        }
  }
} 

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:28:07+00:00Added an answer on June 13, 2026 at 6:28 pm

    I hope my interpretation of the question is correct. To display a row header you need to create a new cell renderer and set it on the required column. For example, here is a basic renderer that mocks a table header:

    static class RowHeaderRenderer extends DefaultTableCellRenderer {
        public RowHeaderRenderer() {
            setHorizontalAlignment(JLabel.CENTER);
        }
    
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            if (table != null) {
                JTableHeader header = table.getTableHeader();
    
                if (header != null) {
                    setForeground(header.getForeground());
                    setBackground(header.getBackground());
                    setFont(header.getFont());
                }
            }
    
            if (isSelected) {
                setFont(getFont().deriveFont(Font.BOLD));
            }
    
            setValue(value);
            return this;
        }
    }
    

    To set it up you can do the following:

    table.getColumnModel().getColumn(0).setCellRenderer(new RowHeaderRenderer());
    

    Here is how it looks like based on the posted code:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started working on localization of this application. Starting with three tab
I have just started working on a wiki where I want registered users to
i have just started using visual studio 2008.I am working on c#. I want
I have just started working on a reusable library. This library is going to
I have just started working in jQuery and ASP.NET MVC. I want the same
I just have started working with codeIgniter and want to know how things in
I have just started leaning and working on xquery with java. I have a
I just started working with Android and I have a problem with my map.
I've just started working with Propel and I love it, but I have a
I have started working on a simple XML pull-parser, and as I've just defuzzed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.