JTables


Overview Basic version Model Cell Renderer Editor Column widths Tooltips Controls in cells

A JTable is a Swing component used to display tables in desktop application. A JTable is different from an html <table > element in a web page in a browser, and is also different from a table in a database.

These notes assume you know core Java and have a basic grasp of Swing

It looks like you simply have a JTable instance to display a table. In fact a JTable instance uses a whole set of other class instances. You can produce a basic table with only a few lines of code. But to control the appearance and operation of the table, you have to know a bit more.

JTables (and some other Swing widgets) use a modified model-view-controller pattern. That means the data which seems to be in the table is actually held in a separate object, the 'model'. Data goes in and out of the table by going into the model.

The cells in the table are actually separate components, which are often JLabels. You define a cell renderer to control the appearance of the cells. You can define different renderers for different columns or different data types.

When the user selects a cell, they might (controlled by the model) be able to edit the contents. In that case, the renderer component is replace by a corresponding editor, which defaults to a JTextfield for most types.

Comments

0 responses so far

Add a new comment

Your name

Your email (will not be published)

Your comment (no HTML)

Are you a robot? Please type the two words: