In this tutorial

You will learn how to...
  • How to create a JTable.
  • How to place a JTable in a ScrollPane.

JTable - Part 1

JTables are used to display data held in multi-dimensional arrays. This is useful for all sorts of stuff, like displaying players, customers, data from various sensor etc.
The default model of a table displays the data you put in as a table with all the cells editable, as if it were in Microsoft Excel. Each cell is of the same size.

The data in this is the 2006 team-sheet for the Scottish Rocks (Basketball).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.table.AbstractTableModel;

// Class implements three different types of listener.
public class TableExample {
 
    public Container createContentPane()
    {
        
        // The data used as the titles for the table.
        String[] title = {"No.", "Country", "Player", "Position"};

        // The data used in the table, placed as a multi-dimensional array.
        Object[][] playerdata = {       
        {new Integer(4), "United States", "Sterling Davis" , "Forward"},
        {new Integer(6), "Germany", "Moritz Wohlers", "Forward/Centre"},
        {new Integer(7), "United Kingdom", "Ross Hutton", "Centre"},
        {new Integer(8), "Belgium", "Hugo Sterk", "Guard"},
        {new Integer(10), "United Kingdom", "Andy Pearson", "Forward"},
        {new Integer(11), "United States", "Robert Yanders", "Guard"},
        {new Integer(12), "United Kingdom", "Graham Hunter", "Guard"},
        {new Integer(14), "United Kingdom", "Julius Joseph", "Guard/Forward"},
        {new Integer(15), "United Kingdom", "Gareth Murray", "Forward"},
        {new Integer(21), "United States", "Maurice Hampton", "Guard"}};

        // Table instantiated using the two sets of data.
        JTable table = new JTable(playerdata, title);
        
        // The table displayed in a Scrollpane.
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(500, 150));
    
        JPanel totalGUI = new JPanel();
        totalGUI.add(scrollPane);
        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] JTable [=]");

        //Create and set up the ContentPane
        TableExample demo = new TableExample();
        frame.setContentPane(demo.createContentPane());
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Picture of JTable

To fill the table, we need arrays of data. The headings for the table is a simple String array, and the data in the table is a 2D Object Array. Although really we only use strings just now, when we get into creating our own models for the table, there are different ways we can display the data depending on the class.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        String[] title = {"No.", "Country", "Player", "Position"};

        // The data used in the table, placed as a multi-dimensional array.
        Object[][] playerdata = {       
        {new Integer(4), "United States", "Sterling Davis" , "Forward"},
        {new Integer(6), "Germany", "Moritz Wohlers", "Forward/Centre"},
        {new Integer(7), "United Kingdom", "Ross Hutton", "Centre"},
        {new Integer(8), "Belgium", "Hugo Sterk", "Guard"},
        {new Integer(10), "United Kingdom", "Andy Pearson", "Forward"},
        {new Integer(11), "United States", "Robert Yanders", "Guard"},
        {new Integer(12), "United Kingdom", "Graham Hunter", "Guard"},
        {new Integer(14), "United Kingdom", "Julius Joseph", "Guard/Forward"},
        {new Integer(15), "United Kingdom", "Gareth Murray", "Forward"},
        {new Integer(21), "United States", "Maurice Hampton", "Guard"}};
The JTable itself is instantiated really easily. We pass the 2D array in, then the headings for the table.
31
        JTable table = new JTable(playerdata, title);
We add the table to a scroll pane for two reasons. Without one the headings aren't shown without some extra code and it means if there is too much data for the size of GUI, we can still get access to it.
34
35
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(500, 150));
That's the default model. You can create your own model for a JTable that can contain non-editable cells, checkboxes, colours and a whole lot more, but that will not be covered in these basic tutorials.

Questions

To finish, answer these simple questions on the JTable

Question 1. What is the datatype of the array containing the data?

  • a) String
  • b) Object
  • c) int

Question 2. How do we instantiate a JTable?

  • a) JTable(data, headings, colour);
  • b) JTable(headings, data);
  • c) JTable(data, headings);




Back Top Next
Email Me
Code Style


Required Lessons

External Links

Created and Edited by Stuart Davidson
All Rights Reserved ©

Valid XHTML 1.0 Strict