Hello.
I display some Double values represented as String in a JTable, but I want to align them RIGHT. Is there no easy way of doing this?
My structure is something like:
public class DataStructure extends AbstractTableModel {
//Overrides standard methods
}
public class DataWindow {
//Creates the JTable here
JTable jt = new JTable(dataVectorVector, columnNamesVector);
// I have tried creating a custom tablecellrenderer, but error see below.
CustomDataRenderer custom = new CustomDataRenderer();
// and tries to implement it with. Fails, see below
jt.getColumn(0).setCellRenderer(custom);
}
public class CustomDataRenderer extends DefaultTableCellRenderer {
// This is copied in; I dont really understand it..:)
public Component getTableCellRendererComponent( JTable jt ,Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
System.out.println("i customhbvrenderer");
Component comp = super.getTableCellRendererComponent(jt ,value,isSelected,hasFocus,row,column);
JLabel label = (JLabel)comp;
if(value.getClass().equals(new Double("2").getClass())){
label.setHorizontalAlignment(JLabel.RIGHT);
}
if( ((Double)value).isNaN() ) {
label.setBackground(Color.RED);
}
else {
label.setBackground(Color.WHITE);
}
return label;
}
}
JWM breaks execusion with :
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Identifier not found
at javax.swing.table.DefaultTableColumnModel.getColumnIndex(Unknown Source)
at javax.swing.JTable.getColumn(Unknown Source)
Am I on the right track, and can anyone help me further?
Also: How can I verify an objects class? Referring to above, I would like to render cells with a Double inside one way, Strings other way etc, and need to check the class of the Objects.
Regards
JT