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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:49:43+00:00 2026-05-27T20:49:43+00:00

I have a JTable with a custom TableCellRenderer and a custom TableCellEditor . By

  • 0

I have a JTable with a custom TableCellRenderer and a custom TableCellEditor. By default, the first click on a table row switch from renderer to editor and the second click select the row.

Is there any way I can make the row selected on a single click (and swith to the editor)?

I have tried to use:

table.getSelectionModel().setSelectionInterval(row, row);

in my getTableCellEditorComponent but it doesn’t work, and if I add it to my getTableCellRendererComponent it works, but only sometimes.

Here is a full example:

public class SelectRowDemo extends JFrame {

    public SelectRowDemo() {
        CellRendererAndEditor rendererAndEditor = new CellRendererAndEditor();
        StringTableModel model = new StringTableModel();
        JTable table = new JTable(model);
        table.setDefaultEditor(String.class, rendererAndEditor);
        table.setDefaultRenderer(String.class, rendererAndEditor);

        model.addElement("");
        model.addElement("");
        model.addElement("");

        add(new JScrollPane(table));
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                new SelectRowDemo();
            }

        });
    }

    class CellRendererAndEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {

        private final JLabel renderer = new JLabel();
        private final JLabel editor = new JLabel();

        @Override
        public Object getCellEditorValue() {
            return editor.getText();
        }

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

            String str = "renderer ";
            str += (isSelected) ? "selected" : "not selected";

            renderer.setText(str);
            return renderer;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {

            table.getSelectionModel().setSelectionInterval(row, row);

            String str = "editor ";
            str += (isSelected) ? "selected" : "not selected";

            editor.setText(str);
            return editor;
        }

    }

    class StringTableModel extends AbstractTableModel {

        private final List<String> data = new ArrayList<String>();

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public Object getValueAt(int row, int column) {
            return data.get(row);
        }

        @Override
        public Class<?> getColumnClass(int column) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return true;
        }

        @Override
        public void setValueAt(Object aValue, int row, int column) {
            if(aValue instanceof String) {
                data.set(row, (String)aValue);
                fireTableRowsUpdated(row, column);
            } else throw new IllegalStateException("aValue is not a String");
        }

        public void addElement(String s) {
            data.add(s);
        }

    }

}
  • 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-27T20:49:44+00:00Added an answer on May 27, 2026 at 8:49 pm

    What’s happening is that the table UI is starting to edit the cell before changing selection. If you put a println in getTableCellEditor() and shouldSelectCell(), the editor gets called first, with isSelected == false, then it calls shouldSelectCell and changes the selection.

    You can see exactly where it happens in BasicTableUI.adjustSelection(MouseEvent).

      boolean dragEnabled = table.getDragEnabled();
    
                if (!dragEnabled && !isFileList && table.editCellAt(pressedRow, pressedCol, e)) {
                    setDispatchComponent(e);
                    repostEvent(e);
                }
    
                CellEditor editor = table.getCellEditor();
                if (dragEnabled || editor == null || editor.shouldSelectCell(e)) {
                    table.changeSelection(pressedRow, pressedCol, 
                            BasicGraphicsUtils.isMenuShortcutKeyDown(e), 
                            e.isShiftDown());
                }
    

    As for rendering purposes, I’d just render it as if selected == true, since it will before that event is finished processing.

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

Sidebar

Related Questions

I have data from a database loaded into a JTable through a custom table
I have a custom cell renderer set in JTable and it works but instead
I have a custom TableCellRenderer (ValueRenderer) for a JTable, the cell is a Checkbox
I have a JTable in Java that has a custom dataMOdel and custom renderer.
I have a JTable where the first column in each row is a checkbox.
I have a custom renderer in a JTable . When my JTable displays, I
If you have JTable set with table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) and then you click drag on a
I have a JTable with custom TableCellRenderer. public class DateCellRenderer extends DefaultTableCellRenderer { private
I have a JTable with a custom Cell Renderer for multi-line cells. Everything is
I have a JTable with a custom cell renderer. The cell is a JPanel

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.