Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

java - How do I set the JTable column and row color?

How do I set the JTable column and row color?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a short example.

Output

import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class JTableExample extends JFrame {

   String[][] data = {{"John", "Sutherland", "Student"},
         {"George", "Davies", "Student"},
         {"Melissa", "Anderson", "Associate"},
         {"Stergios", "Maglaras", "Developer"},
   };

   String[] fields = {"Name", "Surname", "Status"};

   public static void main(String[] args) {
      new JTableExample();
   }

   public JTableExample() {
      super();
      setSize(150, 150);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            dispose();
            System.exit(0);
         }
      });
      init();
      pack();
      setVisible(true);
   }

   private void init() {
      final CustomCellRenderer renderer = new CustomCellRenderer();
      JTable jt = new JTable(data, fields) {

         @Override
         public TableCellRenderer getCellRenderer(int row, int column) {
            return renderer;
         }

      };
      JScrollPane pane = new JScrollPane(jt);
      getContentPane().add(pane);
   }


   /**
    * @author suhas, orwellophile
    *
    */
   private class CustomCellRenderer extends DefaultTableCellRenderer {

      /**
       * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
       */
      public Component getTableCellRendererComponent(JTable table, Object value,
                                                     boolean isSelected, boolean hasFocus, int row, int column) {

         Component rendererComp = super.getTableCellRendererComponent(table, value,
               isSelected, hasFocus, row, column);

         // Very important to handle selected items (render them inversely colored)
         if (isSelected) {
            rendererComp.setBackground(getHuedColor(row).darker().darker());
            rendererComp.setForeground(getHuedColor(row).brighter().brighter());
         }
         else {
            rendererComp.setBackground(getHuedColor(row).brighter().brighter());
            rendererComp.setForeground(getHuedColor(row).darker().darker());
         }

         return rendererComp;
      }

      public Color getHuedColor(int hue) {
         return new Color(
               Color.HSBtoRGB(85 / 360f * hue, 0.7f, 1.0f
               ));
      }
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...