Results 1 to 4 of 4
- 01-09-2012, 04:15 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
How to horizontal align multi-line Text in a JTable-Cell
Hi everyone,
i'm currently trying to accomplish a multi-line column with different horizontal alignment within a JTable. I managed to implement the multi-line with a JTextArea as the CellRenderer but unfortunately the JTextArea doesn't have a (working) setHorizontalAlignment method (setAlignmentX(..) doesn't work either).
Here's a Screenshot and below the current code (The Text in Columns 4 and 5 contain \n for the line break):

Code of my TableCellRenderer:
How can i implement an additional horizontal alignment? Many thanks in advance and best regardsJava Code:public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { this.setText(value.toString()); this.setWrapStyleWord(true); this.setLineWrap(true); System.out.println(value.toString()); // current table column width in pixels int colWidth = table.getColumnModel().getColumn(column).getWidth(); // set the text area width (height doesn't matter here) setSize(new Dimension(colWidth, 1)); // get the text area preferred height and add the row margin int height = getPreferredSize().height + table.getRowMargin(); // ensure the row height fits the cell with most lines if (height != table.getRowHeight(row)) { table.setRowHeight(row, height); rowHeight = height; } return this; }
Hotkey
- 01-09-2012, 06:27 PM #2
Re: How to horizontal align multi-line Text in a JTable-Cell
A JTextArea is meant for unstyled text. For your requirement you need to use a JTextPane and apply appropriate style(s)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-10-2012, 08:46 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: How to horizontal align multi-line Text in a JTable-Cell
Thanks for the hint. But in JTextPane im Missing the setLineWrap(true) and setWrapStyleWord(true) Functions. How do i accomplish that with JTextPane?
- 01-10-2012, 10:33 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: How to horizontal align multi-line Text in a JTable-Cell
Finally i managed it. Here's my solution of a Renderer with JTextPane:
Java Code:@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { this.setText(value.toString()); StyledDocument doc = this.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, alignment); //alignment to be Set in Constructor (ex. StyleConstants.ALIGN_RIGHT) doc.setParagraphAttributes(0, doc.getLength(), center, false); this.setDocument(doc); int height = getPreferredSize().height + table.getRowMargin(); if (height != table.getRowHeight(row)) { table.setRowHeight(row, height); } return this; }
Last thing: I've added the if (height != table.getRowHeight(row)) {...} because otherwise it seems that the method is called in an endless loop (maybe caused by the table.setRowHeight(...)). Although this works now: is there a general fault in my approach?
Similar Threads
-
Align Row to Right in JTable
By shomid in forum AWT / SwingReplies: 2Last Post: 11-25-2011, 10:23 PM -
Render Text Format & Align Cell Border while Exporting PPT Files
By sherazam in forum Java SoftwareReplies: 0Last Post: 01-31-2011, 09:08 AM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
Right Align columns in JTable
By Laura Warren in forum New To JavaReplies: 2Last Post: 12-18-2008, 09:01 PM -
Multi-line Tooltip inside a JTable's Cell
By stevenc49 in forum AWT / SwingReplies: 2Last Post: 06-29-2008, 04:15 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks