Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>how to set Alignment for TableCell into prepareRenderer, </p> </blockquote> <p>This should NOT be done in the prepareRenderer code. This property should be set in the renderer for the class or for the column because it only applies to a specific class or renderer. Instead use:</p> <pre><code>table.setPreferredScrollableViewportSize(table.getPreferredSize()); DefaultTableCellRenderer stringRenderer = (DefaultTableCellRenderer)table.getDefaultRenderer(String.class); stringRenderer.setHorizontalAlignment( SwingConstants.CENTER ); </code></pre> <p>For the highlighting code I used code that assumes that the <code>dealld</code> value is unique for a given set of transactions:</p> <pre><code> private Map&lt;Object, Color&gt; rowColor = new HashMap&lt;Object, Color&gt;(); private Color nextColor = Color.ORANGE; @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); JComponent jc = (JComponent) c; if (isRowSelected(row)) return c; Object value = table.getValueAt(row, 7); Color background = rowColor.get(value); if (background != null) { c.setBackground( background ); } else { rowColor.put(value, nextColor); c.setBackground( nextColor ); nextColor = (nextColor == Color.ORANGE) ? Color.YELLOW : Color.ORANGE; } return c; } </code></pre> <p>Note: It won't work if sorting is required.</p> <p>Here is another approach that should work even if sorting is required (but I didn't test it with sorting);</p> <pre><code> @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); JComponent jc = (JComponent) c; if (!isRowSelected(row)) { c.setBackground(getRowBackground(row)); } return c; } private Color getRowBackground(int row) { boolean isDark = true; Object previous = getValueAt(0, 7); for (int i = 1; i &lt;= row; i++) { Object current = getValueAt(i, 7); if (! current.equals(previous)) { isDark = !isDark; previous = current; } } return isDark ? Color.ORANGE : Color.YELLOW; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload