2

For performance reasons I am using old version of uitable in Matlab. I am trying to change the cell-border-color of the table header. At present two column header cells are indistinguishable from each other. I have tried the following:

Custom column renderer but still can't change the table header border. The following seems to work but borders the entire header rather than table header cells.

h_old=uitable('v0','data',magic(5),'position',[5,5,550,400]);
set(h_old,'Units','normalized');
% get the underlying java table
jtable=h_old.getTable;
% get table header
theader=jtable.getTableHeader;
% set intended color 
color=java.awt.Color(9471874);
% create a line border
theaderborder=javax.swing.BorderFactory.createLineBorder(color);
% set it in table header
theader.setBorder(theaderborder);

This sets border around entire header but not in cells. Any one know how to do that?

For changing jtable grid (table cells border), I can do jtable.setGridColor(color). I am looking for equivalent but for table header cells.

Thanks, Amit

Amit
  • 185
  • 15

1 Answers1

3

You need to do a tiny bit of java coding. You need to implement custom cell renderer by extending DefaultTableCellRenderer. Since you can't write in-line java code in Matlab, you need to package this class in a jar and add to java path.

The class you need should look something like this:

public class CustomBorderCellRenderer extends DefaultTableCellRenderer {
    private static final long serialVersionUID = 1L;

    private Color borderColor;

    public CustomBorderCellRenderer(Color borderColor) {
        this.borderColor = borderColor;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JComponent component = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        ((JLabel) component).setHorizontalAlignment(JLabel.CENTER);
        component.setBorder(BorderFactory.createLineBorder(borderColor));
        return component;
    }
}

Note that it overrides some of the default appearance, so apart from setting border color, you might want to customize background color etc. As an example I also center the header labels, which would otherwise appear on the left.

Once you package and load the jar, the usage is straightforward:

javaaddpath('Path\to\YourJar.jar');
figure();
hTable = uitable('v0', 'data', magic(5), 'position', [5,5,550,400]);
set(hTable, 'Units', 'normalized');
borderColor = java.awt.Color(1,0,0);
renderer = CustomBorderCellRenderer(borderColor);
header = hTable.getTable().getTableHeader();
header.setDefaultRenderer(renderer);

You might also need to trigger the table to repaint itself, e.g. using drawnow.

enter image description here

nirvana-msu
  • 3,877
  • 2
  • 19
  • 28
  • Thanks Nirvana. I guess there is no workaround. I was trying to avoid java programming as I have no background in that or programming for that matter. There is a steep learning curve and have already started working on it. – Amit Mar 20 '16 at 19:00
  • It's not too steep. Learning it would, however, open endless UI (and not only) possibilities for you in Matlab. Packaging a jar only takes a few mouse clicks in any IDE, including a free one such as Eclipse. There's plenty of docs out there describing each step. – nirvana-msu Mar 20 '16 at 19:06
  • Thanks Nirvana. I am using IntelliJ to package a jar. But when I use it in Matlab it is unable to recognise it 'Undefined function or variable CustomBorderCellRenderer'. What is the main class that I have to select when packaging the jar? – Amit Mar 20 '16 at 23:05
  • If you've added all the imports and there were no unresolved errors, it should work. Jar is nothing more that than a zip archive - you can open it with `winrar`/`7zip` and make sure it has a `CustomBorderCellRenderer.class` file. There's also a very useful [whereIsJavaClassLoadingFrom](http://stackoverflow.com/questions/4376565/java-jpa-class-for-matlab#4380622) function, that provides the easiest way to check if a class is loaded. – nirvana-msu Mar 20 '16 at 23:40
  • Re coffee - sorry, I'm pretty busy and don't have much time for the projects now. Feel free to ask more questions here, though) – nirvana-msu Mar 20 '16 at 23:46
  • Added all imports, no unresolved errors, complies ok. Jar has the class file in it. But still no luck. Here is the jar and screenshot: https://www.dropbox.com/s/r3n06zq8hn11s01/CustomBorderCellRenderer.jar?dl=0 https://www.dropbox.com/s/irr425bcyt5uzlm/2016-03-21_002443.jpg?dl=0 – Amit Mar 21 '16 at 00:23
  • You compiled your jar using Java 8. Latest Matlab releases still ship with Java 7, and this apparently is the source of an issue. Change your project set up to point to a Java 7 JDK and re-build the jar. – nirvana-msu Mar 21 '16 at 01:30
  • All is in order now. Final questions:So this is an extension to existing DefaultTableCellRenderer class? All DefaultTableCellRenderer class default properties will still be available to set? Or do have to define what I want in this custom renderer and use it to set header properties. I am also planning to define data format (numerical, date etc) in different columns using getColumnModel and setting renderer. I think this will overwrite what we did above? I guess I will have to merge the two renders into one. - Never looked behind matlab, but its interesting! – Amit Mar 21 '16 at 15:43
  • We only attached this renderer to the header. At the moment it does not interfere with appearance of data in columns. You can assign the same, or a different renderer to the columns as well, e.g. `header.getColumnModel().getColumn(0).setCellRenderer(renderer)`. While this would work, that's not the easiest solution for complex tables. You might want to read up on [Jide Grids Developer Guide](https://www.jidesoft.com/products/JIDE_Grids_Developer_Guide.pdf) - it provides powerful tools creating and customizing tables. Anyways, that sounds like a new question. – nirvana-msu Mar 21 '16 at 15:56
  • Thanks Nirvana. This is very helpful. – Amit Mar 22 '16 at 00:51
  • Hi Nirvana, would you be able to help me on this: http://stackoverflow.com/questions/36153934/how-to-customise-jide-grids-in-matlab – Amit Mar 23 '16 at 07:57