JTable Right Align Header
Here's an alternate approach to modifying the TableCellRenderer
of a table's JTableHeader
. It's not strictly necessary for this usage, but it minimizes the impact on the UI delegate's appearance.
Typical usage:
JTable table = new JTable(…);
JTableHeader header = table.getTableHeader();
header.setDefaultRenderer(new HeaderRenderer(table));
Custom header renderer:
private static class HeaderRenderer implements TableCellRenderer {
DefaultTableCellRenderer renderer;
public HeaderRenderer(JTable table) {
renderer = (DefaultTableCellRenderer)
table.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
return renderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
}
}
How to left or right justify a column header in a JTable
TableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumn(id).setHeaderRenderer(renderer);
Or, if you don't have the column names available:
table.getColumnModel().getColumn(index).setHeaderRenderer(renderer);
JTable Column Headers with wrapped and center aligned text
In your table model class use html of column name
example:
"<html><center>First column</html>"
Set JTable header horizontal alignment based on value renderer column alignment
Here is an example of a custom header renderer that simply makes the text of the selected column Bold:
class MyTableHeaderRenderer implements TableCellRenderer
{
private TableCellRenderer tableCellRenderer;
public MyTableHeaderRenderer(TableCellRenderer tableCellRenderer)
{
this.tableCellRenderer = tableCellRenderer;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = tableCellRenderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
if (column == table.getSelectedColumn())
c.setFont(getFont().deriveFont(Font.BOLD));
return c;
}
}
You would use the renderer with code like:
JTableHeader header = table.getTableHeader();
DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer)header.getDefaultRenderer();
header.setDefaultRenderer( new MyTableHeaderRenderer(defaultRenderer) );
In your case you would need to change the alignment, not the Font.
You might use code like the following:
TableCellRenderer tcr = table.getCellRenderer(0, column);
Component renderer = table.prepareRenderer(tcr, 0, column);
defaultRenderer.setAlignment( renderer.getAlignment() ); // whatever the alignment method is.
Edit:
Somehow what I've done eliminates the normal formatting for a header
You are using:
tableColumn.getCellRenderer();
should that not be:
tableColumn.getHeaderRenderer();
Rarely would people specify a special renderer for the table header by column. So if a header renderer is not specified for the TableColumn you should just use the default renderer of the JTableHeader, don't create a DefaultTableCellRenderer, this is NOT the renderer used by the table header.
JTable header alignment
one of ways is to set Renderer
, TableHeader
by default returns JLabel
, for example
final TableCellRenderer tcrOs = myTable.getTableHeader().getDefaultRenderer();
myTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
JLabel lbl = (JLabel) tcrOs.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
lbl.setForeground(AppVariables.textColor);
lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(),
BorderFactory.createEmptyBorder(0, 5, 0, 0)));
lbl.setHorizontalAlignment(SwingConstants.LEFT);
if (isSelected) {
lbl.setForeground(Color.red);
lbl.setBackground(Color.lightGray);
} else {
lbl.setForeground(Color.blue);
lbl.setBackground(Color.black);
}
return lbl;
}
});
Align text in a table header
Try:
text-align: center;
You may be familiar with the HTML align attribute (which has been discontinued as of HTML 5). The align attribute could be used with tags such as
<table>, <td>, and <img>
to specify the alignment of these elements. This attribute allowed you to align elements horizontally. HTML also has/had a valign attribute for aligning elements vertically. This has also been discontinued from HTML5.
These attributes were discontinued in favor of using CSS to set the alignment of HTML elements.
There isn't actually a CSS align or CSS valign property. Instead, CSS has the text-align which applies to inline content of block-level elements, and vertical-align property which applies to inline level and table cells.
Align table header (mat-header-cell) right
You use mat-text-column which supports alignment.
<mat-text-column [name]="column" *ngFor="let column of columnsToDisplay" justify="right">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{column}} </th>
<td mat-cell *matCellDef="let element" align="right"> {{element[column]}} </td>
</mat-text-column>
https://stackblitz.com/edit/angular-mnqztk-kwajho?file=src%2Fapp%2Ftable-expandable-rows-example.html
Here is documentation link
https://material.angular.io/components/table/api#MatTextColumn
how do you left-align text in JTableHeader?
Your code affects where the table header itself is positioned, not the items within each header. You need to call getTableHeader().setDefaultRenderer(xxx)
with something that left-aligns the types you care about.
Related Topics
Installing Java on Os X 10.9 (Mavericks)
Why Are Interface Method Invocations Slower Than Concrete Invocations
Things Possible in Intellij That Aren't Possible in Eclipse
Hibernate - Foreign Keys Instead of Entities
What's the Purpose of Meta-Inf
Download Attachments Using Java Mail
Differencebetween Canonical Name, Simple Name and Class Name in Java Class
How to Enable Anti Aliasing in Arbitrary Java Apps
How to Deal with the Urisyntaxexception
Differencebetween a Hashmap and a Treemap
How to Reference a Method in Javadoc
Which Loop Has Better Performance? Why
Making a Log4J Console Appender Use Different Colors for Different Threads
How to Configure Hibernate Config File for SQL Server
Matrix Multiplication in Apache Spark