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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:31:23+00:00 2026-05-27T23:31:23+00:00

I am trying to get selected cell in inner JTable which is present at

  • 0

I am trying to get selected cell in inner JTable which is present at some cell in JTable but unable to do so. Please let me know, where am I going wrong? Or if there are any alternative ways to do the same thing.

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.JScrollPane;
import javax.swing.JFrame;

import java.awt.*;
import java.awt.event.*;

public class SimpleTableSelectionDemo extends JFrame {
   private boolean DEBUG = true;
   private boolean ALLOW_COLUMN_SELECTION = true;
   private boolean ALLOW_ROW_SELECTION = true;

  public SimpleTableSelectionDemo() {
    super("SimpleTableSelectionDemo");

    Object[][] subTableData= {{1,2,3}, {4,5,6}, {7,8,9}};        
    String[] subColumnNames = {"Col1", "Col2", "Col3"};

    final JTable table1 = new JTable(subTableData, subColumnNames);
    TableColumnModel tcm1 = table1.getColumnModel();
    for(int it = 0; it < tcm1.getColumnCount(); it++){
        tcm1.getColumn(it).setCellRenderer(new CellRenderer());
    }
    table1.setName("InnerTable");
    processTable(table1);

    Object[][] data = {
        {"Mary", "Campione", 
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"Alison", "Huml", 
         "Rowing", new Integer(3), new Boolean(true)},
        {"Kathy", "Walrath",
         "Chasing toddlers", new Integer(2), new Boolean(false)},
        {"Mark", "Andrews",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Angela", "Lih",
         "Teaching high school", table1, new Boolean(false)}
    };

    String[] columnNames = {"First Name", 
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};

    final JTable table = new JTable(data, columnNames);
    table.setRowHeight(60);
    table.setName("OuterTable");
    processTable(table);

    table.setComponentPopupMenu(new TestPopUpDemo("Add Table"));
    TableColumnModel tcm = table.getColumnModel();
    for(int it = 0; it < tcm.getColumnCount(); it++){
        tcm.getColumn(it).setCellRenderer(new CellRenderer());
    }
    //Create the scroll pane and add the table to it. 
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this window.
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

private class CellRenderer implements TableCellRenderer{

    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
            /* If what we're displaying isn't an array of values we
            return the normal renderer*/
            if(!(value instanceof JTable)){
                return table.getDefaultRenderer( 
                value.getClass()).getTableCellRendererComponent(
                table, value, isSelected, hasFocus,row, column);
            }else {
                JTable subTable = (JTable)value;
                return subTable;
            }
        }

}

private void processTable (final JTable table) {
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow
                                       + " is now selected.");
                }
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            //We allow both row and column selection, which
            //implies that we *really* want to allow individual
            //cell selection.
            table.setCellSelectionEnabled(true);
        } 
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM =
            table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol
                                       + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table, e);
            }
        });
    }
}

private void printDebugData(JTable table, MouseEvent e) {

    System.out.println("OUTERTABLE:"+table.getSelectedRow()+"*"+table.getSelectedColumn());
    JTable innerTable = null;
    int row = table.getSelectedRow();
    int col = table.getSelectedColumn();
    Component c = table.getCellRenderer(row, col)
                        .getTableCellRendererComponent(table, table.getValueAt(row,col), true,true, row, col);
    if(c instanceof JTable){
        innerTable = (JTable)c;
        int rowIndex = innerTable.rowAtPoint(innerTable.getLocation());
        int colIndex = innerTable.columnAtPoint(e.getPoint());
        System.out.println("INNERTABLE:"+rowIndex+"*"+colIndex);
    }

}

public static void main(String[] args) {
    SimpleTableSelectionDemo frame = new SimpleTableSelectionDemo();
    frame.setSize(500, 500);
    frame.pack();
    frame.setVisible(true);
}

}

  • 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-27T23:31:24+00:00Added an answer on May 27, 2026 at 11:31 pm

    The location of the inner table is not set correctly if you call getCellRendererComponent. Use the following lines to correct the position accordingly:

    TableCellRenderer tableCellRenderer = table.getCellRenderer(row, col);
    Component c = table.prepareRenderer(tableCellRenderer, row, col);
    
    if (c instanceof JTable) {
        innerTable = (JTable) c;
    
        Point pnt = e.getPoint();
        Rectangle cellRect = table.getCellRect(row, col, false);
        pnt.translate(-cellRect.x, -cellRect.y);
    
        int rowIndex = innerTable.rowAtPoint(pnt);
        int colIndex = innerTable.columnAtPoint(pnt);
        System.out.println("INNERTABLE:" + rowIndex + "*" + colIndex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the trail name from the selected cell and pass
I am trying to get a cell value from the selected item of a
I am trying to get selected value of dropdownchoice in wicket framework , but
I am trying to get the selected option from a dropdown and populate another
I am trying to get the selected items string out of a Spinner .
I'm trying to get the HTML of a selected object with jQuery. I am
I'm trying to get the integer value of The number selected of the item.
Trying to get an ASP application deployed; it worked for a while but then
Trying to get comfortable with jQuery and I have encountered some sample code that
I'm trying to set up Excel so that the cell's value that is selected

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.