2017-10-17 2 views
0

Ich habe eine Tabelle auf scrollPane und einige meiner Zellen sind in verschiedenen Farben gemalt. Wenn ich auf die Zelle in der betreffenden Zeile klicke, oder die Zeilen sollten ihre Farbe ändern, funktioniert das Neuzeichnen jedoch nur, wenn ich in meiner Tabelle blättere, wenn mehrere Zeilen eingefärbt werden sollen. Wie kann ich vollständige Aktualisierung für JTable nach dem Neuanlegen von Zellen durch Überschreiben von getTableCellRenderrerComponent() tun? Danke.Wie sollte ich Tabelle aktualisieren?

meine Tabellenklasse:

class MyGrid extends JTable { 
    private TNotifyEvent onCustomDrawCell; 

    public MyGrid() { 
     this.setSelectionMode(0); 
    } 

    public TNotifyEvent getOnCustomDrawCell() { 
     return this.onCustomDrawCell; 
    } 

    public void setOnCustomDrawCell(TNotifyEvent onCustomDrawCell) { 
     this.onCustomDrawCell = onCustomDrawCell; 
    } 
} 

Verwendung meiner Tabelle

MyGrid table = new MyGrid(); 
JScrollPane scroll = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

table.setOnCustomDrawCell(() -> { 
     for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { 
      table.getColumnModel().getColumn(i).setCellRenderer(new DefaultTableCellRenderer() { 
       JLabel lbl = new JLabel(); 

       @Override 
       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
                   boolean hasFocus, int row, int column) { 
        lbl = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
        lbl.setText(String.valueOf(value)); 
        MyItem item = new MyItem(); 
        item.setValue((Vector) model.getDataVector().get(row)); 
        item.setCanvas(lbl); 
        MutableBoolean aDoneMutable = new MutableBoolean(false); 
        getContentStyle(null, item, item.getCanvas(), null); 
        drawCells(null, item.getCanvas(), item, aDoneMutable); 
        return lbl; 
       } 
      }); 
     } 
    return true; 
    }); 

    public static void getContentStyle(Object Sender, MyItem ARecord, JLabel AItem, TcxStyle AStyle) { 
    try { 
     if (Integer.parseInt(String.valueOf(ARecord.getValue().get(3))) == 0) { 
      AItem.setOpaque(true); 
      AItem.setBackground(Color.GRAY); 
     } else if (ARecord.getValue().get(4).equals("222")) { 
      AItem.setOpaque(true); 
      AItem.setBackground(Color.PINK); 
     } else { 
      AItem.setOpaque(true); 
      AItem.setBackground(null); 
     } 
    } catch (Exception e) { 
    } 
} 

public static void drawCells(Object Sender, JLabel ACanvas, MyItem AViewInfo, final MutableBoolean mutableADone) { 
    boolean ADone = mutableADone.getValue(); 
    try { 
     if((AViewInfo.getValue().get(4).equals("44")) && (!AViewInfo.isSelected())) { 
      ACanvas.setForeground(Color.black); 
      ACanvas.setOpaque(true); 
      ACanvas.setBackground(PictureUtils.createColorfromString("$CCE6FF")); 
      AViewInfo.setCanvas(ACanvas); 
     } else { 
      if((AViewInfo.getValue().get(5).equals("555")) && (AViewInfo.isSelected()) && (AViewInfo.isHasFocus())) { 
       ACanvas.setOpaque(true); 
       ACanvas.setBackground(PictureUtils.createColorfromString("$CCE6FF")); 
       AViewInfo.setCanvas(ACanvas); 
      } 
     } 
    } catch (Exception e) {} 
    mutableADone.setValue(ADone); 
} 

Auch myItem Klasse:

class MyItem { 
    private String columnName; 
    private Vector value; 
    private JLabel canvas; 
    private Integer index; 
    private boolean isSelected; 
    private boolean hasFocus; 

    public MyItem() { 
    } 

    public String getColumnName() { 
     return this.columnName; 
    } 

    public void setColumnName(String columnName) { 
     this.columnName = columnName; 
    } 

    public Vector getValue() { 
     return this.value; 
    } 

    public void setValue(Vector value) { 
     this.value = value; 
    } 

    public JLabel getCanvas() { 
     return this.canvas; 
    } 

    public void setCanvas(JLabel canvas) { 
     this.canvas = canvas; 
    } 

    public boolean isSelected() { 
     return this.isSelected; 
    } 

    public void setSelected(boolean selected) { 
     this.isSelected = selected; 
    } 

    public boolean isHasFocus() { 
     return this.hasFocus; 
    } 

    public void setHasFocus(boolean hasFocus) { 
     this.hasFocus = hasFocus; 
    } 

    public Integer getIndex() { 
     return this.index; 
    } 

    public void setIndex(Integer index) { 
     this.index = index; 
    } 
} 

Ich versuchte tableChange() Hörer zu verwenden, aber es wirft Stackoverflow Fehler vorher zeigt meine Tabelle

BEARBEITEN Auch ich habe vergessen zu sagen: wenn ich meinen Code debugging der Tisch ist gut gefärbt. Und Tabelle repaint überall außer ScrollPane Ansichtsfenster.

+0

gelöst. Ich muss nur das Ansichtsfenster meiner Schriftrolle aktualisieren. – Nataly

Antwort

1

Jedes Mal, wenn Sie eine richtige einer Swing-Komponente ändern müssen Sie die Komponente sagen, sich neu zu zeichnen:

public void setOnCustomDrawCell(TNotifyEvent onCustomDrawCell) { 
    this.onCustomDrawCell = onCustomDrawCell; 
    repaint(); // added this 
} 
+0

Nein. Es hilft nicht, die Farbgebung funktioniert wie zuvor. – Nataly

+0

Ich gab Ihnen das grundlegende Konzept, wie Swing-Komponenten arbeiten. Beim Scrollen wird die Tabelle angewiesen, bestimmte Zeilen neu zu streichen. Irgendwo in Ihrem Code müssen Sie also die Daten der Tabelle (oder Ihrer MyItem-Klasse) ändern und die Tabelle nicht neu streichen. Der von Ihnen gepostete Code hilft nicht, da wir nicht wissen, in welchem ​​Kontext dieser Code verwendet wird und wie die Daten geändert und zur Tabelle hinzugefügt werden. Poste ein korrektes [mcve], das das "Konzept" dessen zeigt, was du zu tun versuchst. – camickr

+0

Ich habe Code zur Verfügung gestellt, wie ich kann. – Nataly

Verwandte Themen