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 9097427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:06:48+00:00 2026-06-17T00:06:48+00:00

I am learning from this oracle tutorial that uses TableDialogEditDemo.java example class I wrote

  • 0

I am learning from this oracle tutorial that uses TableDialogEditDemo.java example class

I wrote a custom cell Renderer and Editor for JTable.

I register them to this Oracle TableDialogEditDemo.java class

        ...
        ...
        //Set up renderer and editor for the Favorite Color column.
        table.setDefaultRenderer(Color.class,
                                 new ColorRenderer(true));
        table.setDefaultEditor(Color.class,
                               new ColorEditor());

        TableColumn c = table.getColumnModel().getColumn(2);
        c.setCellRenderer(new CellStringRenderer()); //My custom Renderer
        c.setCellEditor(new CellStringEditor()); // My custom Editor

        //Add the scroll pane to this panel.
        add(scrollPane);
        ...
        ...

(Updated description)
When I click on a cell an input dialogue box pops up and that is OK, and when I type a text and click “OK” the cell in the JTable is updated, but text is not displayed/rendered correctly, I have to click on any other cell to make the text content displayed correctly in the cell.

What is the wrong with my code?.

My Renderer

import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class CellStringRenderer extends JLabel implements TableCellRenderer
{

    public CellStringRenderer()
    {
        this.setOpaque(true);
    }


    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
       String stringValue = (String) value;
       this.setText(stringValue);
       return this;
    }

}

My Editor (Updated)

import java.awt.Component;
import javax.swing.*;
import javax.swing.table.TableCellEditor;


public class CellStringEditor extends AbstractCellEditor
        implements TableCellEditor
{

    String input;

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
        if (isSelected)
        {
            JOptionPane dialog = new JOptionPane();
            input = dialog.showInputDialog(null, "new value");
            return dialog;   
        }
        return null;
    }

    @Override
    public Object getCellEditorValue()
    {
        return input;
    }
}
  • 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-17T00:06:49+00:00Added an answer on June 17, 2026 at 12:06 am

    I am The author of this question, and I solved it. I will provide the solution so that others can get help from it.

    I was trying to write a custom renderer and a custom editor to use with JTable.
    The renderer simply uses JLabel to display data. It is already the standard component for JTable.
    The editor is a dialogue box that appears when clicking on the cell that I want to edit.

    Here is the solution:

    The classes that will remain unchanged are the three classes from Oracle + my custom renderer class

    1.TableDialogEditDemo.java.

    2.ColorEditor.java

    3.ColorRenderer.java

    4.CellStringRenderer class provided above in the question body (my class)

    The class that will be updated is the custom editor class (I changed its name from “CellStringEditor” to “DialogStringEditor”

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.AbstractCellEditor;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JTable;
    import javax.swing.table.TableCellEditor;
    
    public class DialogStringEditor extends AbstractCellEditor
            implements TableCellEditor,
            ActionListener
    {
    
        String newInput;
        String oldValue;
        JButton button;
        static final String EDIT = "edit";
    
        public DialogStringEditor()
        {
            button = new JButton();
            button.setBackground(Color.WHITE);
            button.setActionCommand(EDIT);
            button.addActionListener(this);
            button.setBorderPainted(false);
        }
    
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (EDIT.equals(e.getActionCommand()))
            {
                newInput = JOptionPane.showInputDialog("Edit", oldValue);
                if (newInput == null)
                {
                    newInput = oldValue;
                }
                fireEditingStopped();
            }
        }
    
        @Override
        public Object getCellEditorValue()
        {
            return newInput;
        }
    
        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value,
                boolean isSelected,
                int row,
                int column)
        {
            newInput = (String) value;
            oldValue = (String) value;
            return button;
        }
    }
    

    This will work ok

    You can also make a little update for the renderer class “CellStringRenderer” to control how are selction and unselection colors of the cell appear.

    update this method:

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            String stringValue = (String) value;
            this.setText(stringValue);
    
            if (isSelected)
            {
                this.setBackground(table.getSelectionBackground());
                this.setForeground(table.getSelectionForeground());
            } else
            {
                this.setBackground(table.getBackground());
                this.setForeground(table.getForeground());
            }
    
            return this;
        }
    }
    

    Regards.

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

Sidebar

Related Questions

I'm learning Java and OOPS from oracle's website. I'm doing this exercise . I'm
I am learning JPA from this tutorial . I have some confusions in understanding
I am learning GWT, so I am following this tutorial from Google GWT https://developers.google.com/web-toolkit/tools/gwtdesigner/tutorials/stockwatcher
Coming from SQL Server, I am learning some Oracle syntax. This is my table
I'm learning from this tutotrials: http://en.wikibooks.org/wiki/Category:OpenGL_Programming http://www.opengl-tutorial.org/ I have modified the 7.th lesson from
I have a question - I am learning OpenGL ES 2.0 from this tutorial
I'm learning OpenGL from this tutorial: http://openglbook.com/the-book/ . I have problem with shaders. I'm
I am learning the DBConfiguration class from this site. public class MyConfiguration : DbConfiguration
I got the idea of learning how referencing works in Ruby from this tutorial
I am learning EF.I have some classes of model and class that inherited from

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.