RegisterSearchFAQ UsergroupsLog in
Wrapping and Centering text in a header question

 
Reply to topic    Citra Technologies Forum Index » Technical Support - Evaluation View previous topic
View next topic

Wrapping and Centering text in a header question
Author Message
dmacleod



Joined: 04 Oct 2007
Posts: 20

Post Wrapping and Centering text in a header question Reply with quote
Is there a way to wrap the text within a table header? Also, how would I center text within a header and also a data cell?

thanks
Tue Oct 09, 2007 11:17 am View user's profile Send private message Send e-mail
support



Joined: 21 Feb 2006
Posts: 1463

Post Reply with quote
In order to center the text, you should use a TableCellRenderer, such as DefaultRenderer. For example:

Code:
DefaultRenderer dr = new DefaultRenderer();
dr.setForeground(Color.blue);
dr.setHorizontalAlignment(SwingConstants.CENTER);

//assign the renderer to the header renderer of the first column
table.getColumnModel().getColumn(0).setHeaderRenderer(dr);
//assign the renderer to the data renderer of the first column
table.getColumnModel().getColumn(0).setCellRenderer(dr);


For wrapping the text, you must use <html> tags for your column names.
For example:

TableColumn tc = table.getColumnModel().getColumn(0);
tc.setHeaderValue("<html>first line<BR>second line</html>");

Also, you can see this post for relevant information:
http://www.citra-tech.com/forum/viewtopic.php?t=57
Tue Oct 09, 2007 1:45 pm View user's profile Send private message
dmacleod



Joined: 04 Oct 2007
Posts: 20

Post Problem with sorting header Reply with quote
This does work to center the text in the columns, however, my headers are sortable and this approach doesn't show the sort arrow in the headers, also it doesn't use the default background (style) as the other columns in the header.
Tue Oct 09, 2007 3:22 pm View user's profile Send private message Send e-mail
support



Joined: 21 Feb 2006
Posts: 1463

Post Reply with quote
If you are using sortable headers, you should work with SortTableButtonRenderer. Subclass SortTableButtonRenderer and override the getTableCellRendererComponent method:

Code:
public class MySortTableButtonRenderer extends SortTableButtonRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
       Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
       /*if (column == 0) c.setBackground(Color.red);
       else c.setBackground(Color.yellow);*/
       JButton button = ((JButton) c);
       if (column == 0) button.setHorizontalAlignment(SwingConstants.CENTER);
       else button.setHorizontalAlignment(SwingConstants.RIGHT);
       return c;
    }
}


Then, create SortTableModel like this:
Code:
SortTableModel stm = new SortTableModel() {
    protected SortTableRenderer createDefaultSortTableRenderer() {
        return new MySortTableButtonRenderer();
    }
};

This way, you can choose the style for each column.

Alternatively, for each column to have the same style:

Code:
SortTableButtonRenderer b = (SortTableButtonRenderer) sortTableModel.getSortTableRenderer();
b.getAscendingButton().setBackground(Color.red);
b.getDescendingButton().setBackground(Color.red);
b.getNoSortButton().setBackground(Color.red);
b.getAscendingButton().setHorizontalAlignment(SwingConstants.CENTER);
b.getDescendingButton().setHorizontalAlignment(SwingConstants.CENTER);
b.getNoSortButton().setHorizontalAlignment(SwingConstants.CENTER);

Tue Oct 09, 2007 3:52 pm View user's profile Send private message
navTeam2



Joined: 14 Jan 2011
Posts: 99

Post Reply with quote
We used the code :
Code:

      SortTableButtonRenderer r = (SortTableButtonRenderer) stm.getSortTableRenderer();
      r.getAscendingButton().setHorizontalAlignment(SwingConstants.CENTER);
      r.getDescendingButton().setHorizontalAlignment(SwingConstants.CENTER);
      r.getNoSortButton().setHorizontalAlignment(SwingConstants.CENTER);


A few years ago, when we tried it, it used to work but now the centering is ignored: headers are left aligned.
I don't know whether it's a tablelib or java update (currently using 1.6 u24) bug.

Do you confirm? It there something we can do to have the headers centered?

Thanks in advance

NavTeam
Fri Nov 18, 2011 4:14 am View user's profile Send private message
support



Joined: 21 Feb 2006
Posts: 1463

Post Reply with quote
Hi,

This is not a bug, since sometime now the alignment is applied inside SortTableButtonRenderer. In order to change the alignment, you should create a SortTableButtonRenderer subclass and override the prepareRenderer method:

Code:
public Component prepareRenderer(...) {
    JButton b = (JButton) super.prepareRenderer(..);
    b.setHorizontalAlignment(SwingConstants.CENTER);
    return b;
}

Fri Nov 18, 2011 6:56 am View user's profile Send private message
navTeam2



Joined: 14 Jan 2011
Posts: 99

Post Reply with quote
Thanks, works fine...
Mon Nov 21, 2011 11:43 am View user's profile Send private message
Display posts from previous:    

Reply to topic    Citra Technologies Forum Index » Technical Support - Evaluation All times are GMT - 5 Hours
Page 1 of 1

 
Jump to: 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group