Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Relocating JTable Header Sort Icon

796262Apr 28 2009 — edited Apr 28 2009
Hey all,

Say I have a JTable with multiple columns and corresponding headers. The column headers are sized in a way that their text either takes up the entire width of that header or the text spans multiple lines.

If the user clicks one of the single-line headers in order to sort the table rows according to that column, the text is truncated and "..." is appended in order to make room for the sort icon (that little triangle that points either up or down to show ascending vs descending). If the user clicks one of the multiple-line columns, the text is not truncated because there is already enough room for both the text and the icon.

My question is: is there a way to relocate where that sort icon is drawn? I'd like it to be below the text for the single-line column headers. That way when the user sorts according to that row, all of the text in that header is still visible. I thought I'd use a custom renderer for the table headers, but I'm not sure how to obtain on which column the icon should be drawn. That's where I'm stuck.

I know that simply making the columns wider would allow this, but in my "real" program I'm working with many columns and limited space. This is my attempt at cleaning things up a bit.

Here's some compilable code that demonstrates my question:

 import javax.swing.*;
import java.awt.*;

public class TestTable{
	public static void main(String [] args){

		JFrame frame = new JFrame("Testing Table");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		String[] columnNames = {"One Line",
                        "<html>Two<br/>Lines",
                        "Single Line"
			};

		String[][] tableData = {{"one", "two", "three"},
					{"ABC", "DEF", "GHI"},
					{"Cats", "Dog", "Monkey"}};

		JTable table = new JTable(tableData, columnNames);
		table.getTableHeader().setPreferredSize(new Dimension(200,50));
		
		MyHeaderRenderer renderer = new MyHeaderRenderer();

		for(int c = 0; c < table.getColumnCount(); c++){
		//Uncomment this line to use a custom renderer
		//right now the custom renderer doesn't do anything
		//goal: control where the sort icon is located
		//	table.getColumnModel().getColumn(c).setHeaderRenderer(renderer);
		}

		table.setAutoCreateRowSorter(true);

		JScrollPane scroll = new JScrollPane(table);
		frame.getContentPane().add(scroll);
		frame.setSize(200,400);
		frame.setVisible(true);
	}
}
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;


public class MyHeaderRenderer extends DefaultTableCellRenderer{

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
		return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	}

}
Thanks guys / girls!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 26 2009
Added on Apr 28 2009
4 comments
644 views