如何将渐变背景添加到JTable列标题?

时间:2011-10-18 22:34:15

标签: java swing jtable gradient jtableheader

您好如何将渐变背景设置为列标题?我有Jtable:

JTable table = new JTable(4, 5);

并尝试setColor:table.getTableHeader().setBackground(Color.blue);

但没有成功的列标题颜色仍然相同,这些只是一种颜色,我需要渐变 请求帮助

1 个答案:

答案 0 :(得分:7)

您是使用NetBean的代码生成编码还是手动编写Swing代码?

您是否尝试过创建一个扩展JTableHeader的类并重写其paintComponent方法?尝试一下,在该方法中创建一个GradientPaint对象或其变体之一使用它来设置Graphics2D paint的属性,然后使用组件的当前尺寸调用fillRect以用渐变颜色填充它。

例如,这里有两种方法,一种是使用自定义JTableHeader,另一种是使用表头使用的自定义单元格渲染器:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class GradientHeader {
   private static void createAndShowGui() {
      JTable table1 = new JTable();
      GradientTableHeader gradientTableHeader = new GradientTableHeader();
      gradientTableHeader.setColumnModel(table1.getColumnModel());
      table1.setTableHeader(gradientTableHeader);

      DefaultTableModel model = new DefaultTableModel(new Integer[][] {
            { 1, 2 }, { 3, 4 } }, new String[] { "A", "B" });

      table1.setModel(model);

      JTable table2 = new JTable(model);
      table2.getTableHeader().setDefaultRenderer(new MyCellRenderer());

      JPanel mainPanel = new JPanel(new GridLayout(1, 0));
      mainPanel.add(new JScrollPane(table1));
      mainPanel.add(new JScrollPane(table2));

      JFrame frame = new JFrame("GradientHeader");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class GradientTableHeader extends JTableHeader {
   private static final Color COLOR_1 = new Color(255, 0, 0, 240);
   private static final Color COLOR_2 = new Color(0, 0, 255, 120);
   private static final float SIDE = 50;
   private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE,
         SIDE, COLOR_2, true);

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setPaint(gradientPaint);
      g2.fillRect(0, 0, getWidth(), getHeight());
   }
}

class MyCellRenderer extends JPanel implements TableCellRenderer {
   private static final Color COLOR_1 = new Color(255, 0, 0, 200);
   private static final Color COLOR_2 = new Color(0, 0, 255, 200);
   private static final float SIDE = 50;
   private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE,
         SIDE, COLOR_2, true);
   private JLabel label = new JLabel();

   public MyCellRenderer() {
      setOpaque(true);
      setLayout(new BorderLayout());
      add(label, BorderLayout.CENTER);
      label.setHorizontalAlignment(SwingConstants.CENTER);
   }

   @Override
   public Component getTableCellRendererComponent(JTable table, Object value,
         boolean isSelected, boolean hasFocused, int arg4, int arg5) {
      label.setText(value.toString());
      return this;
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setPaint(gradientPaint);
      g2.fillRect(0, 0, getWidth(), getHeight());
   }
}