Reputation: 148
I have the following TableCellRenderer
(NOT DefaultTableCellRenderer
) which allows me to skip lines in my JTable
's cells:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
public class MyCellRenderer extends JTextArea implements TableCellRenderer {
Color highlightBackground = (Color) UIManager.get("Table.selectionBackground");
public MyCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText((String) value); //or something in value, like value.getNote()...
setSize(table.getColumnModel().getColumn(column).getWidth(), getPreferredSize().height);
if (table.getRowHeight(row) != getPreferredSize().height) {
table.setRowHeight(row, getPreferredSize().height);
}
if (table.isRowSelected(row)) {
this.setBackground(highlightBackground);
} else {
this.setBackground(Color.WHITE);
}
return this;
}
}
I would just like to change the horizontal alignment of this renderer. (Meaning the cells would have the text in their center or right, right now it's left).
Here's how I use this TableCellRenderer :
Table.getColumnModel().getColumn(1).setCellRenderer(new MyCellRenderer());
Upvotes: 1
Views: 834
Reputation: 16137
There is no way to do this with JTextArea.
Your cell renderer should be extending JTextPane instead, so you could call from your getTableCellRendererComponent
implementation or the constructor of the renderer:
StyledDocument doc = getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
If you wanted to center. Shamelessly plugged from this answer on StackOverflow.
Upvotes: 2
Reputation: 148
I Have found the answer, thanks TT. for the hints, I simply removed the custom constructor and its linewrap, extended JTextPane, and added a few lines of code. For anyone who needs a class that allows skipping lines & setting horizontal alignment of cells, here's the code:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class MyCellRenderer extends JTextPane implements TableCellRenderer {
Color highlightBackground = (Color) UIManager.get("Table.selectionBackground");
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText((String) value);//or something in value, like value.getNote()...
setSize(table.getColumnModel().getColumn(column).getWidth(),
getPreferredSize().height);
if (table.getRowHeight(row) != getPreferredSize().height) {
table.setRowHeight(row, getPreferredSize().height);
}
if (table.isRowSelected(row)) {
this.setBackground(highlightBackground);
} else {
this.setBackground(Color.WHITE);
}
//Added lines
StyledDocument doc = this.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
//Added Lines
return this;
}
}
Upvotes: 2