- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to retrieve the value from a table cell with TableModel in Java?
At first, create a table with DefaultTableModel −
String data[][] = { {"Australia","5","1"}, {"US","10","2"}, {"Canada","9","3"}, {"India","7","4"}, {"Poland","2","5"}, {"SriLanka","5","6"} }; String col [] = {"Team","Selected Players","Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data,col); JTable table = new JTable(tableModel);
Now, use the getModel() to retrieve the value from table cell −
Object ob = table.getModel().getValueAt(3, 2); System.out.println("Value = "+ob);
The following is an example to retrieve the value from a table cell with TableModel −
Example
package my; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { JFrame frame = new JFrame("Demo"); JPanel panel = new JPanel(); String data[][] = { {"Australia","5","1"}, {"US","10","2"}, {"Canada","9","3"}, {"India","7","4"}, {"Poland","2","5"}, {"SriLanka","5","6"} }; String col [] = {"Team","Selected Players","Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data,col); JTable table = new JTable(tableModel); table.getTableHeader().setResizingAllowed(false); Dimension dim = new Dimension(50,2); table.setIntercellSpacing(new Dimension(dim)); Object ob = table.getModel().getValueAt(3, 2); System.out.println("Value = "+ob); JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane); frame.add(panel); frame.setSize(600,400); frame.setUndecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getRootPane().setWindowDecorationStyle(JRootPane.INFORMATION_DIALOG); frame.setVisible(true); } }
Output
The value of the cell would be visible in the Console −
- Related Articles
- Java Program to retrieve the value from a cell in a JTable
- How to retrieve Date from a table in JDBC?
- How to retrieve table names from a database in MySQL?
- How to set a value in a particular JTable cell with Java?
- How to retrieve binary data from a table using JDBC?
- Write an JDBC example to retrieve Clob value from a table using the getCharacterStream() method?
- How to retrieve a DATALINK object from a table using JDBC?
- How to use MySQL SOUNDEX() function with LIKE operator to retrieve the records from table?
- How can we retrieve time from a table in JDBC?
- How to get the data from a specific cell value (say 2nd row and 2nd column) inside a table in Selenium with python?
- How to retrieve a value with MySQL count() having maximum upvote value?
- How to retrieve the corresponding value for NULL with a MySQL query?
- How to retrieve a value from MongoDB by its key name?
- How to selectively retrieve value from json output JavaScript
- How to get a value from the cell of a Pandas DataFrame?

Advertisements