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

发布于 2024-12-10 15:45:10 字数 223 浏览 0 评论 0原文

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

JTable table = new JTable(4, 5);

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

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

Hi how to set gradient background to column header ? I have Jtable:

JTable table = new JTable(4, 5);

and try to setColor: table.getTableHeader().setBackground(Color.blue);

but with no succes color of column header is still same and these is just one color and I need gradient
thx for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小耗子 2024-12-17 15:45:11

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

您是否尝试过创建一个扩展 JTableHeader 并重写其 PaintComponent 方法的类?尝试一下,在该方法中创建一个 GradientPaint 对象或其变体之一,使用它来设置 Graphics2D 绘制的属性,然后使用组件的当前尺寸调用 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());
   }
}

Are you coding this with NetBean's code generation or are you coding your Swing by hand?

Have you tried creating a class that extends the JTableHeader and overrides its paintComponent method? Give it a try and in that method create a GradientPaint object or one of its variants use it to set the Graphics2D paint's property and then call fillRect using the current dimensions of the component to fill it with gradient color.

For example here are two ways to do this, one with a custom JTableHeader and one with a custom cell renderer that is used by the table header:

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());
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文