#include "gtable.h"

class GTable : public GInteractor

A GTable is a thin wrapper around the Java Swing component JTable. It represents a graphical editable 2D table, like a mediocre facsimile of an Excel spreadsheet. After creating a GTable, you can listen for table events to be notified when the user types a new value into a table cell:

while (true) {
    GEvent event = waitForEvent(TABLE_EVENT);
    GTableEvent tableEvent(event);
    cout << "cell updated: " << tableEvent.toString() << endl;
    ...
}

All row/column indexes in this class are 0-based.

A GTable allows you to perform a bit of overall formatting on the table, such as changing fonts, colors and alignment. But this is done on a per-table level and there is currently no way to individually format a particular cell, row, or column. Such functionality exists in the underlying JTable but has not been exposed into this class presently.

Available since: 2015/11/07 version of C++ library

Constructors
GTable(rows, columns, x, y, width, height)Constructs a new table with the given dimensions and (optional) size.
Methods
clear()Sets all cells in the table to store an empty string value.
get(rowcol)Returns the text stored in the given cell.
getColumnWidth(col)Returns the width of the given column index in pixels.
getFont()Returns the font used in the cells of this table.
getHorizontalAlignment()Returns the horizontal alignment of the text in all cells in the table.
height()Returns the number of rows in the table.
inBounds()Returns true if the given 0-based row/column index is within the bounds of the table.
numCols()Returns the number of columns in the table.
numRows()Returns the number of rows in the table.
resize(numRowsnumCols)Modifies the table to have the given number of rows and columns, retaining any existing data.
set(rowcolvalue)Modifies the value in the given cell to store the given text.
setColumnWidth(colwidth)Sets the given column index to have the given width in pixels.
setFont(font)Modifies the value in the given cell to store the given text.
setHorizontalAlignment(alignment)Sets the horizontal alignment of the text in all cells in the table.
width()Returns the number of columns in the table.

Constructor detail


GTable(int rows = 0, int columns = 0,
       double x = 0, double y = 0,
       double width = 0, double height = 0);

Constructs a new table with the given dimensions and (optional) size. If x, y, width, or height are omitted, they are set automatically by the layout manager of the GWindow into which the table is placed. This is often what you want. Throws an error if the number of rows, columns, width, or height is negative.

Usage:

GTable* table = new GTable(rows, cols);
GTable* table = new GTable(rows, cols, x, y, width, height);

Method detail


void clear();
Sets all cells in the table to store an empty string value.

Usage:

table->clear();

string get(int row, int column) const;
Returns the text stored in the given cell. Throws an error if the given row or column are out of bounds.

Usage:

string text = table->get(row, col);

double getColumnWidth(int column) const;
Returns the width of the given column index in pixels. When a table is constructed, all columns initially have equal width. Throws an error if the given column index is out of bounds.

Usage:

double colWidth = table->getColumnWidth(col);

string getFont() const;
Returns the font used in the cells of this table. If the font has never been explicitly set, returns "*-*-*". See also: GWindow documentation on font strings.

Usage:

string font = table->getFont();

GTable::Alignment getHorizontalAlignment() const;
Returns the horizontal alignment of the text in all cells in the table. The alignment can be GTable::Alignment::LEFT, GTable::Alignment::CENTER, or GTable::Alignment::RIGHT and is initially LEFT. Currently a table's alignment is global and applies to all cells.

Usage:

GTable::Alignment alignment = table->getHorizontalAlignment();

int height() const;
Returns the number of rows in the table. Equivalent to numRows().

Usage:

int height = table->height();

bool inBounds(int row, int column) const;
Returns true if the given 0-based row/column index is within the bounds of the table. In other words, whether the index is between (0, 0) and (numRows-1, numCols-1) inclusive.

Usage:

if (table->inBounds(row, col)) { ...

int numCols() const;
Returns the number of columns in the table. Equivalent to width().

Usage:

int cols = table->numCols();

int numRows() const;
Returns the number of rows in the table. Equivalent to height().

Usage:

int rows = table->numRows();

void resize(int numRows, int numCols);
Modifies the table to have the given number of rows and columns. Any existing data is retained, and any new cells are empty. Throws an error if numRows or numCols is negative.

Usage:

table->resize(rows, cols);

void set(int row, int column, string text);
Modifies the value in the given cell to store the given text. Throws an error if the given row or column are out of bounds.

Usage:

table->set(row, col, text);

void setColumnWidth(int column, double width);
Sets the given column index to have the given width in pixels. Throws an error if the given column index is out of bounds or if the width is negative.

Usage:

table->setColumnWidth(col, width);

void setFont(string font);
Sets the cells of this table to display their text in the given font. See also: GWindow documentation on font strings.

Usage:

table->setFont(font);

void setHorizontalAlignment(GTable::Alignment alignment);
Sets the horizontal alignment of the text in all cells in the table. The alignment can be GTable::Alignment::LEFT, GTable::Alignment::CENTER, or GTable::Alignment::RIGHT and is initially LEFT. Currently a table's alignment is global and applies to all cells.

Usage:

table->setHorizontalAlignment(alignment);

int width() const;
Returns the number of columns in the table. Equivalent to numCols().

Usage:

int width = table->width();