Surfing in the web, I found the way for using a JTextPane into a cell from JTable, only this way I can show the text centered, however, when I added a JScrollPane for showing a scrollbar when the text is longer than the cell height, the scrollbar is shown into a cell but it doesn't move only is shown but doesn't move.
I'm using org.jdesktop.swingx.renderer.DefaultTableRenderer of swingx framework, here part of my code:
//personal editor used in the JTextPane into cell, I found it in the web
class MyEditorKit extends StyledEditorKit {
private StyledViewFactory estilo;
private int maxTextHeight=0;
public ViewFactory getViewFactory() {
estilo = new StyledViewFactory();
maxTextHeight = estilo.getVista();
return estilo;
}
public int getAltura(){
return maxTextHeight;
}
static class StyledViewFactory implements ViewFactory {
private static View vista;
private static int altura;
public View create(Element elem) {
String kind = elem.getName();
String y = "";
y = elem.getDocument().getLength()+"";
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
vista = new LabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
vista = new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
vista = new CenteredBoxView(elem, View.Y_AXIS);
int x = vista.getDocument().getLength();
} else if (kind.equals(StyleConstants.ComponentElementName)) {
vista = new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
vista = new IconView(elem);
}
return vista;
}
return new LabelView(elem);
}
public int getVista(){//It's not important so ignore it
if( vista instanceof CenteredBoxView)
return altura = ( (CenteredBoxView)vista ).getTextHeight();
return 0;
}
}
}
// Class which center the text into a cell
class CenteredBoxView extends BoxView {
private int maxTextHeight=0;
public CenteredBoxView(Element elem, int axis) {
super(elem,axis);
}
protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
super.layoutMajorAxis(targetSpan,axis,offsets,spans);
int textBlockHeight = 0;
int offset = 0;
for (int i = 0; i < spans.length; i++) {
textBlockHeight = spans[i];
maxTextHeight=(textBlockHeight>maxTextHeight)?textBlockHeight:maxTextHeight;
}
offset = (targetSpan - textBlockHeight) / 2;
for (int i = 0; i < offsets.length; i++) {
offsets[i] += offset;
}
}
public int getTextHeight(){//It's not important so ignore it
return maxTextHeight;
}
}
//class that generate a JScrollPane
class TextAreaProvider extends ComponentProvider<JScrollPane> {
private JTextPane area;
private JScrollPane pane;
@Override
protected void configureState(CellContext context) {
}
@Override
protected JScrollPane createRendererComponent() {
area = new JTextPane();
area.setOpaque(true);
area.setEditorKit(new MyEditorKit());
StyledDocument doc = area.getStyledDocument();
MutableAttributeSet center = new SimpleAttributeSet();
StyleConstants.setBackground(center, Color.BLUE);
//StyleConstants.setBold(center, true);
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
//area.setBorder(BorderFactory.createLineBorder(Color.BLACK));
int x = ((MyEditorKit)area.getEditorKit()).getAltura();
pane = new JScrollPane(area);
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
return pane;
}
@Override
protected void format(CellContext context) {//Here is the format which will be shown in the cell
area.setText(getValueAsString(context));
}
}
}
I just put the code tabla.getColumnModel().getColumn(col).setCellRenderer( new DefaultTableRenderer( new TextAreaProvider() ) )
for generating a cell with a JScrollPane.
I'm tired of try a lot of things but I can't make that it works. I need a solution please!.