Class DataGrid

All Implemented Interfaces:
Animation, Editable, StyleListener

public class DataGrid extends ScrollSurface

A grid of data for large sets: a TableModel or a tree of rows, with sorting, filtering, grouping, frozen columns, resizable and movable columns, selection, keyboard navigation and editing in place.

The grid paints its cells itself, like the com.codename1.ui.scheduler views. There is no component per cell or per row: only the rows on screen are painted, sorting and filtering move row numbers in an array and never the data, and opening a node of a tree costs what it shows. A million rows scroll as one hundred do. Table, which lays out a component per cell, is the class for a small form laid out as a table.

TableModel model = new DefaultTableModel(new String[]{"Name", "Team", "Score"}, new Object[][]{
        {"dana", "red", 7}, {"Ann", "blue", 3}, {"carl", "red", 9}}, true);
Form form = new Form("Scores", new BorderLayout());
DataGrid grid = new DataGrid(model);
grid.getColumn(0).setFlex(1);
grid.getColumn(2).setKind(GridColumn.Kind.NUMBER).setAggregate(GridAggregate.SUM);
grid.setFrozenColumns(1);
grid.sortBy(grid.getColumn(2), false);
form.add(BorderLayout.CENTER, grid);
form.show();
Rows

A row number of the grid is a view row: an index into what is shown after filtering, sorting, grouping and the opening of nodes. It is not a row of the model; getModelRow(int) and getNode(int) translate it.

Trees

Built over a TreeTableModel, the grid is a tree table: the tree column (setTreeColumn, the first visible one by default) shows each node indented under its parent with an arrow that opens it. Children are asked of the model when a node is first opened. Sorting orders siblings.

Editing

A cell is edited when its column and its model both allow it. A boolean toggles with a tap. Any other value is edited by its column's GridCellEditor, which supplies a component the grid lays over the cell while it is edited and removes afterwards: a text field, or a picker for a date or a choice.

Styles

DataGrid (the background), DataGridHeader, DataGridRow and its selected state, DataGridRowAlt (every other row), DataGridGroupRow, DataGridLine (the foreground is the colour of the grid lines), DataGridCheck and DataGridEditor. Only colours, fonts and paddings are read; nothing has a border.

  • Constructor Details

    • DataGrid

      public DataGrid()
      A grid with no data.
    • DataGrid

      public DataGrid(TableModel model)
      A grid over the rows of a table model, with a column for each of its columns.
    • DataGrid

      public DataGrid(TreeTableModel model)
      A tree grid over a tree table model, with a column for each of its columns.
  • Method Details

    • setModel

      public final void setModel(TableModel model)
      Shows the rows of a table model, with a column for each of its columns. Sorting, filtering and grouping are kept when their columns still exist.
    • setModel

      public final void setModel(TreeTableModel model)
      Shows the nodes of a tree table model, with a column for each of its columns. The roots are shown closed.
    • getTableModel

      public TableModel getTableModel()
      The table model shown, or null for a tree grid.
    • getTreeModel

      public TreeTableModel getTreeModel()
      The tree model shown, or null for a flat grid.
    • isTree

      public boolean isTree()
      Whether this is a tree grid.
    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class ScrollSurface
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class ScrollSurface
    • refresh

      public void refresh()
      Reads the model again: filters, sorts and groups its rows anew and keeps the selection on the same rows where they are still shown.
    • refreshNode

      public void refreshNode(Object node)
      Asks the tree model again for the children of one node, and shows them. Use it after the children of a node changed; refresh() reads the whole tree again.
    • getColumns

      public List<GridColumn> getColumns()
      The columns, in the order they are shown, hidden ones included.
    • getColumnCount

      public int getColumnCount()
      How many columns there are, hidden ones included.
    • getColumn

      public GridColumn getColumn(int index)
      The column at a place in the order, hidden ones counted.
    • getColumnForModel

      public GridColumn getColumnForModel(int modelColumn)
      The column showing a column of the model, or null.
    • setColumns

      public void setColumns(GridColumn... cols)
      Replaces the columns. A model column with no GridColumn is not shown, and one may be shown twice.
    • addColumn

      public void addColumn(GridColumn c)
      Adds a column at the end.
    • removeColumn

      public void removeColumn(GridColumn c)
      Removes a column.
    • moveColumn

      public void moveColumn(int from, int to)
      Moves the column at from in the order to to, hidden ones counted.
    • refreshColumns

      public void refreshColumns()
      Asks the grid to lay its columns out again, after a column's width, title or visibility changed.
    • getFrozenColumns

      public int getFrozenColumns()
      How many of the first visible columns stay in place while the rest scroll sideways.
    • setFrozenColumns

      public void setFrozenColumns(int count)
      Sets how many of the first visible columns stay in place while the rest scroll sideways; 0 by default.
    • getTreeColumn

      public GridColumn getTreeColumn()
      The column a tree grid draws the tree in, or null for the first visible one.
    • setTreeColumn

      public void setTreeColumn(GridColumn c)
      Sets the column a tree grid draws the tree in; null, the default, is the first visible one.
    • sortBy

      public void sortBy(GridColumn c, boolean ascending)
      Sorts by one column alone.
    • addSort

      public void addSort(GridColumn c, boolean ascending)
      Adds a column to sort by after the ones already sorted by, or changes its direction when it is one of them.
    • clearSort

      public void clearSort()
      Stops sorting: rows are shown in the model's order.
    • getSortColumns

      public List<GridColumn> getSortColumns()
      The columns sorted by, most significant first.
    • getSortDirection

      public int getSortDirection(GridColumn c)
      1 when sorted by c ascending, -1 descending, 0 when not sorted by it.
    • getFilter

      public GridFilter getFilter()
      The filter of the rows, or null.
    • setFilter

      public void setFilter(GridFilter filter)
      Shows only the rows a filter accepts; null shows them all.
    • getGroupColumn

      public GridColumn getGroupColumn()
      The column the rows are grouped by, or null.
    • setGroupColumn

      public void setGroupColumn(GridColumn c)

      Groups the rows by the values of a column, each group under a row of its own that opens and closes it; null stops grouping. A tree grid is never grouped.

      Groups are ordered by their value, in the column's sort direction when it is sorted by, and the group rows show the column's GridAggregates.

    • getRowCount

      public int getRowCount()
      How many rows are shown, group rows included.
    • getValueAt

      public Object getValueAt(int row, GridColumn c)
      The value a view row shows in a column.
    • setValueAt

      public void setValueAt(int row, GridColumn c, Object value)
      Sets the value of a view row in a column, through the model.
    • getModelRow

      public int getModelRow(int row)
      The model row a view row shows, or -1 for a group row or in a tree grid.
    • getViewRow

      public int getViewRow(int modelRow)
      The view row showing a model row, or -1 when it is filtered out or in a closed group.
    • getNode

      public Object getNode(int row)
      The node a view row of a tree grid shows, or null.
    • getRowOfNode

      public int getRowOfNode(Object node)
      The view row showing a node, or -1 when it is not shown.
    • isGroupRow

      public boolean isGroupRow(int row)
      Whether a view row is a group row.
    • getDepth

      public int getDepth(int row)
      How deep a view row is in the tree: 0 for a root.
    • isExpandable

      public boolean isExpandable(int row)
      Whether a view row can be opened: a node that is not a leaf, or a group.
    • isExpanded

      public boolean isExpanded(int row)
      Whether a view row is open.
    • setExpanded

      public void setExpanded(int row, boolean open)
      Opens or closes a view row: a node of a tree, or a group.
    • setNodeExpanded

      public void setNodeExpanded(Object node, boolean open)
      Opens or closes a node of a tree grid, whether it is shown or not; a node that is not shown opens the next time it is.
    • isNodeExpanded

      public boolean isNodeExpanded(Object node)
      Whether a node of a tree grid is open.
    • expandAll

      public void expandAll()
      Opens every row. In a tree grid this loads the whole tree.
    • collapseAll

      public void collapseAll()
      Closes every row.
    • getSelectionMode

      public DataGrid.SelectionMode getSelectionMode()
      What a tap selects.
    • setSelectionMode

      public void setSelectionMode(DataGrid.SelectionMode mode)
      Sets what a tap selects; MULTIPLE_ROWS by default.
    • getSelectedRow

      public int getSelectedRow()
      The row the keyboard is on, the last one chosen, or -1.
    • getSelectedColumn

      public int getSelectedColumn()
      The visible column index of the cell the keyboard is on.
    • getSelectedRows

      public int[] getSelectedRows()
      The selected view rows, in order.
    • getSelectedRowCount

      public int getSelectedRowCount()
      How many rows are selected.
    • isRowSelected

      public boolean isRowSelected(int row)
      Whether a view row is selected.
    • setSelectedRow

      public void setSelectedRow(int row)
      Selects one view row and moves the keyboard to it; -1 clears the selection.
    • selectRows

      public void selectRows(int from, int to, boolean add)
      Selects a range of view rows, adding to the selection when add.
    • selectAll

      public void selectAll()
      Selects every row.
    • clearSelection

      public void clearSelection()
      Selects nothing.
    • addSelectionListener

      public void addSelectionListener(SelectionListener l)
      Listens to changes of the selection. The values given are the row the keyboard was on and is on now; read the rest from the grid.
    • removeSelectionListener

      public void removeSelectionListener(SelectionListener l)
      Stops listening to changes of the selection.
    • addActionListener

      public void addActionListener(ActionListener l)
      Listens to rows being opened: a double tap, or Enter on a cell that is not edited. The selected row is the one opened.
    • removeActionListener

      public void removeActionListener(ActionListener l)
      Stops listening to rows being opened.
    • getRowHeightMM

      public float getRowHeightMM()
      The height of a row in millimetres, 0 for the height of the font.
    • setRowHeightMM

      public void setRowHeightMM(float mm)
      Sets the height of a row in millimetres; 0, the default, fits the row style's font and padding.
    • getRowHeight

      public int getRowHeight()
      The height of a row, in pixels, as last laid out.
    • isHeaderVisible

      public boolean isHeaderVisible()
      Whether the header row is shown.
    • setHeaderVisible

      public void setHeaderVisible(boolean v)
      Shows or hides the header row.
    • isStriped

      public boolean isStriped()
      Whether every other row is painted in DataGridRowAlt.
    • setStriped

      public void setStriped(boolean striped)
      Sets whether every other row is painted in DataGridRowAlt.
    • setGridLines

      public void setGridLines(boolean horizontal, boolean vertical)
      Sets which grid lines are drawn.
    • getEmptyText

      public String getEmptyText()
      The text shown when there are no rows.
    • setEmptyText

      public void setEmptyText(String text)
      Sets the text shown when there are no rows; empty by default.
    • getDefaultRenderer

      public GridCellRenderer getDefaultRenderer()
      The renderer of a column that has none.
    • setDefaultRenderer

      public void setDefaultRenderer(GridCellRenderer r)
      Sets the renderer of a column that has none.
    • getDefaultEditor

      public GridCellEditor getDefaultEditor()
      The editor of a column that has none.
    • setDefaultEditor

      public void setDefaultEditor(GridCellEditor e)
      Sets the editor of a column that has none.
    • layoutSurface

      protected void layoutSurface(int width, int height)
      Description copied from class: ScrollSurface
      Lays the surface out for a size: sets the header sizes and whatever the content's size depends on. Called before every paint and every pointer event, so it has to return at once when nothing has changed.
      Specified by:
      layoutSurface in class ScrollSurface
    • isMirrored

      protected boolean isMirrored()
      Right to left the grid is mirrored: the first column, and the frozen ones, are at the right, and it scrolls toward the left.
      Overrides:
      isMirrored in class ScrollSurface
    • getContentWidth

      protected int getContentWidth()
      Description copied from class: ScrollSurface
      The width of what scrolls, in pixels, without the left header.
      Specified by:
      getContentWidth in class ScrollSurface
    • getContentHeight

      protected int getContentHeight()
      Description copied from class: ScrollSurface
      The height of what scrolls, in pixels, without the top header.
      Specified by:
      getContentHeight in class ScrollSurface
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class ScrollSurface
    • getPageStep

      protected int getPageStep(int bodyHeight)
      Description copied from class: ScrollSurface
      How far down a page of content height bodyHeight goes before the next one starts. A view of rows answers a whole number of them, so no page ends in the middle of one.
      Overrides:
      getPageStep in class ScrollSurface
    • paintContent

      protected void paintContent(Graphics g, int x, int y)
      Description copied from class: ScrollSurface
      Paints what scrolls. x and y are where the content's own origin is on screen, which is off screen once it has scrolled. The graphics is clipped to the content region.
      Specified by:
      paintContent in class ScrollSurface
    • paintLeftHeader

      protected void paintLeftHeader(Graphics g, int x, int y)
      Description copied from class: ScrollSurface
      Paints the pinned left column. x is its left; y is where the content's origin is.
      Overrides:
      paintLeftHeader in class ScrollSurface
    • paintTopHeader

      protected void paintTopHeader(Graphics g, int x, int y)
      Description copied from class: ScrollSurface
      Paints the pinned top row. x is where the content's origin is; y is the top of the row.
      Overrides:
      paintTopHeader in class ScrollSurface
    • paintCorner

      protected void paintCorner(Graphics g, int x, int y)
      Description copied from class: ScrollSurface
      Paints the corner where the two headers meet, at its top left.
      Overrides:
      paintCorner in class ScrollSurface
    • paintOverlay

      protected void paintOverlay(Graphics g, int x, int y)
      Description copied from class: ScrollSurface
      Paints over everything, in the component's own bounds, whose top left is x, y. Not painted on a printed page: it is for what a gesture in progress shows.
      Overrides:
      paintOverlay in class ScrollSurface
    • setCellPicker

      public void setCellPicker(GridCellPicker picker)

      Sets what takes the taps on cells while it asks for them, as the formula editor of a spreadsheet does to put the tapped cell's reference into a formula. Null, the default, leaves every tap to the grid.

      Parameters
      • picker: the picker, or null
    • getCellPicker

      public GridCellPicker getCellPicker()

      The picker set with #setCellPicker(GridCellPicker).

      Returns

      the picker, or null

    • setOverlayPainter

      public void setOverlayPainter(Painter painter)

      Sets a painter that draws over the cells and the headers, after everything else, as the outlines a spreadsheet draws around the cells a formula being edited refers to. The rectangle it is given is the grid's, in the coordinates it paints in, so a cell is at the rectangle's corner plus what #getCellBounds(int, int, int[]) answers. Null, the default, draws nothing; repaint() after what it draws changes.

      Parameters
      • painter: the painter, or null
    • getOverlayPainter

      public Painter getOverlayPainter()

      The painter set with #setOverlayPainter(com.codename1.ui.Painter).

      Returns

      the painter, or null

    • getCellBounds

      public void getCellBounds(int row, int column, int[] out)
      The bounds of a cell in the grid's own coordinates: x, y, width, height. column is an index among the visible columns.
    • getVisibleColumns

      public GridColumn[] getVisibleColumns()
      The columns shown, in order: the ones getSelectedColumn() and getCellBounds count.
    • scrollToCell

      public void scrollToCell(int row, int column)
      Scrolls so a view row is shown, and a visible column too when it is 0 or more.
    • pressStarted

      protected void pressStarted(int x, int y)
      Description copied from class: ScrollSurface
      The pointer went down at a point, in the component's coordinates. Whatever it turns into -- a tap, a drag, a long press -- follows.
      Overrides:
      pressStarted in class ScrollSurface
    • tapped

      protected void tapped(int x, int y, boolean doubleTap)
      Description copied from class: ScrollSurface
      A tap that was not a drag or a long press.
      Overrides:
      tapped in class ScrollSurface
    • editStarted

      protected boolean editStarted(int x, int y, ScrollSurface.EditTrigger trigger)
      Description copied from class: ScrollSurface

      Asked when a drag or a long press begins, at the point where the pointer went down, in the component's coordinates.

      Parameters
      • trigger: what kind of gesture it is
      Returns

      true when an edit has begun and the drag belongs to it; false to pan

      Overrides:
      editStarted in class ScrollSurface
    • editMoved

      protected void editMoved(int x, int y)
      Description copied from class: ScrollSurface
      The pointer of an edit moved, or the surface scrolled under it.
      Overrides:
      editMoved in class ScrollSurface
    • editEnded

      protected void editEnded(boolean committed)
      Description copied from class: ScrollSurface
      The edit is over. committed is false when it was cancelled: by cancelEdit, a second finger, or the surface leaving the form.
      Overrides:
      editEnded in class ScrollSurface
    • isEditAutoScroll

      protected boolean isEditAutoScroll(int x, int y)
      Description copied from class: ScrollSurface
      Whether the surface scrolls under an edit dragged near its edge. Asked on every move of an edit; true by default.
      Overrides:
      isEditAutoScroll in class ScrollSurface
    • hoverAt

      protected void hoverAt(int x, int y)
      Description copied from class: ScrollSurface
      The pointer rests at a point of the surface without being pressed. A view answers with setHoverTarget, naming what is there and the cursor that says what a press would do.
      Overrides:
      hoverAt in class ScrollSurface
    • scrollChanged

      protected void scrollChanged()
      Description copied from class: ScrollSurface
      The scroll position changed.
      Overrides:
      scrollChanged in class ScrollSurface
    • handlesInput

      public boolean handlesInput()
      Description copied from class: Component

      Prevents key events from being grabbed for focus traversal. E.g. a list component might use the arrow keys for internal navigation so it will switch this flag to true in order to prevent the focus manager from moving to the next component.

      Returns
      Overrides:
      handlesInput in class Component
      Returns:
      true if key events are being used for focus traversal ; otherwise false
    • keyPressed

      public void keyPressed(int keyCode)
      Description copied from class: Component

      If this Component is focused, the key pressed event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyPressed in class Component
    • isEditing

      public boolean isEditing()
      Whether a cell is being edited.
      Specified by:
      isEditing in interface Editable
      Overrides:
      isEditing in class Component
    • getEditingRow

      public int getEditingRow()
      The view row being edited, or -1.
    • getEditingColumn

      public GridColumn getEditingColumn()
      The column being edited, or null.
    • editCellAt

      public boolean editCellAt(int row, int column)

      Edits a cell, when it may be edited. column is an index among the visible columns. A boolean is toggled at once; anything else gets its column's editor over the cell.

      Returns

      true when the cell is being edited, or was toggled

    • commitEdit

      public void commitEdit()
      Ends the edit, writing the editor's value to the model.
    • cancelEdit

      public final void cancelEdit()
      Ends the edit without changing the model, and any drag in progress.
      Overrides:
      cancelEdit in class ScrollSurface