Tablecellrenderer Usage Practice (i)

Source: Internet
Author: User

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?
  1. Import java.awt.*;
  2. Import javax.swing.*;
  3. Import javax.swing.table.*;
  4. Public class Roundcolortable extends JFrame {
  5. private string[] colname = {"1th column","2nd column","3rd column","4th column","5th column"}; //table header information
  6. private string[][] data = new string[10][5]; //table contents
  7. private JTable table;
  8. Public roundcolortable () {
  9. //table content assignment
  10. For (int i = 0; i < i++) {
  11. For (int j = 0; j < 5; j + +) {
  12. DATA[I][J] = "(" + (j+1) + "," + (i+1) + ")";
  13. }
  14. }
  15. Table = New JTable (new DefaultTableModel (Data,colname));
  16. Tablecellrenderer TCR = new Colortablecellrenderer ();
  17. Table.setdefaultrenderer (Object. CLASS,TCR); //Add renderer for jtable, because it is for all cells in the table, all with Object.class
  18. Add (new JScrollPane (table), borderlayout.center);
  19. setvisible (true);
  20. SetSize (300);
  21. Setdefaultcloseoperation (Jframe.exit_on_close);
  22. }
  23. public static void Main (String args[]) {
  24. new Roundcolortable ();
  25. }
  26. }
  27. Class Colortablecellrenderer extends Defaulttablecellrenderer
  28. {
  29. Defaulttablecellrenderer renderer=New Defaulttablecellrenderer ();
  30. Public Component gettablecellrenderercomponent (JTable table, Object value,
  31. Boolean isSelected, boolean hasfocus, int row, int column) {
  32. if (row%2 = = 0) {
  33. //Call base class method
  34. return super.gettablecellrenderercomponent (table, value, Isselected,hasfocus, row, column);
  35. }
  36. else{
  37. return renderer.gettablecellrenderercomponent (table, value, Isselected,hasfocus, row, column);
  38. }
  39. }
  40. //This class inherits with Jlabel,graphics for drawing cells, drawing red lines
  41. public void Paintcomponent (Graphics g) {
  42. Super.paintcomponent (g);
  43. Graphics2D g2= (graphics2d) G;
  44. final Basicstroke stroke=new Basicstroke (2.0f);
  45. G2.setcolor (color.red);
  46. G2.setstroke (stroke);
  47. G2.drawline (0,getheight ()/2,getwidth (), getheight ()/2);
  48. }
  49. }

Tablecellrenderer Usage Practice (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.