0

4 つの列を持つテーブルがあり、各列の見出しを異なる色にしたいと考えています。列の残りのセルではなく、列の見出しにのみ色を付けたいです。DefaultTableCellRenderer を使用してみましたが、列見出しを除くすべてのセルが赤くなりました。各列の見出しに個別に色を割り当てるには、コード (以下) で何を変更できますか?

class CustomRenderer extends DefaultTableCellRenderer{

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, 3, 3);
    c.setBackground(new java.awt.Color(255,72,72));
    return c;
}

}

    table.setDefaultRenderer(Object.class, new CustomRenderer());
4

3 に答える 3

1

個々の色を簡単に設定することはできません。

// Get the column header for your specific column
JTableHeader th = table.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(column);

/* Create a new renderer for column headers, and
pass in the column number so you can differntiate
which column will be which color. */
tc.setCellRenderer(new headerCellRenderer(column));

ただし、これはニーズに合うはずです。

于 2012-07-19T19:26:54.497 に答える
0

緑色のチェック マークが付いた上記のコードは正しいですが、ヘッダー以外のセルのプロパティを設定するだけです。TableColumn クラスのメソッド setCellRenderer() と setHeaderRenderer() には違いがあることに注意してください。

複数の色のヘッダー セルとヘッダー以外の黄色のセルを含む結果のテーブル

/********************* FULL WORKING IMPLEMENTATION. *************************
  -Table with custom header (multiple colors) and custom non-header cells.
  -Cells' manipulated properties are colors, font, opacity and border types.*/

        import java.awt.Color;
        import java.awt.Component;
        import java.awt.Dimension;
        import java.awt.Font;
        import java.awt.GridLayout;
        import javax.swing.BorderFactory;
        import javax.swing.JComponent;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JPanel;
        import javax.swing.JScrollPane;
        import javax.swing.JTable;
        import javax.swing.border.Border;
        import javax.swing.table.AbstractTableModel;
        import javax.swing.table.TableCellRenderer;
        import javax.swing.table.TableColumn;

        public class CustomHeaderTable  extends JPanel
        {
            CustomHeaderTable()
            {
                super(new GridLayout(1,0));

                JTable table = new JTable(new MyTableModel());
                table.setPreferredScrollableViewportSize(table.getPreferredSize());
                table.setFillsViewportHeight(true);
                table.setPreferredScrollableViewportSize(new Dimension(500, 70));

                //Setting header cells
                CustomCellRenderer customHeaderCellRenderer = 
                        new CustomCellRenderer(Color.white,
                                               Color.red,
                                               new Font("Consolas",Font.BOLD,14),
                                               BorderFactory.createEtchedBorder(),
                                               true);

                for(int columnIndex = 0; columnIndex < table.getTableHeader().getColumnModel().getColumnCount(); columnIndex++)
                {
                    TableColumn tc = table.getTableHeader().getColumnModel().getColumn(columnIndex);    
                    tc.setHeaderRenderer(customHeaderCellRenderer);
                }

                //Setting non-heather's cells colors, font, borders, and opacity
                CustomCellRenderer customNonHeaderCellRenderer = 
                        new CustomCellRenderer(Color.blue,
                                               Color.yellow,
                                               new Font("Courier",Font.PLAIN,12),
                                               BorderFactory.createLineBorder(Color.gray),
                                               true);

                for(int columnIndex = 0; columnIndex < table.getTableHeader().getColumnModel().getColumnCount(); columnIndex++)
                {
                    TableColumn tc = table.getTableHeader().getColumnModel().getColumn(columnIndex);    
                    tc.setCellRenderer(customNonHeaderCellRenderer);
                }

                JScrollPane scrollpane = new JScrollPane(table);
                add(scrollpane);
            }

            public class CustomCellRenderer extends JLabel implements TableCellRenderer
            {
                public CustomCellRenderer(Color foregroundColor, Color backgroundColor, Font font, Border border, boolean isOpaque)
                {
                    setFont(font);
                    setOpaque(isOpaque);
                    setForeground(foregroundColor);
                    setBackground(backgroundColor);
                    setBorder(border); 
                }

                @Override
                public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column) 
                {
                    setText(value.toString());
                    return this;
                }
            }

            class MyTableModel extends AbstractTableModel 
            {
                private String[] columnNames = {"First Name",
                                                "Last Name",
                                                "Sport",
                                                "# of Years",`enter code here`
                                                "Vegetarian"};
                private Object[][] data = {
                {"Kathy", "Smith",
                 "Snowboarding", new Integer(5), false},
                {"John", "Doe",
                 "Rowing", new Integer(3), true},
                {"Sue", "Black",
                 "Knitting", new Integer(2), false},
                {"Jane", "White",
                 "Speed reading", new Integer(20), true},
                {"Joe", "Brown",
                 "Pool", new Integer(10), false}
                };

                public int getColumnCount() 
                {
                    return columnNames.length;
                }

                public int getRowCount() 
                {
                    return data.length;
                }

                public String getColumnName(int col) 
                {
                    return columnNames[col];
                }

                public Object getValueAt(int row, int col) 
                {
                    return data[row][col];
                }

                public Class getColumnClass(int c) 
                {
                    return getValueAt(0, c).getClass();
                }

                public boolean isCellEditable(int row, int col) 
                {
                    if (col < 3) 
                    {
                        return false;
                    } 
                    else 
                    {
                        return true;
                    }
                }
            }

            public static void createAndShowGUI()
            {
                JFrame frame = new JFrame("CustomHeaderTable");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JComponent newContentPane = new CustomHeaderTable();
                newContentPane.setOpaque(true);
                frame.setContentPane(newContentPane);

                frame.pack();
                frame.setLocationRelativeTo( null );
                frame.setVisible(true);
            }

            public static void main(String[] args) {
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        createAndShowGUI();
                    }
                });
            }
        }
于 2019-10-22T03:35:46.597 に答える