Java Code Examples for javax.swing.table.JTableHeader#getDefaultRenderer()

The following examples show how to use javax.swing.table.JTableHeader#getDefaultRenderer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SkillPointTableModel.java    From pcgen with GNU Lesser General Public License v2.1 7 votes vote down vote up
public static void initializeTable(JTable table)
{
	table.setAutoCreateColumnsFromModel(false);
	JTableHeader header = table.getTableHeader();
	TableColumnModel columns = new DefaultTableColumnModel();
	TableCellRenderer headerRenderer = header.getDefaultRenderer();
	columns.addColumn(Utilities.createTableColumn(0, "in_level", headerRenderer, false));
	columns.addColumn(Utilities.createTableColumn(1, "in_class", headerRenderer, true));
	TableColumn remainCol = Utilities.createTableColumn(2, "in_iskRemain", headerRenderer, false);
	remainCol.setCellRenderer(new BoldNumberRenderer());
	columns.addColumn(remainCol);
	columns.addColumn(Utilities.createTableColumn(3, "in_gained", headerRenderer, false));
	table.setDefaultRenderer(Integer.class, new TableCellUtilities.AlignRenderer(SwingConstants.CENTER));
	table.setColumnModel(columns);
	table.setFocusable(false);
	header.setReorderingAllowed(false);
	header.setResizingAllowed(false);
}
 
Example 2
Source File: SortableTableModel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  TableColumnModel columnModel = h.getColumnModel();
  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  if (viewColumn < 0) {
    return;
  }
  TableCellRenderer tcr = h.getDefaultRenderer();
  int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1 && tcr instanceof SortButtonRenderer) {
    SortButtonRenderer sbr = (SortButtonRenderer) tcr;
    sbr.setPressedColumn(column);
    sbr.setSelectedColumn(column);
    h.repaint();
    JTable table = h.getTable();
    if (table.isEditing()) {
      table.getCellEditor().stopCellEditing();
    }
    SortableTableModel model = (SortableTableModel) table.getModel();
    model.sortByColumn(column, SortButtonRenderer.DOWN == sbr.getState(column));
  }
}
 
Example 3
Source File: ListTable.java    From consulo with Apache License 2.0 6 votes vote down vote up
public ListTable(ListWrappingTableModel tableModel) {
    super(tableModel);
    setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
    setRowSelectionAllowed(true);
    setDragEnabled(false);
    final JTableHeader header = getTableHeader();
    header.setReorderingAllowed(false);
    final TableCellRenderer delegate = header.getDefaultRenderer();
    final TableCellRenderer newRenderer = new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            final Component component = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            // to display the table header in disabled state when the table is disabled.
            component.setEnabled(table.isEnabled());
            return component;
        }
    };
    header.setDefaultRenderer(newRenderer);
    setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
 
Example 4
Source File: ClassLevelTableModel.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void initializeTable(JTable classLevelTable)
{
	JTableHeader tableHeader = classLevelTable.getTableHeader();
	tableHeader.setResizingAllowed(false);
	tableHeader.setReorderingAllowed(false);
	TableColumnModel columnModel = new DefaultTableColumnModel();
	TableCellRenderer headerRenderer = tableHeader.getDefaultRenderer();
	columnModel.addColumn(Utilities.createTableColumn(0, "Level", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(1, "HP", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(2, "Class (All Levels In Class)", headerRenderer, true));
	classLevelTable.setColumnModel(columnModel);
	classLevelTable.setAutoCreateColumnsFromModel(false);
	classLevelTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
	classLevelTable.setFocusable(false);
	classLevelTable.setCellSelectionEnabled(false);
	classLevelTable.setRowHeight(20);
}
 
Example 5
Source File: SkillPointTableModel.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void initializeTable(JTable table)
{
	table.setAutoCreateColumnsFromModel(false);
	JTableHeader header = table.getTableHeader();
	TableColumnModel columns = new DefaultTableColumnModel();
	TableCellRenderer headerRenderer = header.getDefaultRenderer();
	columns.addColumn(Utilities.createTableColumn(0, "in_level", headerRenderer, false));
	columns.addColumn(Utilities.createTableColumn(1, "in_class", headerRenderer, true));
	TableColumn remainCol = Utilities.createTableColumn(2, "in_iskRemain", headerRenderer, false);
	remainCol.setCellRenderer(new BoldNumberRenderer());
	columns.addColumn(remainCol);
	columns.addColumn(Utilities.createTableColumn(3, "in_gained", headerRenderer, false));
	table.setDefaultRenderer(Integer.class, new TableCellUtilities.AlignRenderer(SwingConstants.CENTER));
	table.setColumnModel(columns);
	table.setFocusable(false);
	header.setReorderingAllowed(false);
	header.setResizingAllowed(false);
}
 
Example 6
Source File: SortableTableModel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  TableColumnModel columnModel = h.getColumnModel();
  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  if (viewColumn < 0) {
    return;
  }
  TableCellRenderer tcr = h.getDefaultRenderer();
  int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1 && tcr instanceof SortButtonRenderer) {
    SortButtonRenderer sbr = (SortButtonRenderer) tcr;
    if (!sbr.isEnabledAt(column)) {
      return;
    }
    sbr.setPressedColumn(column);
    sbr.setSelectedColumn(column);
    h.repaint();
    JTable table = h.getTable();
    if (table.isEditing()) {
      table.getCellEditor().stopCellEditing();
    }
    SortableTableModel model = (SortableTableModel) table.getModel();
    model.sortByColumn(column, SortButtonRenderer.DOWN == sbr.getState(column));
  }
}
 
