Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Hotkey

Thread: How to horizontal align multi-line Text in a JTable-Cell

  1. #1
    Hotkey is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default 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):
    How to horizontal align multi-line Text in a JTable-Cell-tableexample.jpg

    Code of my TableCellRenderer:
    Java 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;
        }
    How can i implement an additional horizontal alignment? Many thanks in advance and best regards

    Hotkey

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default 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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Hotkey is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default 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?

  4. #4
    Hotkey is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default 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;
        }
    How to horizontal align multi-line Text in a JTable-Cell-table2p.jpg

    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?
    DarrylBurke likes this.

Similar Threads

  1. Align Row to Right in JTable
    By shomid in forum AWT / Swing
    Replies: 2
    Last Post: 11-25-2011, 10:23 PM
  2. Replies: 0
    Last Post: 01-31-2011, 09:08 AM
  3. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  4. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 09:01 PM
  5. Multi-line Tooltip inside a JTable's Cell
    By stevenc49 in forum AWT / Swing
    Replies: 2
    Last Post: 06-29-2008, 04:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •