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

  • Home
  • SEARCH
  • 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 7983699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:02:31+00:00 2026-06-04T11:02:31+00:00

How do I display the information of a JTable row when selected? I’ll briefly

  • 0

How do I display the information of a JTable row when selected?

I’ll briefly explain what I am trying to do and then post the SSCCE that I created in case any of my explanation is confusing.

I’m wanting to be able to click any row in a table and display that information on a panel. I’m not sure what I need to make use of to get the job done.

I’m thinking I’ll need to use:

  • table.getSelectedRow()
  • MouseListener()
  • ListSelectionListener()

I haven’t used Listeners before, so I only know of those from reading articles/documentation while researching what I need to do to complete the job.

I’m also slightly confused as to how to display the info on my JPanel. The panel is created in the main class where as the table is created in its own class.

I appreciate any help and advice.

Example source :

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class SwingTesting {

    private final JFrame frame;
    private final TablePane tablePane;
    private final JSplitPane splitPane;
    private final JPanel infoPanel;

    public SwingTesting() {
        tablePane = new TablePane();
        infoPanel = new JPanel();
        frame = new JFrame();

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tablePane, infoPanel);

        frame.add(splitPane);
        frame.pack();
        frame.setVisible(true);
    }


    class TablePane extends JPanel {

        private final JTable table;
        private final TableModel tableModel;
        private final ListSelectionModel listSelectionModel;

    public TablePane() {
        table = new JTable();
        tableModel = createTableModel();
        table.setModel(tableModel);
        table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        table.add(table.getTableHeader(), BorderLayout.PAGE_START);
        table.setFillsViewportHeight(true); 

        listSelectionModel = table.getSelectionModel();
        table.setSelectionModel(listSelectionModel);
        listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
        table.setSelectionModel(listSelectionModel);

        this.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    gbc.gridwidth = 3;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.ipadx = 2;
    gbc.ipady = 2;
    gbc.weightx = 1;
    gbc.weighty = 1;
        this.add(new JScrollPane(table), gbc);
    }

    private TableModel createTableModel() {
        DefaultTableModel model = new DefaultTableModel(
            new Object[] {"Car", "Color", "Year"}, 0 
        ){
            @Override 
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        addTableData(model);
        return model;
    }

    private void addTableData(DefaultTableModel model) {
        model.addRow(new Object[] {"Nissan", "Black", "2007"});
        model.addRow(new Object[] {"Toyota", "Blue", "2012"});
        model.addRow(new Object[] {"Chevrolet", "Red", "2009"});
        model.addRow(new Object[] {"Scion", "Silver", "2005"});
        model.addRow(new Object[] {"Cadilac", "Grey", "2001"});
    }


    class SharedListSelectionHandler implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();
            String contents = "";

            if(lsm.isSelectionEmpty()) {
                System.out.println("<none>");
            } else {
                int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();
                for(int i = minIndex; i <= maxIndex; i++) {
                    if(lsm.isSelectedIndex(i)) {
                        for(int j = 0; j < table.getColumnCount(); j++) {
                            contents += table.getValueAt(i, j) + " ";
                        }
                    }
                }
                System.out.println(contents);
            }
        }

    }
}

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

This doesn’t quite perform like I want it. It prints out double the information.

So instead of Chevrolet Red 2009 it prints out Chevrolet Red 2009 Chevrolet Red 2009. Ultimately I’m wanting to put the text in a JLabel and put it on the panel. Keeping in mind that the panel containing the JLabel is in a different class than the table.

  • 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-04T11:02:32+00:00Added an answer on June 4, 2026 at 11:02 am
    table.getModel().addTableModelListener(tableModelListener);
    

    See TableModel.addTableModelListener(TableModelListener) for details.


    Based on the SSCCE.

    GUI with details on selection

    import javax.swing.*;
    import java.awt.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class SwingTesting {
    
        private final JFrame frame;
        private final TablePane tablePane;
        private final JSplitPane splitPane;
        private final JPanel infoPanel;
    
        JTextField make = new JTextField(9);;
        JTextField color = new JTextField(7);;
        JTextField year = new JTextField(4);
    
        public SwingTesting() {
            tablePane = new TablePane();
            infoPanel = new JPanel(new FlowLayout(5));
    
            infoPanel.add(new JLabel("Make"));
            infoPanel.add(make);
            infoPanel.add(new JLabel("Color"));
            infoPanel.add(color);
            infoPanel.add(new JLabel("Year"));
            infoPanel.add(year);
    
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tablePane, infoPanel);
    
            frame.add(splitPane);
            frame.pack();
            frame.setVisible(true);
        }
    
        class TablePane extends JPanel {
    
            private final JTable table;
            private final TableModel tableModel;
            private final ListSelectionModel listSelectionModel;
    
        private void setFields(int index) {
            make.setText(table.getValueAt(index, 0).toString());
            color.setText(table.getValueAt(index, 1).toString());
            year.setText(table.getValueAt(index, 2).toString());
        }
    
        private void clearFields() {
            make.setText("");
            color.setText("");
            year.setText("");
        }
    
        public TablePane() {
            table = new JTable();
            tableModel = createTableModel();
            table.setModel(tableModel);
            table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            table.add(table.getTableHeader(), BorderLayout.PAGE_START);
            table.setFillsViewportHeight(true);
    
            listSelectionModel = table.getSelectionModel();
            table.setSelectionModel(listSelectionModel);
            listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
            table.setSelectionModel(listSelectionModel);
    
            this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 3;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.ipadx = 2;
        gbc.ipady = 2;
        gbc.weightx = 1;
        gbc.weighty = 1;
            this.add(new JScrollPane(table), gbc);
        }
    
        private TableModel createTableModel() {
            DefaultTableModel model = new DefaultTableModel(
                new Object[] {"Car", "Color", "Year"}, 0
            ){
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };
    
            addTableData(model);
            return model;
        }
    
        private void addTableData(DefaultTableModel model) {
            model.addRow(new Object[] {"Nissan", "Black", "2007"});
            model.addRow(new Object[] {"Toyota", "Blue", "2012"});
            model.addRow(new Object[] {"Chevrolet", "Red", "2009"});
            model.addRow(new Object[] {"Scion", "Silver", "2005"});
            model.addRow(new Object[] {"Cadilac", "Grey", "2001"});
        }
    
    
        class SharedListSelectionHandler implements ListSelectionListener {
    
            @Override
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                String contents = "";
    
                if(lsm.isSelectionEmpty()) {
                    System.out.println("<none>");
                } else {
                    int minIndex = lsm.getMinSelectionIndex();
                    int maxIndex = lsm.getMaxSelectionIndex();
                    if (minIndex==maxIndex) {
                        setFields(minIndex);
                    } else {
                        clearFields();
                        for(int i = minIndex; i <= maxIndex; i++) {
                            if(lsm.isSelectedIndex(i)) {
                                for(int j = 0; j < table.getColumnCount(); j++) {
                                    contents += table.getValueAt(i, j) + " ";
                                }
                            }
                        }
                        System.out.println(contents);
                    }
                }
            }
    
        }
    }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new SwingTesting();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m making a simple website to display information from XML file that is created
I'm trying to display information from a Cursor in a ListView , each row
REASON: I'm working on an emergency alert application that needs to display information on
I'm working on a C# XNA project that requires me to display information based
I am trying to display volunteer information with duty and what performance is allocated.
I have to develop an application that display some information about the possessor of
Example now I have a main frame contains jtable display all the customer information,
I have Java Desktop application that displays some information in a JTable that contains
I'm creating a query that will display information for a record which is derived
I'm trying to display information from my ObservableCollection<MyData> in a ListView . MyData has:

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.