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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:42+00:00 2026-05-26T05:32:42+00:00

I have a JTable which only allows row selection using a ListSelectionListener as shown

  • 0

I have a JTable which only allows row selection using a ListSelectionListener as shown in the below code snippet. There are two problems in the JTable:

Q1) Although I set the first row selected by default using the setSelectionRow method, the related listSelection event is not fired when the program is started. The event is only fired if I click on another row but not the the first row – What do you think I should do to fix this considering the below code?

Q2) When I select a row, it isn’t colored in blue in JTable. I noticed this started to happen after I introduced ListSelectionListener to the JTable’s selection model. Before adding the listener, the table was making this color business when I selected a row – What do you think I should do to fix this considering the below code snippet? Thanks a lot.

     jTableBookings = new javax.swing.JTable();

     jTableBookings.setModel(new MyBookingTableModel(bookingTableData));
     jTableBookings.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
     jTableBookings.setRowSelectionAllowed(true);
     jTableBookings.setCellSelectionEnabled(false);

     jTableBookings.setColumnSelectionAllowed(false);
     initColumnSizesForMinaBokBokningarJTable(jTableBookings);

     //QUESTION 1
     //Set selected row to first row for inital load.
     if (jTableBookings.getModel().getRowCount() > 0) {
         jTableBookings.setRowSelectionInterval(0, 0);
     }

     //QUESTION 2
     jTableBookings.getSelectionModel().addListSelectionListener(new   ListSelectionListener() {
     public void valueChanged(ListSelectionEvent e) {
           int sel = jTableBookings.getSelectedRow();
           fillBookingRecordFields(sel); //Here we do some business logic based on the selected row
     }
    });

    jScrollPane9.setViewportView(jTableBookings);
  • 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-26T05:32:42+00:00Added an answer on May 26, 2026 at 5:32 am

    1) myTable.changeSelection(row, column, false, false);

    2) disable setCellSelectionEnabled() and setColumnSelectionAllowed(), then works

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    class SelectedColumnTest {
    
        private JTableHeader header;
        private Object selectedColumn = null;
        private String[] columnNames = {"String", "Integer", "Boolean"};
        private Object[][] data = {{"aaa", 12, true}, {"bbb", 5, false}, {"CCC", 92, true}};
        private TableModel model = new DefaultTableModel(data, columnNames) {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        private JTable table = new JTable(model);
    
        public JComponent makeUI() {
            //table.setRowSelectionAllowed(true);
            //table.setCellSelectionEnabled(false);
            //table.setColumnSelectionAllowed(false);
            JPanel p = new JPanel(new BorderLayout());
            p.add(new JScrollPane(table));
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            table.changeSelection(0, 0, false, false);
            return p;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        public static void createAndShowGUI() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.getContentPane().add(new SelectedColumnTest().makeUI());
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable which is loaded from a data-structure using table model.The data-structure
I have a JTable which has 1st row blank. Now when I sort the
I have two JFrame Forms-SelectContactsfrm.java and Taskfrm.java. There is JTable in SelectContactsfrm file to
I have a jTable which currently displays and allows editing of a database table,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a JTable (extended) and an implementation of TableModel , in which I
I am working in JTable and I have a requirement like this. Say There
I have a JTable which is bound to my EventTracker bean, essentially a wrapper
I have a JTable which has certain cells editable. If the user enters in
We have a Java Swing application which contains components like JTable, JCombobox, JTextArea and

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.