Example 7
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void actionPerformed(ActionEvent e) {
  if (table.isEditing()) {
    table.getCellEditor().stopCellEditing();
  }
  int row = table.getRowCount();
  if (row <= 0) {
    return;
  }
  RowDataModel model = (RowDataModel) table.getModel();
  RowDataModel nmodel = new RowDataModel();
  List<?> dv = model.getDataVector();
  for (int i = 0; i < row; i++) {
    nmodel.addRowData(makeRowData((List<?>) dv.get(i)));
  }
  JTableHeader h = table.getTableHeader();
  TableCellRenderer tcr = h.getDefaultRenderer();
  if (tcr instanceof SortButtonRenderer) {
    SortButtonRenderer sbr = (SortButtonRenderer) tcr;
    sbr.setPressedColumn(-1);
    sbr.setSelectedColumn(-1);
  }
  table.setAutoCreateColumnsFromModel(false);
  table.setModel(nmodel);
  table.clearSelection();
}
 
Example 8
Source File: SortableTableModel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JTableHeader h = (JTableHeader) e.getComponent();
  TableColumnModel columnModel = h.getColumnModel();
  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
  if (viewColumn < 0) {
    return;
  }
  TableCellRenderer tcr = h.getDefaultRenderer();
  int column = columnModel.getColumn(viewColumn).getModelIndex();
  if (column != -1 && tcr instanceof SortButtonRenderer) {
    SortButtonRenderer sbr = (SortButtonRenderer) tcr;
    sbr.setPressedColumn(column);
    sbr.setSelectedColumn(column);
    h.repaint();
    JTable table = h.getTable();
    if (table.isEditing()) {
      table.getCellEditor().stopCellEditing();
    }
    SortableTableModel model = (SortableTableModel) table.getModel();
    model.sortByColumn(column, SortButtonRenderer.DOWN == sbr.getState(column));
  }
}
 
