Java Code Examples for javax.swing.table.TableColumn#getHeaderValue()

The following examples show how to use javax.swing.table.TableColumn#getHeaderValue() . 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: TableColumnAdjuster.java    From magarena with GNU General Public License v3.0 6 votes vote down vote up
private int getColumnHeaderWidth(int column)
{
    if (! isColumnHeaderIncluded) return 0;

    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    Object value = tableColumn.getHeaderValue();
    TableCellRenderer renderer = tableColumn.getHeaderRenderer();

    if (renderer == null)
    {
        renderer = table.getTableHeader().getDefaultRenderer();
    }

    Component c = renderer.getTableCellRendererComponent(table, value, false, false, -1, column);
    return c.getPreferredSize().width;
}
 
Example 2
Source File: HeaderCheckBoxHandler.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mouseClicked(MouseEvent e) {
  JTableHeader header = (JTableHeader) e.getComponent();
  JTable tbl = header.getTable();
  TableColumnModel columnModel = tbl.getColumnModel();
  TableModel m = tbl.getModel();
  int vci = columnModel.getColumnIndexAtX(e.getX());
  int mci = tbl.convertColumnIndexToModel(vci);
  if (mci == targetColumnIndex && m.getRowCount() > 0) {
    TableColumn column = columnModel.getColumn(vci);
    boolean b = column.getHeaderValue() == Status.DESELECTED;
    for (int i = 0; i < m.getRowCount(); i++) {
      m.setValueAt(b, i, mci);
    }
    column.setHeaderValue(b ? Status.SELECTED : Status.DESELECTED);
    // header.repaint();
  }
}
 
Example 3
Source File: HeaderCheckBoxHandler.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mouseClicked(MouseEvent e) {
  JTableHeader header = (JTableHeader) e.getComponent();
  JTable tbl = header.getTable();
  TableColumnModel columnModel = tbl.getColumnModel();
  TableModel m = tbl.getModel();
  int vci = columnModel.getColumnIndexAtX(e.getX());
  int mci = tbl.convertColumnIndexToModel(vci);
  if (mci == targetColumnIndex && m.getRowCount() > 0) {
    TableColumn column = columnModel.getColumn(vci);
    boolean b = column.getHeaderValue() == Status.DESELECTED;
    for (int i = 0; i < m.getRowCount(); i++) {
      m.setValueAt(b, i, mci);
    }
    column.setHeaderValue(b ? Status.SELECTED : Status.DESELECTED);
    // header.repaint();
  }
}
 
Example 4
Source File: HeaderCheckBoxHandler.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void tableChanged(TableModelEvent e) {
  int vci = table.convertColumnIndexToView(targetColumnIndex);
  TableColumn column = table.getColumnModel().getColumn(vci);
  Object status = column.getHeaderValue();
  TableModel m = table.getModel();
  boolean repaint = false;
  if (e.getType() == TableModelEvent.DELETE) {
    // System.out.println("DELETE");
    // System.out.println(status + ":   " + Status.INDETERMINATE.equals(status));
    repaint = fireDeleteEvent(m, column, status);
  } else if (e.getType() == TableModelEvent.INSERT && status != Status.INDETERMINATE) {
    // System.out.println("INSERT");
    repaint = fireInsertEvent(m, column, status, e);
  } else if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == targetColumnIndex) {
    // System.out.println("UPDATE");
    repaint = fireUpdateEvent(m, column, status);
  }
  if (repaint) {
    JTableHeader h = table.getTableHeader();
    h.repaint(h.getHeaderRect(vci));
  }
}
 
Example 5
Source File: TableColumnAdjuster.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private int getColumnHeaderWidth(int column) {
	if (!isColumnHeaderIncluded) {
		return 0;
	}

	TableColumn tableColumn = table.getColumnModel().getColumn(column);
	Object value = tableColumn.getHeaderValue();
	TableCellRenderer renderer = tableColumn.getHeaderRenderer();

	if (renderer == null) {
		renderer = table.getTableHeader().getDefaultRenderer();
	}

	Component c = renderer.getTableCellRendererComponent(table, value, false, false, -1, column);
	return c.getPreferredSize().width;
}
 
Example 6
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void mouseClicked(MouseEvent e) {
  JTableHeader header = (JTableHeader) e.getComponent();
  JTable tbl = header.getTable();
  TableColumnModel columnModel = tbl.getColumnModel();
  TableModel m = tbl.getModel();
  int vci = columnModel.getColumnIndexAtX(e.getX());
  int mci = tbl.convertColumnIndexToModel(vci);
  if (mci == targetColumnIndex && m.getRowCount() > 0) {
    TableColumn column = columnModel.getColumn(vci);
    boolean b = column.getHeaderValue() == Status.DESELECTED;
    for (int i = 0; i < m.getRowCount(); i++) {
      m.setValueAt(b, i, mci);
    }
    column.setHeaderValue(b ? Status.SELECTED : Status.DESELECTED);
    // header.repaint();
  }
}
 
Example 7
Source File: MBasicTable.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Adapte les largeurs des colonnes selon les données de cette table. <br/>
 * Pour chaque colonne la taille préférée est déterminée selon la valeur (et le renderer) du header et selon la valeur (et le renderer) de chaque cellule.
 */
public void adjustColumnWidths() {
	if (ADJUST_COLUMN_WIDTHS_MAX_ROWS > 0) {
		TableColumn tableColumn;
		TableCellRenderer renderer;
		Object value;
		Component component;
		final int columnCount = getColumnCount();
		final int rowCount = Math.min(getRowCount(), ADJUST_COLUMN_WIDTHS_MAX_ROWS);
		int columnWidth;
		final int maxWidth = 250; // taille ajustée maximum (15 minimum par défaut)

		// Boucle sur chaque colonne et chaque ligne.
		// Trouve le max de la largeur du header et de chaque cellule
		// et fixe la largeur de la colonne en fonction.
		for (int colNum = 0; colNum < columnCount; colNum++) {
			tableColumn = columnModel.getColumn(colNum);
			if (tableColumn.getMaxWidth() <= 0) {
				continue; // colonne invisible
			}

			renderer = getTableHeader().getDefaultRenderer();
			value = tableColumn.getHeaderValue();
			component = renderer.getTableCellRendererComponent(this, value, false, false, -1,
					colNum);
			columnWidth = component.getPreferredSize().width + 10;
			renderer = getCellRenderer(-1, colNum);

			for (int rowNum = 0; rowNum < rowCount; rowNum++) {
				value = getValueAt(rowNum, colNum);
				component = renderer.getTableCellRendererComponent(this, value, false, false,
						rowNum, colNum);
				columnWidth = Math.max(columnWidth, component.getPreferredSize().width);
			}
			columnWidth = Math.min(maxWidth, columnWidth);

			tableColumn.setPreferredWidth(columnWidth + getIntercellSpacing().width);
		}
	}
}
 
Example 8
Source File: HeaderCheckBoxHandler.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void tableChanged(TableModelEvent e) {
  if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == targetColumnIndex) {
    int vci = table.convertColumnIndexToView(targetColumnIndex);
    TableColumn column = table.getColumnModel().getColumn(vci);
    Object status = column.getHeaderValue();
    TableModel m = table.getModel();
    if (m instanceof DefaultTableModel && fireUpdateEvent((DefaultTableModel) m, column, status)) {
      JTableHeader h = table.getTableHeader();
      h.repaint(h.getHeaderRect(vci));
    }
  }
}
 
Example 9
Source File: ListEmployeePane.java    From OpERP with MIT License 5 votes vote down vote up
@Override
public void updateEmployees(List<Employee> employees) {
	tableModel = null;
	tableModel = new BeanTableModel<>(Employee.class, employees);

	for (int i = 0; i < table.getColumnCount(); i++) {
		tableModel.setColumnEditable(i, false);
	}
	tableModel.setModelEditable(false);
	table.setModel(tableModel);

	JTableHeader tableHeader = table.getTableHeader();
	Enumeration<TableColumn> columns = tableHeader.getColumnModel()
			.getColumns();
	while (columns.hasMoreElements()) {
		final TableColumn column = columns.nextElement();
		String columnName = (String) column.getHeaderValue();
		if (columnName.equalsIgnoreCase("Account")
				|| columnName.equalsIgnoreCase("Contact Info")) {

			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					table.removeColumn(column);
				}
			});
		}

	}
}
 
Example 10
Source File: ListVendorPane.java    From OpERP with MIT License 5 votes vote down vote up
@Override
public void updateVendors(List<Vendor> vendors) {
	tableModel = null;
	tableModel = new BeanTableModel<>(Vendor.class, Party.class, vendors);

	for (int i = 0; i < table.getColumnCount(); i++) {
		tableModel.setColumnEditable(i, false);
	}
	tableModel.setModelEditable(false);
	table.setModel(tableModel);

	JTableHeader tableHeader = table.getTableHeader();
	Enumeration<TableColumn> columns = tableHeader.getColumnModel()
			.getColumns();
	while (columns.hasMoreElements()) {
		final TableColumn column = columns.nextElement();
		String columnName = (String) column.getHeaderValue();
		if (columnName.equalsIgnoreCase("Contact Info")) {

			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					table.removeColumn(column);
				}
			});
		}

	}
}
 
Example 11
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void tableChanged(TableModelEvent e) {
  if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == targetColumnIndex) {
    int vci = table.convertColumnIndexToView(targetColumnIndex);
    TableColumn column = table.getColumnModel().getColumn(vci);
    Object status = column.getHeaderValue();
    TableModel m = table.getModel();
    if (m instanceof DefaultTableModel && fireUpdateEvent((DefaultTableModel) m, column, status)) {
      JTableHeader h = table.getTableHeader();
      h.repaint(h.getHeaderRect(vci));
    }
  }
}
 
Example 12
Source File: ListPurchasePane.java    From OpERP with MIT License 5 votes vote down vote up
@Override
public void updatePurchases(List<Purchase> purchases) {
	tableModel = null;
	tableModel = new BeanTableModel<>(Purchase.class, Business.class,
			purchases);

	for (int i = 0; i < table.getColumnCount(); i++) {
		tableModel.setColumnEditable(i, false);
	}
	tableModel.setModelEditable(false);
	table.setModel(tableModel);

	JTableHeader tableHeader = table.getTableHeader();
	Enumeration<TableColumn> columns = tableHeader.getColumnModel()
			.getColumns();
	while (columns.hasMoreElements()) {
		final TableColumn column = columns.nextElement();
		String columnName = (String) column.getHeaderValue();
		if (columnName.equalsIgnoreCase("Business Desc")) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					table.removeColumn(column);
				}
			});
		}

	}
}
 
Example 13
Source File: EditableHeaderTableColumn.java    From chipster with MIT License 5 votes vote down vote up
public void copyValues(TableColumn base) {    
	modelIndex     = base.getModelIndex();
	identifier     = base.getIdentifier();
	width          = base.getWidth();
	minWidth       = base.getMinWidth();
	setPreferredWidth(base.getPreferredWidth());
	maxWidth       = base.getMaxWidth();
	headerRenderer = base.getHeaderRenderer();
	headerValue    = base.getHeaderValue();
	cellRenderer   = base.getCellRenderer();
	cellEditor     = base.getCellEditor();
	isResizable    = base.getResizable();
}
 
Example 14
Source File: ListStockActivitiesPane.java    From OpERP with MIT License 5 votes vote down vote up
@Override
public void updateStockActivities(List<StockKeeper> stockActivities) {
	tableModel = null;
	tableModel = new BeanTableModel<>(StockKeeper.class, stockActivities);

	for (int i = 0; i < table.getColumnCount(); i++) {
		tableModel.setColumnEditable(i, false);
	}
	tableModel.setModelEditable(false);
	table.setModel(tableModel);

	JTableHeader tableHeader = table.getTableHeader();
	Enumeration<TableColumn> columns = tableHeader.getColumnModel()
			.getColumns();
	while (columns.hasMoreElements()) {
		final TableColumn column = columns.nextElement();
		String columnName = (String) column.getHeaderValue();
		if (columnName.equalsIgnoreCase("Stock Keeper Id")
				|| columnName.equalsIgnoreCase("Stock")
				|| columnName.equalsIgnoreCase("Transfer Stock Keeper")) {

			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					table.removeColumn(column);
				}
			});
		}

	}
}
 
Example 15
Source File: TableColumnManager.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
private void showPopup(int index)
{
	Object headerValue = tcm.getColumn( index ).getHeaderValue();
	int columnCount = tcm.getColumnCount();
	JPopupMenu popup = new SelectPopupMenu();

	//  Create a menu item for all columns managed by the table column
	//  manager, checking to see if the column is shown or hidden.

	for (TableColumn tableColumn : allColumns)
	{
		Object value = tableColumn.getHeaderValue();
		JCheckBoxMenuItem item = new JCheckBoxMenuItem( value.toString() );
		item.addActionListener( this );

		try
		{
			tcm.getColumnIndex( value );
			item.setSelected( true );

			if (columnCount == 1)
				item.setEnabled( false );
		}
		catch(IllegalArgumentException e)
		{
			item.setSelected( false );
		}

		popup.add( item );

		if (value == headerValue)
			popup.setSelected( item );
	}

	//  Display the popup below the TableHeader

	JTableHeader header = table.getTableHeader();
	Rectangle r = header.getHeaderRect( index );
	popup.show(header, r.x, r.height);
}
 
Example 16
Source File: XTableHeader.java    From scelight with Apache License 2.0 5 votes vote down vote up
/**
 * Overridden to return header cell value as tool tip if it's truncated (due to not fitting into its cell's area).
 */
@Override
public String getToolTipText( final MouseEvent event ) {
	// First check if super implementation returns a tool tip:
	// (super implementation checks if a tool tip is set on the renderer for the cell,
	// and if no, also checks if a tool tip has been set on the table itself)
	final String tip = super.getToolTipText( event );
	if ( tip != null )
		return tip;
	
	// No tool tip from the renderer, and no own tool tip has been set.
	// So let's check if value doesn't fit into the cell, and if so provide it's full text as the tool tip
	final Point point = event.getPoint();
	
	final int column = columnAtPoint( point );
	
	// Check if value is truncated (doesn't fit into the cell):
	if ( column < 0 ) // event is not on a cell
		return null;
	
	// Check if value is truncated (doesn't fit into the cell):
	final TableColumn tableColumn = getColumnModel().getColumn( column );
	TableCellRenderer renderer = tableColumn.getHeaderRenderer();
	if ( renderer == null )
		renderer = getDefaultRenderer();
	final Object value = tableColumn.getHeaderValue();
	if ( value == null )
		return null;
	if ( getHeaderRect( column ).width >= renderer.getTableCellRendererComponent( getTable(), value, false, false, 0, column ).getPreferredSize().width )
		return null; // Value fits in the cell, no need to show tool tip
		
	// At this point no one provided tool tip, and value is rendered truncated. Provide its full text as the tool tip.
	return value.toString();
}
 
Example 17
Source File: TableCheckBoxColumn.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
@Override
public void tableChanged(TableModelEvent e) {
    if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == 0) {
        int mci = 0;
        int vci = table.convertColumnIndexToView(mci);
        TableColumn column = table.getColumnModel().getColumn(vci);
        Object title = column.getHeaderValue();
        if (!Status.INDETERMINATE.equals(title)) {
            column.setHeaderValue(Status.INDETERMINATE);
        } else {
            int selected = 0, deselected = 0;
            TableModel m = table.getModel();
            for (int i = 0; i < m.getRowCount(); i++) {
                if (Boolean.TRUE.equals(m.getValueAt(i, mci))) {
                    selected++;
                } else {
                    deselected++;
                }
            }
            if (selected == 0) {
                column.setHeaderValue(Status.DESELECTED);
            } else if (deselected == 0) {
                column.setHeaderValue(Status.SELECTED);
            } else {
                return;
            }
        }
        table.getTableHeader().repaint();
    }
}
 
Example 18
Source File: WarehouseDetailsPane.java    From OpERP with MIT License 5 votes vote down vote up
@Override
public void updateStock(List<Stock> stocks) {

	List<Stock> warehouseStock = new LinkedList<>();
	for (Stock stock : stocks) {
		if (stock.getWarehouse().compareTo(getEntity()) == 0) {
			warehouseStock.add(stock);
		}
	}

	tableModel = null;
	tableModel = new BeanTableModel<>(Stock.class, warehouseStock);

	for (int i = 0; i < table.getColumnCount(); i++) {
		tableModel.setColumnEditable(i, false);
	}
	tableModel.setModelEditable(false);
	table.setModel(tableModel);

	JTableHeader tableHeader = table.getTableHeader();
	Enumeration<TableColumn> columns = tableHeader.getColumnModel()
			.getColumns();
	while (columns.hasMoreElements()) {
		final TableColumn column = columns.nextElement();
		String columnName = (String) column.getHeaderValue();
		if (columnName.equalsIgnoreCase("Stock Id")
				|| columnName.equalsIgnoreCase("Warehouse")) {

			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					table.removeColumn(column);
				}
			});
		}

	}
}
 
Example 19
Source File: DataToolPropsTable.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
public void addColumn(TableColumn aColumn) {
  int modelColumn = aColumn.getModelIndex();
  if(aColumn.getHeaderValue()==null) {
    String columnName = getModel().getColumnName(modelColumn);
    aColumn.setHeaderValue(columnName);
  }
  // workaround to prevent adding multiple columns with same model index
  for (int i = 0; i < getColumnModel().getColumnCount(); i++) {
  	int index = getColumnModel().getColumn(i).getModelIndex();
 	if (index==modelColumn) return;
  }
  getColumnModel().addColumn(aColumn);
}
 
Example 20
Source File: TableViewPagePanel.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
private int getColumnMinWith(TableColumn column, TableCellRenderer renderer, int margin) {
    final Object headerValue = column.getHeaderValue();
    final JLabel label = (JLabel) renderer.getTableCellRendererComponent(table, headerValue, false, false, 0, 0);

    return label.getPreferredSize().width + margin;
}