My question like this:
I have a JTable, I put a JProgressBar in one Column, but it fill the full cell.
I just want to set JProgressBar’s size so that it shows with margin with cell border.
My JProgressBar code:
private final JProgressBar b;
public ProgressBarRender(){
super();
setOpaque(true);
b = new JProgressBar();
b.setStringPainted(true);
b.setMinimum(0);
b.setMaximum(100);
b.setBorderPainted(true);
b.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
//b.setPreferredSize(new Dimension(1,1));
b.setOpaque(true);
//TODO
//b.setForeground(Color.magenta);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Integer i = (Integer) value;
b.setValue(i);
return b;
}
I have called JProgressBar’s setPreferredSize method, but it does’t work.
I also called JTable’s setIntercellSpacing method, but when JTable’row selected background not show in the space,it is’t I wanted.
Can any one help me?
The cell renderer will fill in the whole cell, until you implement your TableUI class.
The reason why setPreferredSize() is not working, it is because the cell renderer not use preferered size. And preferred size should not never to explicitly set, should overwrite the getPreferredSize() method.
To add the margin is easy, just put the progress bar into the panel, then set the border to panel. And if you want to different color when selected or deselected, you need to implement it too.
Following is the code.