Creating a JTable with rows of variable height : Table « Swing JFC « Java






Creating a JTable with rows of variable height

 

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

public class Main {

  public static void main(String[] argv) {

    JFrame demoFrame = new JFrame("Variable Row Height Table Demo");

    StringTableModel imageTableModel = new StringTableModel();

    JTable imageTable = new JTable(imageTableModel);
    imageTable.getColumnModel().getColumn(0).setCellRenderer(new VariableRowHeightRenderer());

    demoFrame.getContentPane().add(new JScrollPane(imageTable));

    demoFrame.pack();
    demoFrame.setVisible(true);
  }

}

class VariableRowHeightRenderer extends JLabel implements TableCellRenderer {
  public VariableRowHeightRenderer() {
    super();
    setOpaque(true);
    setHorizontalAlignment(JLabel.CENTER);
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    if (isSelected) {
      setBackground(UIManager.getColor("Table.selectionBackground"));
    }

    if (hasFocus) {
      setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));

      if (table.isCellEditable(row, column)) {
        super.setForeground(UIManager.getColor("Table.focusCellForeground"));
        super.setBackground(UIManager.getColor("Table.focusCellBackground"));
      }
    } else {
      setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    }
    setText((String) (value));
    table.setRowHeight(row, getPreferredSize().height + row * 10);
    return this;
  }
}

class StringTableModel extends AbstractTableModel {
  public static final int IMG_COL = 0;

  public String[] m_colNames = { "Variable Dimension" };

  public Class[] m_colTypes = { String.class };

  public StringTableModel() {
    super();

  }

  public int getColumnCount() {
    return m_colNames.length;
  }

  public int getRowCount() {
    return 3;
  }

  public String getColumnName(int col) {
    return "" + col;
  }

  public Object getValueAt(int row, int col) {
    return "aa";
  }
}

   
  








Related examples in the same category

1.Creating a JTable Component
2.Creates tables that allow rows and columns to be added or deleted
3.Build a table from list data and column names
4.Getting the Number of Rows and Columns in a JTable Component
5.Appending a Row to a JTable Component
6.Add columns to a table through DefaultTableModel
7.Insert a row to a table through DefaultTableModel
8.Insert a row to a table through DefaultTableModel at specified row
9.Scroll Table SampleScroll Table Sample
10.Simple demonstration of JTableSimple demonstration of JTable
11.Create a table with two dimensional arrayCreate a table with two dimensional array
12.Create table with Unicode dataCreate table with Unicode data
13.Resize TableResize Table
14.Expense TableExpense Table
15.StockTable 6: Action and Dynamic DisplayStockTable 6: Action and Dynamic Display
16.JTable selection eventsJTable selection events
17.A table with the ability to play with row and column selectionsA table with the ability to play with row and column selections
18.Table Selection modeTable Selection mode
19.Display ResultSet in Table (JTable)
20.JTable(Table) with JDBC and ResultSetJTable(Table) with JDBC and ResultSet
21.List UI Properties in a JTable and sortableList UI Properties in a JTable and sortable
22.Property Table: Use JTable to display and edit propertiesProperty Table: Use JTable to display and edit properties
23.Table with tool tips for both cells and column headersTable with tool tips for both cells and column headers
24.StockTable 4: Table SorterStockTable 4: Table Sorter
25.JTable sorter:click the table header to sort a column and a tableJTable sorter:click the table header to sort a column and a table
26.Creating image out of a JTable
27.JTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
28.JTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
29.JTable.setColumnSelectionAllowed(boolean b);
30.JTable.setRowSelectionAllowed(boolean b);
31.JTable.setCellSelectionEnabled(boolean b);
32.Handle selection and model change events for a JTable.
33.ListSelectionModel rowSelMod = JTable.getSelectionModel();
34.ListSelectionModel colSelMod = JTable.getColumnModel().getSelectionModel();
35.JTree.getModel().addTreeModelListener(new TreeModelListener())
36.Move the last visible column so it becomes the first visible column
37.the last column is moved to the first position
38.Allowing the User to Resize a Column in a JTable Component
39.Retrieve the value in the visible cell (1,2) in a JTable
40.Retrieve the value in cell (1,2) from the model
41.Change a cell in the 2nd visible column
42.Change a cell in the 3rd column in the model
43.Showing the Table Header in a Non-Scrollable JTable Component
44.Changing the Name of a Column in a JTable Component
45.Displaying an Icon in a Column Head of a JTable Component
46.Implementing Variable-Height Column Headers in a JTable Component
47.Removing the Column Headers from a Scrollable in a JTable Component
48.Creating a Custom Column Header Renderer in a JTable Component
49.Setting Column Header Tool Tips in a JTable Components
50.Setting Tool Tips on Cells in a JTable Component
51.Enable row selection (default) in a JTable
52.Enable column selection in a JTable
53.Enable cell selection in a JTable
54.Get default selection mode:MULTIPLE_INTERVAL_SELECTION
55.Allow only single a selection
56.Allow selection to span one contiguous set of rows, visible columns, or block of cells
57.Allow multiple selections of rows, visible columns, or cell blocks (default)
58.Select a column - column 0 in a JTable
59.Select an additional range of columns - columns 1 to 2
60.Deselect a range of columns - columns 0 to 1
61.Select a row - row 0
62.Select an additional range of rows - rows 1 to 2
63.Deselect a range of rows - rows 0 to 1
64.Select a cell: cell (2,1)
65.Extend the selection to include all cells (5,3)
66.Deselect a cell: cell (3,2), All cells in the row and column containing (3,2) are deselected.
67.Toggles the selection state, if it were called again, it exactly reverses the first call.
68.Select all cells
69.Deselect all cells
70.Column selection is enabled, get the indices of the selected columns
71.Row selection is enabled, Get the indices of the selected rows
72.Get selected row and selected index
73.Get the min and max ranges of selected cells
74.Check each cell in the min and max ranges of selected cells
75.Disabling Selections in a JTable Component
76.Getting the Anchor Cell in a JTable Component
77.Creating a Scrollable JTable Component
78.Disable auto resizing to make the table horizontal scrollable
79.Determining If a Cell Is Visible in a JTable Component
80.Making a Cell Visible in a JTable Component
81.Scrolling a Cell to the Center of a JTable Component
82.Show both horizontal and vertical grid lines (the default)
83.Don't show any grid lines
84.Show only vertical grid lines
85.Show only horizontal grid lines
86.Set the grid color
87.Getting the Gap Size Between Cells in a JTable Component
88.Add 5 spaces to the left and right sides of a cell.
89.Increase the row height
90.Programmatically Starting Cell Editing in a JTable Component
91.Save the current value in the cell being edited and stops the editing process
92.Discard any changes made by the user and stops the editing process
93.Disabling User Edits in a JTable
94.Disabling User Edits in a JTable with DefaultTableModel
95.Sorting the Rows in a JTable Component Based on a Column
96.Sorting a Column in a JTable Component
97.Listening for Selection Events in a JTable Component
98.Listening for Changes to the Rows and Columns of a JTable Component
99.Listening for Column-Related Changes in a JTable Component
100.Listening for Clicks on a Column Header in a JTable Component
101.Sorting and Filtering Tables
102.Use a regexFilter to filter table content
103.Scroll JTable To Center