Reprint: http://blog.csdn.net/chosen0ne/article/details/4453267
Swing components are divided into two types based on the type of data they manipulate, a component of a scalar data type, and a class of components of a composite data type. A component of a scalar data type operates on basic types of data, such as strings, Booleans, numbers, and so on, including JTextField, Jcheckbox, JLabel, JButton, and so on. Components of a composite data type operate on data such as vectors, matrices, and non-linear types. The components of a vector data type have JComboBox, JList, components of the matrix data type have jtable, components of non-linear data types such as JTree.
To visualize various types of data, components of a composite data type tend to use scalar data type components to represent each data element. For example, a column of jtable data is a string type, then the cell of the column is often used JLabel way each string, if a column of data is a Boolean type, then the cell of the column is often jcheckbox way to show each Boolean value.
How do I implement the rendering of components of a composite data type? Most directly in the Paint method one by one to draw each component according to the data type, but this method is obviously the code reuse rate is very low, a large number of duplicate the corresponding scalar component code, code maintenance and synchronization can be very difficult, and not easy to achieve skin switching.
To solve this problem, the concept of the so-called renderer (Renderer) is presented in swing system, the core idea of which is to use interface, encapsulate and reuse the rendering code of the scalar component, reduce the code repetition rate and improve the extensibility of the component.
JTable is responsible for managing the visual appearance of the table, and when each cell is drawn, call getcellrenderer (int row,int col) to get the current cell renderer, get a component, and then draw it. Tablecellrenderer is an interface with only one method:
Component gettablecellrenderercomponent(JTable table,
Object value,
Boolean isSelected,
Boolean hasfocus,
int row,
int column)
Where row and column identify a cell. That is, depending on the row and column (the cell is different), you can get a different fill component, so that you only need to make appropriate logical judgments within the method. For example, you can make different rows of the same column render differently, either in a cell with a different background color, or in a cell with a different font. The following is the effect of implementing an even line that is drawn as a line:
[Java] view plain copy print?
- Import java.awt.*;
- Import javax.swing.*;
- Import javax.swing.table.*;
- Public class Roundcolortable extends JFrame {
- private string[] colname = {"1th column","2nd column","3rd column","4th column","5th column"}; //table header information
- private string[][] data = new string[10][5]; //table contents
- private JTable table;
- Public roundcolortable () {
- //table content assignment
- For (int i = 0; i < i++) {
- For (int j = 0; j < 5; j + +) {
- DATA[I][J] = "(" + (j+1) + "," + (i+1) + ")";
- }
- }
- Table = New JTable (new DefaultTableModel (Data,colname));
- Tablecellrenderer TCR = new Colortablecellrenderer ();
- Table.setdefaultrenderer (Object. CLASS,TCR); //Add renderer for jtable, because it is for all cells in the table, all with Object.class
- Add (new JScrollPane (table), borderlayout.center);
- setvisible (true);
- SetSize (300);
- Setdefaultcloseoperation (Jframe.exit_on_close);
- }
- public static void Main (String args[]) {
- new Roundcolortable ();
- }
- }
- Class Colortablecellrenderer extends Defaulttablecellrenderer
- {
- Defaulttablecellrenderer renderer=New Defaulttablecellrenderer ();
- Public Component gettablecellrenderercomponent (JTable table, Object value,
- Boolean isSelected, boolean hasfocus, int row, int column) {
- if (row%2 = = 0) {
- //Call base class method
- return super.gettablecellrenderercomponent (table, value, Isselected,hasfocus, row, column);
- }
- else{
- return renderer.gettablecellrenderercomponent (table, value, Isselected,hasfocus, row, column);
- }
- }
- //This class inherits with Jlabel,graphics for drawing cells, drawing red lines
- public void Paintcomponent (Graphics g) {
- Super.paintcomponent (g);
- Graphics2D g2= (graphics2d) G;
- final Basicstroke stroke=new Basicstroke (2.0f);
- G2.setcolor (color.red);
- G2.setstroke (stroke);
- G2.drawline (0,getheight ()/2,getwidth (), getheight ()/2);
- }
- }
Tablecellrenderer Usage Practice (i)