I want to increase readability of my jtable , here is MyTableModel.java class below , how to make each row with 2 different color shown in this picture . What should be the specific method that I can give different color to each row to increase readability of the user.
public class MyTableModel extends AbstractTableModel{
String [] columnNames;
Vector<Vector<Object>> data;
public DataAccessObject ObjDb = new DataAccessObject ();
public MyTableModel(String [] coln , Vector<Vector<Object>> data)
{
columnNames = coln;
this.data =data;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data.get(row).get(col);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col <= 0) {
return true;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data.get(row).set(col, value);
fireTableCellUpdated(row, col);
}
private class RowListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if (event.getValueIsAdjusting()) {
return;
}
// output.append("ROW SELECTION EVENT. ");
// outputSelection();
}
}
private class ColumnListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if (event.getValueIsAdjusting()) {
return;
}
// output.append("COLUMN SELECTION EVENT. ");
// outputSelection();
}
}
}
Netbeans automatically created my jtable with variable name I set is mytable then I
have problem with defining prepareRenderer below , do I miss step here ? I wanna make
each row with different color Here is my sample code below.
mytable.prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// Alternate row color
if (!isRowSelected(row))
c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
return c;
}
};
I have also use this one but this sets 5 columns i have 7 columns remaining two is not
colored at the end , when I click the white colored row the text color disappears.
mytable.setDefaultRenderer(Object.class, new TableCellRenderer() {
private DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean
isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
System.out.println(column);
if (row % 2 == 0) {
c.setBackground(Color.WHITE);
} else {
c.setBackground(Color.LIGHT_GRAY);
}
return c;
}
});
See this Table Row Rendering made by Camick. It'll solve your problem.
You can use his method
createAlternating
for rendering the colors of the row.long time ago, i had the same problem.
In this case i use the JXTable from the swingX library instead of the JTable. This componenet has a great method:
good luck
You could...
Use
JXTable
from SwingLabs, SwingX library which has functionality which could do this...this would be my preferred solution if you can use 3rd party libraries...You could...
Use Nimbus look and feel which does this...but this precludes you from using other look and feels (like the system look and feel)
You could...
Create your own series of custom
TableCellRenderer
s which color their background based on the current row...this is tedious as EVERY cell renderer you might need, needs to be able to perform this operation...You could...
Override the
prepareCellRenderer
method of theJTable
and forcefully set the background of the cell renderer yourself, based on the row...I'm not a fan of this solution, as it's forcing a design choice onto the renderer which might not fit it's requirements (overriding values the cell renderer might want) and locks you into a particular implementation of the table...You could...
Create a
JViewport
which was capable of talking with theJTable
and rendered the candy stripping underneath the table, but the table and renderers would need to be transparent...I've done this, it's...complicated...but allowed me to continue the candy stripping the full length of the view port (beyond the renderable area of the table)...Updated with
JViewport
exampleThis is an example of a concept I implemented a while ago for a project (it is actually managed by an
interface
so I could includeJList
andJTextArea
as well, but that's another question)...Basically, it makes the
JViewport
, which appears below theJTable
, responsible for rendering the actual candy stripping.But "why?" you ask...because it doesn't affect the table or cell renderer. The only requirement that this makes is that both the table and cell renderers are transparent (unless they have to be otherwise).
This means, you don't need to put candy stripping logic in your renderers, this means you don't need to override
prepareRenderer
of every table you create and mangle the requirements of the cell renderers...It's far from perfect, but demonstrates the basic idea...
Okay, but why bother? Well, basically, this approach allows you to paint beyond the area painted by the table...
Formatting numbers in cell renderers
You could start with something like...
If, for some reason, the default format instead suitable, you could do something like...
Instead