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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:28:24+00:00 2026-06-17T14:28:24+00:00

I have a JTable and its TableModel, it works well but what I want

  • 0

I have a JTable and its TableModel, it works well but what I want to do now is to get the selected cells of it. I thought of doing something like :

int rows = this.getTable().getRowCount();
int columns = this.getTable().getColumnCount();
for(int i = 0 ; i < rows ; i++)
{
    for(int j = 0 ; j < columns ; j++)
    {
         if(table.getCell(i,j).isSelected() //...
    }
}

But of course something like this doesn’t exist. What should I do instead?

  • 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-17T14:28:25+00:00Added an answer on June 17, 2026 at 2:28 pm

    In JTable, you have the

    JTable.getSelectedRow()
    

    and

    JTable.getSelectedColumn()
    

    You can try combine this two method with a MouseListener and a KeyListener.
    With the KeyListener you check if user is pressing the CTRL key, which means that user is selecting cells, then with a mouse listener, for every click you store maybe in a Vector or ArrayList the selected cells:

    //global variables
    JTable theTable = new JTable();//your table
    boolean pressingCTRL=false;//flag, if pressing CTRL it is true, otherwise it is false.
    Vector selectedCells = new Vector<int[]>();//int[]because every entry will store {cellX,cellY}
    
    public void something(){
       KeyListener tableKeyListener = new KeyAdapter() {
    
          @Override
          public void keyPressed(KeyEvent e) {
             if(e.getKeyCode()==KeyEvent.VK_CTRL){//check if user is pressing CTRL key
                pressingCTRL=true;
             }
          }
    
          @Override
          public void keyReleased(KeyEvent e) {
             if(e.getKeyCode()==KeyEvent.VK_CTRL){//check if user released CTRL key
                pressingCTRL=false;
             }
          }
       };
    
       MouseListener tableMouseListener = new MouseAdapter() {
    
          @Override
          public void mouseClicked(MouseEvent e) {
             if(pressingCTRL){//check if user is pressing CTRL key
                int row = theTable.rowAtPoint(e.getPoint());//get mouse-selected row
                int col = theTable.columnAtPoint(e.getPoint());//get mouse-selected col
                int[] newEntry = new int[]{row,col};//{row,col}=selected cell
                if(selectedCells.contains(newEntry)){
                   //cell was already selected, deselect it
                   selectedCells.remove(newEntry);
                }else{
                   //cell was not selected
                   selectedCells.add(newEntry);
                }
             }
          }
       };
       theTable.addKeyListener(tableKeyListener);
       theTable.addMouseListener(tableMouseListener);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having the following issue: I have a JTable with its on tablemodel
I have a JTable and i want a cell (or its row) painted in
I have a JTable that uses JTextArea as its TableCellRenderer, so that table cells
I have a JTable and, inside its TableModel, I saved an int; this is
I want to add JComboBox inside a JTable (3,3) on column 1. But in
I have a class that extends JTable , and I want to set it's
I have a Password field editor for my JTable. I want to display an
I have this JTable having an AbstractTableModel as its model. The initial contents are
I have a JTable which has 1st row blank. Now when I sort the
I have a jtable, everything works fine if I set the table data as

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.