2011-08-20 10 views
8

come ho letto che non è possibile codificare la mia lingua navajoJTable fila hightlighter in base al valore da TableCell

finging il modo come unico alternato/strisce di colore in JTable (esempio @camickr)

enter image description here

Ho un JTable che contiene alcune operazioni di mercato (meglio per comprendere a ragione le mie scarse competenze in inglese), ma alcune delle offerte hanno solo una gamba, ma un'altra (ad esempio vanilla Cross Currency Swap) potrebbe avere due gambe. Come è possibile evidenziare TableRows in base al valore dalle specifiche TableColumn (ad esempio l'ultima colonna con nome DealId). Ho provato a controllare row con row - 1 && row + 1, ma la mia testa vuota ha generato un sacco di codici, per avere un'idea di come fermare complicate semplici cose semplici, come verificare se esiste un valore duplicato in un'altra riga (sempre con un ordine rigoroso come catturato nelle immagini). Non ho idea di come implementa semplice formula per quella

immagini dimostrata:

enter image description here
enter image description here enter image description here

generato dal codice:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.table.*; 

public class TablePrepareRenderer extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private Object[] columnNames = { 
     "Buy/Sell", "Type", "SubType", "Ccy1", "Amount1", "Ccy2", "Amount2", "DealId"}; 
    private Object[][] data = { 
     {"Buy&Sell", "Ccy Swap", "A1", "EUR", new Double(1000000.00), "USD", new Double(1439000.00), 50}, 
     {"Buy&Sell", "Ccy Swap", "A3", "USD", new Double(1438900.00), "EUR", new Double(1000000.00), 50}, 
     {"Buy&Sell", "Ccy Swap", "A1", "EUR", new Double(500000.00), "CHF", new Double(550000.00), 350}, 
     {"Buy&Sell", "Ccy Swap", "A1", "CHF", new Double(549800.00), "EUR", new Double(500000.00), 350}, 
     {"Sell&Buy", "Ccy Swap", "A3", "USD", new Double(1000000.00), "EUR", new Double(749000.00), 2250}, 
     {"Sell&Buy", "Ccy Swap", "A1", "EUR", new Double(748900.00), "USD", new Double(1000000.00), 2250}, 
     {"Buy&Sell", "Ccy Swap", "A1", "GBP", new Double(1000000.00), "USD", new Double(1638100.00), 400}, 
     {"Buy&Sell", "Ccy Swap", "A3", "USD", new Double(1638200.00), "GBP", new Double(1000000.00), 400}, 
     {"Sell", "Ccy Spot", "A1", "AUD", new Double(343575.0), "EUR", new Double(250000.0), 11990}, 
     {"Buy", "Ccy Spot", "A1", "EUR", new Double(100000.00), "JPY", new Double(1099000.00), 259}, 
     {"Sell", "Ccy Fwd", "A3", "DKK", new Double(74889.00), "EUR", new Double(10000.00), 115439},}; 
    private JTable table; 

    public TablePrepareRenderer() { 

     DefaultTableModel model = new DefaultTableModel(data, columnNames); 

     table = new JTable(model) { 

      private static final long serialVersionUID = 1L; 

      @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(getBackground()); 
       int modelRow = convertRowIndexToModel(row); 
       String type = (String) getModel().getValueAt(modelRow, 0); 
       if (("Buy".equals(type)) && !("Buy&Sell".equals(type))) { 
       c.setBackground(Color.orange); 
       } else if (("Sell".equals(type)) && !("Sell&Buy".equals(type))) { 
       c.setBackground(Color.orange); 
       } else if ("Buy&Sell".equals(type)) { 
       c.setBackground(Color.yellow); 
       } else if ("Sell&Buy".equals(type)) { 
       c.setBackground(Color.yellow); 
       } 
       }*/ 
       /*if (!isRowSelected(row)) { 
       if (row == 0 ||row == 1||row == 4||row == 6||row == 7||row == 9||row == 10) { 
       ((JComponent) c).setBackground(Color.orange); 
       } else { 
       ((JComponent) c).setBackground(Color.yellow); 
       } 
       }*/ 

       if (!isRowSelected(row)) { 
        if (row == 0 || row == 1 || row == 4 || row == 5 || row == 8 || row == 10) { 
         ((JComponent) c).setBackground(Color.orange); 
        } else { 
         ((JComponent) c).setBackground(Color.yellow); 
        } 
       } 

       if (column == 0 || column == 1 || column == 2 || column == 3 || column == 5) { 
        //setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
        //c.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
        //(JComponent) c.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
       } 
       return c; 
      } 

      @Override 
      public Class getColumnClass(int column) { 
       switch (column) { 
        case 0: 
         return String.class; 
        case 1: 
         return String.class; 
        case 2: 
         return String.class; 
        case 3: 
         return String.class; 
        case 4: 
         return Double.class; 
        case 5: 
         return String.class; 
        case 6: 
         return Double.class; 
        case 7: 
         return Integer.class; 
       } 
       return null; 
      } 
     }; 
     table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
     JScrollPane scrollPane = new JScrollPane(table); 
     getContentPane().add(scrollPane); 
    } 

    public static void main(String[] args) { 
     TablePrepareRenderer frame = new TablePrepareRenderer(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 

Edit:

come impostare Alignment per TableCell in prepareRenderer,

+0

Siamo spiacenti, qual è il tuo obiettivo finale? Quali immagini sopra mostrano l'output desiderato e quali immagini mostrano l'uscita corrente? –

+0

@Hovercraft pieno di anguille per tutte le possibili combinazioni/immagini, e mi aspettavo di raggruppamento l'evidenziatore per tutte le possibili combinazioni che esistono, in base al valore che restituisce TableCell – mKorbel

+0

Sta dicendo che tutte e 3 le immagini mostrano la colorazione desiderata? Sembra che il colore della riga debba cambiare ogni volta che cambia il valore 'dealld'? È questo il semplice requisito o è più complesso di così? – camickr

risposta

4

come impostare allineamento per TableCell in prepareRenderer,

questo non dovrebbe essere fatto nel codice prepareRenderer. Questa proprietà deve essere impostata nel renderer per la classe o per la colonna poiché si applica solo a una classe o a un renderer specifici. Invece utilizzare:

table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
DefaultTableCellRenderer stringRenderer = (DefaultTableCellRenderer)table.getDefaultRenderer(String.class); 
stringRenderer.setHorizontalAlignment(SwingConstants.CENTER); 

Per il codice evidenziazione ho usato il codice che assume che il valore dealld è unico per un dato insieme di transazioni:

 private Map<Object, Color> rowColor = new HashMap<Object, Color>(); 
     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; 
     } 

Nota: Non funzionerà se è richiesta l'ordinamento .

Ecco un altro approccio che dovrebbe funzionare anche se è richiesto l'ordinamento (ma non l'ho verificato con l'ordinamento);

+0

grazie per questo +1 – mKorbel

2

è domenica, mentre sta scendendo, quindi non ho potuto resistere a mostrare la versione SwingX.E 'la stessa logica @camickr 2 °, grazie :-)

Vantaggi:

codice
  • possono concentrarsi sulla logica come valore recuperando le maniglie di selezione/filtraggio/colonna si muove automagically
  • sa di predefinito ui colori dello striping alternativi (e aggiornamenti sulla commutazione del LAF)
  • Il supporto dell'evidenziatore è incorporato, non è necessario sottoclasse della tabella, né preoccuparsi del cattivo comportamento del renderer
  • facile aggiungere ulteriori evidenziatori (Urlando vendere, vendere, vendere :-)

Il codice snipped:

JXTable table = new JXTable(data, columnNames); 
HighlightPredicate predicate = new HighlightPredicate() { 

    @Override 
    public boolean isHighlighted(Component renderer, 
      ComponentAdapter adapter) { 
     if (adapter.row == 0) return false; 
     return isOddValue(adapter); 
    } 

    private boolean isOddValue(ComponentAdapter adapter) { 
     Object previous = adapter.getFilteredValueAt(0, 7); 
     boolean odd = false; 
     for (int i = 1; i <= adapter.row; i++) { 
      Object current = adapter.getFilteredValueAt(i, 7); 
      if (!previous.equals(current)) { 
       odd = !odd; 
      } 
      previous = current; 
     } 
     return odd; 
    } 

}; 
table.addHighlighter(new UIColorHighlighter(predicate)); 
+0

strana Primavera & Estate in Europa continentale +1 – mKorbel

Problemi correlati