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....

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

Edited 3 Years Ago by JamesCherrill

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

Edited 3 Years Ago by sankubha: forget to add

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

Edited 3 Years Ago by mKorbel

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));
        menu.setForeground(Color.green);
        menu.setBackground(Color.black);
        menu.add(mItem);
        mBar.setForeground(Color.green);
        mBar.setBackground(Color.black);
        mBar.setToolTipText("Menu Bar");
        mBar.setBorderPainted(false);
        mBar.add(menu);
        mItem.setForeground(Color.gray);
        mItem.setBackground(Color.black);

        JTable jTable = new JTable();
        DefaultTableModel dt = new DefaultTableModel(200, 3) {
            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }
        };
        dt.setRowCount(50);
        jTable.setDragEnabled(false);
        jTable.setGridColor(Color.gray);
        jTable.setSelectionMode(1);

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

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

        }

        jTable.setForeground(Color.green);
        jTable.setSelectionBackground(Color.green);
        jTable.setSelectionForeground(Color.black);

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

        jTable.setFont(f);
        jTable.setModel(dt);

        JScrollPane spane = new JScrollPane(jTable);

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

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

        frm.add(spane);
        frm.setJMenuBar(mBar);
        frm.setBackground(Color.BLACK);
        frm.setForeground(Color.GREEN);
        frm.setIconImage(new ImageIcon(ClassLoader.getSystemResource("images\\bing.ico")).getImage());
        frm.setSize(d);
        frm.setLocation(dim.width / 2 - frm.getWidth() / 2, dim.height / 2 - frm.getHeight() / 2);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setResizable(false);
        frm.show();
    }
}

class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {

    @Override
    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

Edited 3 Years Ago by mKorbel

This article has been dead for over six months. Start a new discussion instead.