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!

JTable cell renderer issue

843806Mar 28 2008 — edited Apr 1 2008
Hi,

I'm having an issue with my JTable that can hopefully be resolved without having to change my JTable custom cell renderer. I have a JTable where each column is sortable. There was a sortable table class made available before java6, and thats what I'm using. I believe this sortable table requires a custom cell renderer that extends JLabel for the sort to work right. What I would like to be able to handle is the following:

The third column in my table may have multiple lines in it. I would like for the ROW containing that cell with the multiple lines in it to resize, so that the row is clearly delineated, and that the multiple lines in the column show up properly. I've seen multiple posting on some variation of this problem, but none of those solutions have worked for me.

Here is my cell renderer so far:
private final class MyTableCellRenderer extends DefaultTableCellRenderer {
    
        public MyTableCellRenderer () { 
            super();
        }
        
        public Component getTableCellRendererComponent(JTable table, Object value, 
                boolean isSelected, boolean hasFocus, int row, int column) {
        
            Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		
            if(value instanceof Long) {
                Number n = (Number) value;
                NumberFormat formatter = NumberFormat.getIntegerInstance(Environment.getLocale());
                value = formatter.format(n.longValue());
                super.setValue(value);
                ((JLabel)cell).setHorizontalAlignment(JLabel.RIGHT);
                //((JLabel)cell).setFont( monoFont );
            } else if (value instanceof Double) {
                Number n = (Number) value;
                NumberFormat formatter = NumberFormat.getPercentInstance(Environment.getLocale());
                formatter.setMinimumFractionDigits (3);
                formatter.setMaximumFractionDigits (3); 
                value = formatter.format(n.doubleValue());
                super.setValue(value);
                ((JLabel)cell).setHorizontalAlignment(JLabel.RIGHT);
                //((JLabel)cell).setFont( monoFont );
            } else if (value instanceof String) { 
               
                if (column == 2) { 
                    String s =  "<html><body>"; 
                    //System.out.println("value = " +value);
                    String[] pieces = ((String)value).split("\\n");
                    for(int i = 0; i < pieces.length; i++) { 
                        //System.out.println("\tpieces["+i+"] = " +pieces);
s += pieces[i];
s += "<br>";
}
s += "</body></html>";
((JLabel)cell).setText(s);
}

if (column == 3) {
String s = "<html> ";
String[] pieces = ((String)value).split("/");

for(int i = 0; i < pieces.length - 1; i++ )
s += pieces[i] + ".";

s = s + "<B>" + pieces[pieces.length -1] + "</B>" + "</html>";
((JLabel)cell).setText(s);
}

((JLabel)cell).setHorizontalAlignment(JLabel.LEFT);
//((JLabel)cell).setText(s);
((JLabel)cell).setBorder(new EmptyBorder(0,10,0,0));
}

return cell;

}

}


Edited by: Konigs on Mar 28, 2008 10:03 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 29 2008
Added on Mar 28 2008
4 comments
274 views