1

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());

enter image description here

Elio
  • 148
  • 9

2 Answers2

2

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.

Community
  • 1
  • 1
TT.
  • 15,774
  • 6
  • 47
  • 88
  • This will only work for a JTextPane, not a JTextArea. – camickr Jan 21 '16 at 20:36
  • @camickr Hmz good point =) so back to setAlignmentY I suppose – TT. Jan 21 '16 at 20:39
  • @camickr You got EXP in things like these looking at your user profile... Do you think JTextArea would be a good component to extend from, or would he be better off extending JTextPane? – TT. Jan 21 '16 at 20:43
  • Not sure I really understand what the OP is trying to do so I don't have a suggestion. – camickr Jan 21 '16 at 20:48
  • setAlignmentY does not work (neither does setAlignmentX) – Elio Jan 22 '16 at 08:49
  • Have you considered using another control, like JTextPane? I think JTextArea is just too primitive to allow for setting alignment. Try it out. If you think you can use JTextPane rather than JTextArea, I will revert my answer to the previous (which has a solution to set alignment for it). – TT. Jan 22 '16 at 08:57
  • Look at [this thread on SO](http://stackoverflow.com/q/3213045/243373) which also suggests that it is not possible with a JTextArea. Consider moving to JTextPane. It is a little more involved than JTextArea but at least you can set alignment on it. – TT. Jan 22 '16 at 09:43
  • You are quite welcome. Did you end up using JTextPane then? – TT. Jan 22 '16 at 14:21
  • Yes, I've written the solution down. – Elio Jan 22 '16 at 15:03
  • @Elio answer rollbacked, and provided some text saying you can't do it with JTextArea. GL! – TT. Jan 22 '16 at 15:06
2

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;
}
}
Elio
  • 148
  • 9