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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:56:42+00:00 2026-06-03T05:56:42+00:00

MyTableCellEditor needs to add overwrite editing (like an Excel cell) and data entry rules

  • 0

MyTableCellEditor needs to add overwrite editing (like an Excel cell) and data entry rules (parseDouble) to JTable.
Why does the cell erase the user entered value when the user clicks into another cell?
IOW: the value that is entered into a cell is visible until another cell is clicked, then it is gone.
Stepping through the program, getTableCellEditorComponent is called when when a cell value is edited by the user and getCellEditorValue is called when cell editing is completed.

public class MyTableFrame extends javax.swing.JFrame 
    implements TableModelListener {
    private static TableColumn column2;

    public MyTableFrame() {
        initComponents();
        ...
        column2 = jTable.getColumnModel().getColumn(2);
        column2.setCellEditor(new MyTableCellEditor());
        jTable.getModel().addTableModelListener(this);
    }
}

public class MyTableCellEditor extends AbstractCellEditor implements
        TableCellEditor {

    // Component to handle the editing of a cell value
    private JTextField component = new JTextField();
    // Return value
    private Object value;
    private DefaultTableModel model;
    private int rowIndex;
    private int columnIndex;

    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int columnIndex) {
        model = (DefaultTableModel) table.getModel();
        this.value = value;
        this.rowIndex = rowIndex;
        this.columnIndex = columnIndex;
        component.setText("");
        return component;
    }

    public Object getCellEditorValue() {
        try {
            return Double.parseDouble(model.getValueAt(rowIndex, columnIndex)
                .toString());
        } catch (Exception ex) {
            return value;
        }
    }
}
  • 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-03T05:56:44+00:00Added an answer on June 3, 2026 at 5:56 am

    I guess the entry is made into the model after the editing is complete. So getting the value from the model in getCellEditorValue() will give you the value before the edit.

    Instead you should use component.getText() to fetch the value.

     return Double.parseDouble(component.getText())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The intention of MyTableCellEditor is to make a JTable cell behave like an Excel

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.