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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:33:23+00:00 2026-05-30T17:33:23+00:00

I have an application which uses JTables to display data, and the cells are

  • 0

I have an application which uses JTables to display data, and the cells are editable so that the user can change the data. The user can also revert the changes, or load data from an external source. However, if the user reverts/loads the data using a keyboard shortcut, so that mouse focus is not taken away from the table, the currently selected cell does not get reverted. In fact, after the refresh, the cell goes into edit mode! Then when the user navigates away from this cell, a change event is triggered, so the old value gets committed back to the data store.

I have a short example program that demonstrates this problem. It shows a table in which every cell displays the value 0. There is also a File menu with a single menu item called “Increment”, which has a keyboard shortcut of Ctrl-I. Each time the Increment command is invoked, it increments the number displayed in all of the cells. To see the problem, do the following:

  1. Compile and run the program
  2. Hit Ctrl-I a bunch of times to invoke the Increment command. Notice that the cell values increment each time.
  3. Click a cell.
  4. Hit Ctrl-I a bunch of times to invoke the Increment command. Notice that all cell values increment except the one that is selected.

I have tried various methods to remove the selection from the table before refreshing it, to no avail. Neither

table.editCellAt(-1, -1);

nor

table.getSelectionModel().clearSelection();

worked, for example.

Here is the sample program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableBug extends JFrame {
    private static final int ROW_COUNT = 3;
    private static final int COL_COUNT = 3;

    private int mDataValue = 0;
    private DefaultTableModel mTableModel;

    // Constructor
    public TableBug() {
        setTitle("TableBug");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // Create table model and table
        mTableModel = new DefaultTableModel();        

        for (int col = 0; col < COL_COUNT; col++) {
            mTableModel.addColumn("Value");
        }

        JTable table = new JTable(mTableModel);
        setUpTable(table);
        refresh();

        // Create menu bar
        int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

        JMenu fileMenu = new JMenu("File");

        JMenuItem incrementMenuItem = new JMenuItem("Increment");
        incrementMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, keyMask));
        incrementMenuItem.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                doIncrement();
            }
        });

        fileMenu.add(incrementMenuItem);

        JMenuBar mainMenuBar = new JMenuBar();
        mainMenuBar.add(fileMenu);

        // Populate GUI
        setJMenuBar(mainMenuBar);
        add(new JScrollPane(table), BorderLayout.CENTER);

        // Display window
        pack();
        setVisible(true);
    }

    // Configures the table
    private void setUpTable(JTable table) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);

        table.setRowSelectionAllowed(false);
    }

    // Populates the table
    private void refresh() {
        mTableModel.setRowCount(ROW_COUNT);

        for (int col = 0; col < COL_COUNT; col++) {
            for (int row = 0; row < ROW_COUNT; row++) {
                mTableModel.setValueAt(mDataValue, row, col);
            }
        }
    }

    // Handles the Increment menu item
    public void doIncrement() {
        mDataValue++;
        refresh();
    }

    // Main program
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TableBug();
            }
        });
    }
}
  • 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-05-30T17:33:24+00:00Added an answer on May 30, 2026 at 5:33 pm

    In your refresh function, check if the table is being edited. If it is, get the row and column that are being edited and stop the cell editing.

    private void refresh() {
        if (table.isEditing()) {
            int row = table.getEditingRow();
            int column = table.getEditingColumn();
            table.getCellEditor(row, column).stopCellEditing();
        }
     ...
    

    To do this, you’ll need to make your table variable accessible (make it a class variable).

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

Sidebar

Related Questions

I have a core data application which uses a navigation controller to drill down
I have an application that uses Swing. The display I am working on, uses
I have an application which uses a web service to get data. Before getting
I have an application which uses SslStream to send and receive data with its
I have an application which uses Zend_Date to display dates. Zend_Date instances are created
I have a application which uses socket connection to send and receive data from
We have an WCF application which uses NHibernate to query data from the database.
I have an application which uses traditional Database for all of its data ,
I have an application which uses DirectByteBuffers to store data, but I'd like to
I have developed web application which uses JasperReports for reporting purpose. In that I

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.