2017-06-07 138 views
0

JTable支持3種顏色條件格式,比如excel。 例如: enter image description here有條件格式化的Jtable,如Excel

我檢查了谷歌和所有我能找到一次分配單一顏色到符合條件的單元格。我想知道如果我能提供三種顏色,Jtable會根據單元的值分配顏色陰影。

回答

1

以下是其中一種方法,您可以如何做到這一點。我們可以找到其他課程here

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import javax.swing.AbstractListModel; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JViewport; 
import javax.swing.ListCellRenderer; 
import javax.swing.ListModel; 
import static javax.swing.SwingConstants.CENTER; 
import javax.swing.UIManager; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.JTableHeader; 

import general.VerticalTableHeaderCellRenderer; 
import java.awt.Color; 
import java.util.Random; 
import javax.swing.table.TableCellRenderer; 

// The class of template for table's presentation of knowledge 
//==============================================================================  
public class ExcelTable extends JFrame { 

    public static int rowCount = 1024; 
    public static int colCount = 1024; 
    public static int CHLength = 100; 
    public static int RWLength = 50; 
    public static int DCW = 20; 

// The constructor of this class 
//==============================================================================  
    public ExcelTable() { 
     super("Draw cell rotate"); 
     setSize(400, 400); 

     //The abstract model of table's presentation 
     ListModel lm = new AbstractListModel() { 
      String[] headers = new String[rowCount]; 

      @Override 
      public int getSize() { 
       return headers.length; 
      } 

      @Override 
      public Object getElementAt(int index) { 
       return headers[index]; 

      } 
     }; 

     DefaultTableModel dtm = new DefaultTableModel(lm.getSize(), colCount); 
     JTable table = new JTable(dtm) { 
      @Override 
      public Component prepareRenderer(TableCellRenderer renderer, int row, int col) { 
       Component comp = super.prepareRenderer(renderer, row, col); 
       Object value = getModel().getValueAt(row, col); 
       setCellSelectionEnabled(true); 

// Here you can write you schemes 
       final Random r = new Random(); 
       Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256), r.nextInt(256)); 

       if (getSelectedRow() != 0) { 
        if (row % 2 == 0 && col % 2 == 1) { 
         comp.setBackground(c); 
        } else if (row % 2 == 1 && row % 2 == 1) { 
         comp.setBackground(c); 
        } else { 
         comp.setBackground(c); 
        } 
       } else { 
        comp.setBackground(Color.white); 
       } 
       return comp; 
      } 
     }; 
     table.getTableHeader().setDefaultRenderer(new VerticalTableHeaderCellRenderer()); 
     for (int i = 0; i < colCount; i++) { 
      table.getColumnModel().getColumn(i).setPreferredWidth(DCW);// .setTotalColumnWidth(); 
     } 
     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 

     table.setCellSelectionEnabled(true); 

     // Create header rotation 
     JList rowHeader = new JList(lm); 
     // if (fieldlength) 
     rowHeader.setFixedCellWidth(RWLength); //RowHeaser width 
     rowHeader.setFixedCellHeight(table.getRowHeight()); 

     //Set render 
     rowHeader.setCellRenderer(new RowRenderer(table)); 

     //JScrollPane 
     JScrollPane pane = new JScrollPane(table); 
     pane.setColumnHeader(new JViewport() { 
      @Override 
      public Dimension getPreferredSize() { 
       Dimension d = super.getPreferredSize(); 
       d.height = CHLength; // Col header Height 
       return d; 
      } 
     }); 
     pane.setRowHeaderView(rowHeader); 
     getContentPane().add(pane, BorderLayout.CENTER); 

    } 

//========================================= 
    public static void main(String[] args) { 
     ExcelTable jr = new ExcelTable(); 
     jr.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 
     jr.setVisible(true); 
    } 
} 

class RowRenderer extends JLabel implements ListCellRenderer { 

    public RowRenderer(JTable table) { 
     JTableHeader header = table.getTableHeader(); 
     setOpaque(true); 
     setBorder(UIManager.getBorder("TableHeader.cellBorder")); 
     setHorizontalAlignment(CENTER); 
     setForeground(header.getForeground()); 
     setBackground(header.getBackground()); 
     setFont(header.getFont()); 
    } 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
     setText((value == null) ? "" : value.toString()); 
     return this; 
    } 
} 

OUTPUT: enter image description here

也:enter image description hereenter image description here

必須落實到覆蓋方法所需的配色方案:不是它設置只有兩個

@Override 
    public Component prepareRenderer 
    (TableCellRenderer renderer, int row, int col 

     ) { 
       Component comp = super.prepareRenderer(renderer, row, col); 
     Object value = getModel().getValueAt(row, col); 
     setCellSelectionEnabled(true); 

    // Here you can write you schemes in RGB 
// of course you need to describe it before mathematicaly 
// and using variable `row` and `col` you will have coordinates to the cells 
     final Random r = new Random(); 
     int R = r.nextInt(Math.round(256/(row + 1))); 
     int G = r.nextInt(Math.round(256/(col + 1))); 
     int B = r.nextInt(Math.round(256/(row + col + 1))); 
     Color c = new Color(R, G, B); 

     if (getSelectedRow() != 0) { 
      if (row % 2 == 0 && col % 2 == 1) { 
       comp.setBackground(c); 
      } else if (row % 2 == 1 && row % 2 == 1) { 
       comp.setBackground(c); 
      } else { 
       comp.setBackground(c); 
      } 
     } else { 
      comp.setBackground(Color.white); 
     } 
     return comp; 
    } 
}; 
+0

顏色,紅色和綠色。我正在尋找色調的色調。就像我的例子一樣,顏色的陰影也應該根據值 – user1631306

+1

改變,我在哪裏可以找到「VerticalTableHeaderCellRenderer」類 – user1631306

+0

我再次看到,你正在爲三種不同的條件分配三種顏色。我正在嘗試製作一個熱圖,其中會有漸變的顏色。我怎樣才能做到這一點。 – user1631306