I am Using tablecellrendere for a table to add a JPanel at the fourth column.... It is adding to all ..... But i want to add a textfield at first row only....

Recommended Answers

In your getTableCellRendererComponent method just test the row parameter to see if this is the first row, then return a JPanel or a JLabel as appropriate

Jump to Post

I have no idea how that fragment fits into your program, but anyway, I don't know how to make it any clearer than I did in my first post.

Jump to Post

All 6 Replies

In your getTableCellRendererComponent method just test the row parameter to see if this is the first row, then return a JPanel or a JLabel as appropriate

JPanel comp= (JPanel) jTable.getCellRenderer(0, 4).getTableCellRendererComponent(jTable, jTable.getValueAt(0, 4), false, false, 0, 4) ;JTextField txt=new JTextField("santhosh");comp.add(txt);
but it is adding to all the rows

I have no idea how that fragment fits into your program, but anyway, I don't know how to make it any clearer than I did in my first post.

  • don't put JComponents to the XxxTableModel, there is stored only referrence, prepared value for Renderer/initial value for Editor

  • XxxCellRenderer isn't somehow restricted, but you have to accepting that everything here is only about static painting, virtual illusion based on methods implemented in APIs

  • there are three ways of How to use Renderer (as obviously everything is very well, quite good described in Oracle tutorial), see Concepts: Editors and Renderers for working examples

    1. prepareRenderer

    2. TableCellRenderer

    3. TableCellRenderer applied to column, quite easy to concrete cell, then all cells in one column can has been different (valid for editor too)

  • your two posts, including code snipped (uncompilable) talking about 4th. of ways (not attach, nor intented as) quite unknow for me

  • before anything to read next chapter Using Custom Renderers, if doesn't help you, then post an SSCCE, short, runnable, compilable with hardcoded value for JTable, (or its XxxTableModel), otherwise everything here will be about guessing, maybe (better variant) will shots to the dark

  • XxxRenderer is initialized from all mouse & keys events over visible cells in JViewport, plus internal methods implemented in API

package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

 * @author Admin
public class NewClass {

    public static void main(String[] args) {
        // TODO code application logic here

        JFrame frm = new JFrame("Simple");
        JMenuBar mBar = new JMenuBar();
        JPanel panel = new JPanel();
        JMenu menu = new JMenu("File");
        JMenuItem mItem = new JMenuItem("Connects");
        menu.setPreferredSize(new Dimension(50, 20));
        mBar.setToolTipText("Menu Bar");

        JTable jTable = new JTable();
        DefaultTableModel dt = new DefaultTableModel(200, 3) {
            public boolean isCellEditable(int row, int col) {
                return false;

        for (int r = 0; r < dt.getRowCount(); r++) {

            dt.setValueAt("Simple", r, 0);
            dt.setValueAt("simple", r, 1);
            dt.setValueAt("India", r, 2);



        Font f = new Font(null, 12, 16);


        JScrollPane spane = new JScrollPane(jTable);

        spane.setPreferredSize(new Dimension(690, 290));
        panel.setPreferredSize(new Dimension(200, 200));

        TableColumnModel tcm = jTable.getColumnModel();
        tcm.getColumn(2).setCellRenderer(new PlusMinusCellRenderer());
        Dimension d = new Dimension(700, 350);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        frm.setIconImage(new ImageIcon(ClassLoader.getSystemResource("images\\bing.ico")).getImage());
        frm.setLocation(dim.width / 2 - frm.getWidth() / 2, dim.height / 2 - frm.getHeight() / 2);

class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        return this;

I need to add a text box to the jpanel in the first row when a button is clicked

  • better my +1 for posting an SSCCE (-1mio for frm.setIconImage...)

  • now coming my questions ---> whats potential logics, can/will be for PlusMinusCellRenderer, is about (can increasing whatever or not, current cell or column, or not, description is not clear for me, described like as by chief of partisans)

    a) (talking me) one column with JSpinner (SpinnerNumberModel) as TableCellRenderer and Editor too

    b) two columns contains JButton, both TableCellRenderer and Editor too

    c) something (JButton, JSpinner, JComboBox) will be placed in JPanel (is empty must contains some JComponents, properly laid to return, output fomr your cell renderer is correct) to one column as TableCellRenderer and Editor too

  • I see that you override isCellEditable to false, but havent any impact, nothing to do with my previous points

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.