• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Swing » JTable in Java Swing Example
Next →
← Prev

JTable in Java Swing Example

By Dinesh Thakur

The JTable class is another Swing component that has no equivalent in AWT. JTable provides a very flexible possibility to create and display tables. Lets build tables from arrays or vectors of objects or from objects that implement the TableModel interface.

The JTableModel interface defines methods for objects that specify the contents of a table. The class provides an implementation AbstractTableModel the JTableModel predetermined interface. This class is typically extended to provide a custom implementation of model table.
The JTable class provides the ability to edit tables. the method setCellEditor () allows an object of the interface is TableCellEditor identified as the editor table cells.

                       Constructors and methods of the class JTable.

Constructors and Methods

Description

JTable()

Constructs a default JTable Initialized to default data model, default column and selection model.

JTable(int nrows, int ncols)

Constructs a new JTable with nrows and neols of empty cells using DelaultTableModel.

JTable(Object[][] rowdata,

Object[ ] colnames)

Constructs a JTable to display the data contained in the two-dimensional array, rowdata, having the column names specified in the array, colnames.

JTable(Vector rowvector, Vector colvector)

Constructs a JTable to display the data available in vector of vectors, rowvector, with column names available in colvector.

JTable(TableModel dm)

Constructs a new JTable initialized to specify data model, dm, with the default column model and the default selection model.

jT able(TableModel dm, TableColumnModel cm)

Constructs a JTable initialized to specified data model, dm, also initialized to specified column model, cm, with default selection model.

JTable(TableModel dm, TableColumn

Model cm, ListSelectlonModel lsm)

Constructs a JTable initialized to specified data model, dm. specified column model, cm, and specified list selection model, Ism.

void addColumn(TableColumn coI)

Appends a column to the array of columns.

int getColumnCount()

Returns the number of columns in the column model. (Note: number of columns here may differ from the number of columns In the table model).

int getRowCount()

Returns the number of rows in the table.

int getSelectedColumn()

Returns the index of first selected column or returns -1 if no column is selected.

int getSelectedColumnCount()

Returns the number of columns selected.

int[ ] getSelectedColumns()

Returns the indices of all columns selected.

int getSelectedRow()

Returns the index of first selected row or returns -1 if no row is selected.

int getSelectedRowCount()

Returns the number of rows selected.

int[ ] getSelectedRows()

Returns the index of all the rows selected.

Object getValueAt(int r, int c)

Returns the value at rth row and cth column of JTable.

void removeColumn(Table

Column tc)

Deletes or removes a column from JTable’s array of columns.

import java.awt.*; 
import javax.swing.*;
import javax.swing.table.*;
public class JavaExampleTableInJApplet extends JApplet
 {
    Object[] Dt = new Object[5];
    DefaultTableModel DfltTblModl = new DefaultTableModel();
    JTable Tbl = new JTable(DfltTblModl);
    public void init()
     {
        for(int clmn = 0; clmn < 5; clmn++)
           {
             DfltTblModl.addColumn("Column"+clmn);
           }
             for(int row = 0; row < 10; row++)
             {
                     for(int clmn = 0; clmn < 5; clmn++)
                  {
                          Dt[clmn] = "Cell " + row + "," + clmn;
                      }
                          DfltTblModl.addRow(Dt);
                 }
                    getContentPane().add(new JScrollPane(Tbl));
      }
  }
/*<APPLET CODE=JavaExampleTableInJApplet.class WIDTH=360 HEIGHT=290 ></APPLET>*/

JTable in Java Swing Example

You’ll also like:

  1. JTable Image Cell Example in Java
  2. JList in Java Swing Example
  3. SpringLayout in Java Swing Example
  4. FocusListener in Java Swing Example
  5. CheckerBoard Java Swing Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW