zoukankan      html  css  js  c++  java
  • Java Swing JTable 设置隔行变色

    设置JTable隔行变色,关键是重写方法prepareRenderer.

    ——————————–以下是演示代码,可自己运行看看——————————-

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.WindowConstants;
    import javax.swing.plaf.nimbus.NimbusLookAndFeel;
    import javax.swing.table.TableCellRenderer;
    
    public class JTableAlternateRowColors {
    
        public JTableAlternateRowColors() {
            initComponents();
        }
    
        public static void main(String[] args) {
    
            try {
                UIManager.setLookAndFeel(new NimbusLookAndFeel());
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new JTableAlternateRowColors();
                }
            });
        }
    
        private void initComponents() {
    
            final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
    
            MyTable table = new MyTable(new String[][]{
                        {"one", "two", "three"},
                        {"one", "two", "three"},
                        {"one", "two", "three"}
                    }, new String[]{"col1", "col2", "col3"});
    
            table.setFillsViewportHeight(true);//will fill the empty spaces too if any
    
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
            JScrollPane jScrollPane = new JScrollPane(table);
    
            jFrame.getContentPane().add(jScrollPane);
            jFrame.pack();
            jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jFrame.setVisible(true);
        }
    }
    
    class MyTable extends JTable {
    
        public MyTable(String[][] data, String[] fields) {
            super(data, fields);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            if (getFillsViewportHeight()) {
                paintEmptyRows(g);
            }
        }
    
        /**
         * Paints the backgrounds of the implied empty rows when the table model is
         * insufficient to fill all the visible area available to us. We don't
         * involve cell renderers, because we have no data.
         */
        protected void paintEmptyRows(Graphics g) {
            final int rowCount = getRowCount();
            final Rectangle clip = g.getClipBounds();
            if (rowCount * rowHeight < clip.height) {
                for (int i = rowCount; i <= clip.height / rowHeight; ++i) {
                    g.setColor(colorForRow(i));
                    g.fillRect(clip.x, i * rowHeight, clip.width, rowHeight);
                }
            }
        }
    
        /**
         * Returns the appropriate background color for the given row.
         */
        protected Color colorForRow(int row) {
            return (row % 2 == 0) ? Color.RED : Color.PINK;
        }
    
        /**
         * Shades alternate rows in different colors.
         */
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component c = super.prepareRenderer(renderer, row, column);
            if (isCellSelected(row, column) == false) {
                c.setBackground(colorForRow(row));
                c.setForeground(UIManager.getColor("Table.foreground"));
            } else {
                c.setBackground(UIManager.getColor("Table.selectionBackground"));
                c.setForeground(UIManager.getColor("Table.selectionForeground"));
            }
            return c;
        }
    }
  • 相关阅读:
    java中i++ 和 ++i的区别
    下载及配置Python+openCV
    Java 计算两个日期相差多少年月日
    conda创建、查看、删除虚拟环境
    MySQL Explain详解
    mysql实现group by后取各分组的最新一条
    Mybatis中的映射结果resutType和resultMap
    java8 Stream 快速实现List转map 、分组、过滤等操作
    LC1263-AI寻路优化: 距离优先bfs -> heuristic + A* -> tarjan + A*
    第8章复习
  • 原文地址:https://www.cnblogs.com/swbzmx/p/5605820.html
Copyright © 2011-2022 走看看