I have a Jtable which gets populated from an array of values. My code is like this:
private static final String[] columnNames = {"Line Number", "Error","Fix Proposed","Percentage (%)"};
static DefaultTableModel model = new DefaultTableModel(null,columnNames);
public static void DisplayMyJList(List<CaptureErrors> x,String extension,
ArrayList<Integer> l,ArrayList<Integer> p,
ArrayList<String> e,ArrayList<String> s) throws IOException {//Method to Dynamic get values to be populated in Jtable.
String theExtension = extension;
if(FILE_EXTENSION.equals("java")) {
for(CaptureErrors ex: x) {
Vector row = new Vector();
row.add(ex.getLinenumber());
row.add(ex.getMyfounderror());
row.add(ex.getMycorrection());
row.add(ex.getMyPercentage()+"%");
model.addRow( row );
//model.setRowColour(1, Color.YELLOW);
}
}
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFillsViewportHeight(true);
table.setShowGrid(true);
table.setShowVerticalLines(true);
table.setGridColor(new Color(0,128,0));
JTableHeader header = table.getTableHeader();
table.setBackground(new Color(255,228,225));
table.setEnabled(true);
header.setFont(new Font("Dialog", Font.CENTER_BASELINE, 12));
header.setBackground(Color.black);
header.setForeground(Color.yellow);
JScrollPane pane4 = new JScrollPane(table);
I can populate the Jtable from the array of values by Using a JButton. I want to have a condition where if column "percentage" ,get all values in this column >30, it highlights the rows to color.red.
I dont want to user TableCellRendererComponent .I want this action to be performed upon clicking the Jbutton.
The actual Jtable looks like this:
Then according to what I want to get, the first 2 rows should be highlighted in red. Any help appreciated.
See the approach in Table Row Rendering for a solution without creating custom renderers.
You may also want to check out Table Format Renderers so you can format the percentage column easily.