JTable row coloring page

I want to colorize specific rows in jTable .. I did this for columns using this code,

private class CustomCellRenderer extends DefaultTableCellRenderer { /* (non-Javadoc) * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int) */ @Override public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) { Component rendererComp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column); //Set foreground color // rendererComp.setForeground(Color.red); //Set background color rendererComp .setBackground(Color.pink); return rendererComp ; } } 

And I call the above code using

  jTable1.getColumnModel().getColumn(3).setCellRenderer(new CustomCellRenderer()); 

But I want to do the same for strings in jTable. In the case of strings, there is no getColumnModel () or getColumn (). What is this alternative way to do this? I do this in Netbeans using Java Swing ..

+4
source share
3 answers

Here is an example of how you can combine column colors and row colors. You basically run tests in TableCellRenderer to see if the background should be one color or another.

 import java.awt.Color; import java.awt.Component; import java.util.Enumeration; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; public class TestTable { public class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setBackground(null); super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); setText(String.valueOf(value)); boolean interestingRow = row % 5 == 2; boolean secondColumn = column == 1; if (interestingRow && secondColumn) { setBackground(Color.ORANGE); } else if (interestingRow) { setBackground(Color.YELLOW); } else if (secondColumn) { setBackground(Color.RED); } return this; } } private JFrame f; private JTable table; protected void initUI() { Vector<Vector<Object>> data = new Vector<Vector<Object>>(); Vector<String> columNames = new Vector<String>(); columNames.add("Col 0"); columNames.add("Col 1"); columNames.add("Col 2"); for (int i = 0; i < 20; i++) { Vector<Object> v = new Vector<Object>(); v.add(i % 3 == 0 ? "Hello" : "World"); v.add("Some data in row " + (i + 1)); v.add("Some other data in row " + (i + 1)); data.add(v); } table = new JTable(new DefaultTableModel(data, columNames)); Enumeration<TableColumn> en = table.getColumnModel().getColumns(); while (en.hasMoreElements()) { TableColumn tc = en.nextElement(); tc.setCellRenderer(new MyTableCellRenderer()); } f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); f.add(new JScrollPane(table)); f.pack(); f.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestTable().initUI(); } }); } } 
+6
source

How you want to change whole rows, regardless of the column class, consider overriding prepareRenderer() , as discussed here . The approaches TableCellRenderer and prepareRenderer() contrasted here .

+3
source

This seems like a very dirty way to handle it. It will be much better if you use gridLayout using layoutManager for your containment device (I think it should be a JFrame). U can add individual components (JPanels, Jbuttons, or any other JComponent) and handle their appearance using the paint () / repaint () methods.

EDIT

OR you can change the getTableCellRendererComponent (....) method to set your custom background colors using nested if-else statements or a switching case according to int rows, int columns (which are given as arguments).

It will be much easier.

0
source

All Articles