javacodex.com
Java Examples
Java Examples
memu home questions

JTable

Swing lets you display and edit data in a tabular form using the JTable component. A JTable displays data using rows and columns. You can set the labels for column headers. You can also sort the table’s data at runtime. Working with a JTable can be as simple as writing a few lines of code, or it can be as complex as writing a few hundred lines of code.

Source: (JTableExample.java)

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
 
public class JTableExample {
 
  public static void main(String[] arguments) {
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JTable Example");
    frame.setSize(350,150);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    //headers for the table
    String[] columns = new String[] {
      "Id", "Name", "Price"
    };
 
    //actual data for the table in a 2d array
    Object[][] data = new Object[][] {
        { 1, "Book", 12.99 },
        { 2, "Ball",  3.50 },
        { 3, "Truck", 7.00 },
    };
 
    JTable table = new JTable(data, columns);
    JScrollPane pane = new JScrollPane(table);
 
    frame.add(pane);
 
    frame.setVisible(true);
 
  }
}
 

Output:

$ java JTableExample
Output



Contact: javacodex@yahoo.com