Example 9
Source File: ClassLevelTableModel.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void initializeTable(JTable classLevelTable)
{
	JTableHeader tableHeader = classLevelTable.getTableHeader();
	tableHeader.setResizingAllowed(false);
	tableHeader.setReorderingAllowed(false);
	TableColumnModel columnModel = new DefaultTableColumnModel();
	TableCellRenderer headerRenderer = tableHeader.getDefaultRenderer();
	columnModel.addColumn(Utilities.createTableColumn(0, "Level", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(1, "HP", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(2, "Class (All Levels In Class)", headerRenderer, true));
	classLevelTable.setColumnModel(columnModel);
	classLevelTable.setAutoCreateColumnsFromModel(false);
	classLevelTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
	classLevelTable.setFocusable(false);
	classLevelTable.setCellSelectionEnabled(false);
	classLevelTable.setRowHeight(20);
}
 
Example 10
Source File: JTableHeaderItemJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public static Component getRendererComponent(JTableHeader jTableHeader, int item) {
    int columnCount = jTableHeader.getColumnModel().getColumnCount();
    if (item < 0 || item >= columnCount) {
        throw new NoSuchElementException("Index out of bounds error on JTableHeader: " + item, null);
    }
    Object headerValue = jTableHeader.getColumnModel().getColumn(item).getHeaderValue();
    TableCellRenderer headerRenderer = jTableHeader.getColumnModel().getColumn(item).getHeaderRenderer();
    if (headerRenderer == null) {
        headerRenderer = jTableHeader.getDefaultRenderer();
    }
    Component rendererComponent = headerRenderer.getTableCellRendererComponent(jTableHeader.getTable(), headerValue, false,
            false, 0, item);
    return rendererComponent;
}
 
Example 11
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
public Dimension getSize(JTableHeader header) {
  TableCellRenderer r = header.getDefaultRenderer();
  Component c = r.getTableCellRendererComponent(header.getTable(), getHeaderValue(), false, false, -1, -1);
  int width = 0;
  for (Object obj: list) {
    if (obj instanceof TableColumn) {
      TableColumn tc = (TableColumn) obj;
      width += tc.getWidth();
    } else {
      width += ((ColumnGroup) obj).getSize(header).width;
    }
  }
  return new Dimension(width, c.getPreferredSize().height);
}
 
Example 12
Source File: HeaderComponent.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
protected void paintComponent(Graphics g) {
    JTableHeader header = getHeader();
    setupHeader(header);
    TableCellRenderer renderer = header.getDefaultRenderer();
    JComponent component = (JComponent)renderer.getTableCellRendererComponent(
                           getTable(), "", isSelected && isPressed, isFocusOwner(), -1, 0); // NOI18N
    
    int height = header.getPreferredSize().height;
    component.setBounds(0, 0, getWidth(), height);
    component.setOpaque(false);
    getPainter().paintComponent(g, component, null, 0, 0, getWidth(), height, false);
}
 
Example 13
Source File: FreeColTableHeaderUI.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void installUI(JComponent c) {
    super.installUI(c);

    JTableHeader j = (JTableHeader) c;
    j.setOpaque(false);

    DefaultTableCellRenderer dtcr = (DefaultTableCellRenderer) j.getDefaultRenderer();
    dtcr.setOpaque(false);
}
 
Example 14
Source File: GameStatsJTable.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private void setDefaultColumnProperties() {
    final TableColumnModel cm = getColumnModel();
    // set initial column widths.
    for (int i = 0; i < cm.getColumnCount(); i++) {
        cm.getColumn(i).setMinWidth(50);
        cm.getColumn(i).setPreferredWidth(50);
    }
    final JTableHeader header = getTableHeader();
    header.setReorderingAllowed(true);
    final DefaultTableCellRenderer renderer =
            (DefaultTableCellRenderer) header.getDefaultRenderer();
    // center the column header captions.
    renderer.setHorizontalAlignment(SwingConstants.CENTER);
}
 
Example 15
Source File: CardsJTable.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private void setDefaultColumnProperties() {
    final TableColumnModel cm = getColumnModel();
    // set initial column widths.
    for (int i = 0; i < cm.getColumnCount(); i++) {
        cm.getColumn(i).setMinWidth(CardTableColumn.getMinWidth(i));
        cm.getColumn(i).setPreferredWidth(CardTableColumn.getMinWidth(i));
    }
    final JTableHeader header = getTableHeader();
    header.setReorderingAllowed(true);
    final DefaultTableCellRenderer renderer =
            (DefaultTableCellRenderer) header.getDefaultRenderer();
    // center the column header captions.
    renderer.setHorizontalAlignment(SwingConstants.CENTER);
}
 
Example 16
Source File: DeckGamesJTable.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private void setDefaultColumnProperties() {
    final TableColumnModel cm = getColumnModel();
    // set initial column widths.
    cm.getColumn(0).setMinWidth(30);
    cm.getColumn(1).setMinWidth(130);
    cm.getColumn(2).setMinWidth(170);
    cm.getColumn(3).setMinWidth(170);
    cm.getColumn(4).setMinWidth(180);
    final JTableHeader header = getTableHeader();
    header.setEnabled(false);
    final DefaultTableCellRenderer renderer =
            (DefaultTableCellRenderer) header.getDefaultRenderer();
    // center the column header captions.
    renderer.setHorizontalAlignment(SwingConstants.CENTER);
}
 
Example 17
Source File: TableViewPagePanel.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
void setModel(TableModel tableModel) {
    table.setModel(tableModel);
    if (table.getColumnCount() > 0) {
        final JTableHeader tableHeader = table.getTableHeader();
        final int margin = tableHeader.getColumnModel().getColumnMargin();
        final TableCellRenderer renderer = tableHeader.getDefaultRenderer();
        final Enumeration<TableColumn> columns = table.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn tableColumn = columns.nextElement();
            final int width = getColumnMinWith(tableColumn, renderer, margin);
            tableColumn.setMinWidth(width);
        }
    }
}
 
Example 18
Source File: HeaderComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void paintComponent(Graphics g) {
    JTableHeader header = getHeader();
    setupHeader(header);
    TableCellRenderer renderer = header.getDefaultRenderer();
    JComponent component = (JComponent)renderer.getTableCellRendererComponent(
                           getTable(), "", isSelected && isPressed, isFocusOwner(), -1, 0); // NOI18N
    
    int height = header.getPreferredSize().height;
    component.setBounds(0, 0, getWidth(), height);
    component.setOpaque(false);
    getPainter().paintComponent(g, component, null, 0, 0, getWidth(), height, false);
}
 
Example 19
Source File: TableDemo.java    From beautyeye with Apache License 2.0 4 votes vote down vote up
protected void initComponents() {
	setLayout(new BorderLayout());

	controlPanel = createControlPanel();
	add(controlPanel, BorderLayout.NORTH);

	// <snip>Create JTable
	oscarTable = new JTable(oscarModel);
	// </snip>

	// </snip>Set JTable display properties
	oscarTable.setColumnModel(createColumnModel());
	oscarTable.setAutoCreateRowSorter(true);
	oscarTable.setRowHeight(26);
	oscarTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
	oscarTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	oscarTable.setIntercellSpacing(new Dimension(0, 0));
	// </snip>

	// <snip>Initialize preferred size for table's viewable area
	Dimension viewSize = new Dimension();
	viewSize.width = oscarTable.getColumnModel().getTotalColumnWidth();
	viewSize.height = 10 * oscarTable.getRowHeight();
	oscarTable.setPreferredScrollableViewportSize(viewSize);
	// </snip>

	// <snip>Customize height and alignment of table header
	JTableHeader header = oscarTable.getTableHeader();
	header.setPreferredSize(new Dimension(30, 26));
	TableCellRenderer headerRenderer = header.getDefaultRenderer();
	if (headerRenderer instanceof JLabel) {
		((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
	}
	// </snip>

	JScrollPane scrollpane = new JScrollPane(oscarTable);
	dataPanel = new Stacker(scrollpane);
	add(dataPanel, BorderLayout.CENTER);

	add(createStatusBar(), BorderLayout.SOUTH);

}
 
Example 20
Source File: TableDemo.java    From littleluck with Apache License 2.0 4 votes vote down vote up
protected void initComponents() {
    setLayout(new BorderLayout());

    controlPanel = createControlPanel();
    add(controlPanel, BorderLayout.NORTH);

    //<snip>Create JTable
    oscarTable = new JTable(oscarModel);
    //</snip>

    //</snip>Set JTable display properties
    oscarTable.setColumnModel(createColumnModel());
    oscarTable.setAutoCreateRowSorter(true);
    oscarTable.setRowHeight(26);
    oscarTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
    oscarTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    oscarTable.setIntercellSpacing(new Dimension(0, 0));
    //</snip>

    //<snip>Initialize preferred size for table's viewable area
    Dimension viewSize = new Dimension();
    viewSize.width = oscarTable.getColumnModel().getTotalColumnWidth();
    viewSize.height = 10 * oscarTable.getRowHeight();
    oscarTable.setPreferredScrollableViewportSize(viewSize);
    //</snip>

    //<snip>Customize height and alignment of table header
    JTableHeader header = oscarTable.getTableHeader();
    header.setPreferredSize(new Dimension(30, 26));
    TableCellRenderer headerRenderer = header.getDefaultRenderer();
    if (headerRenderer instanceof JLabel) {
        ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
    }
    //</snip>

    LuckScrollPane scrollpane = new LuckScrollPane(oscarTable);
    dataPanel = new Stacker(scrollpane);
    add(dataPanel, BorderLayout.CENTER);

    add(createStatusBar(), BorderLayout.SOUTH);

}