简体   繁体   中英

i want to color my Cells in Jtable Java?

i want to color my Cells in Jtable Java ? like this in Image:

http://i.stack.imgur.com/yuzZR.png

can sameone help me thanks !

the code i tried last

import javax.swing.*;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.DefaultTableCellRenderer;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

public class Main
{
        /**
         * @param args
         */
        public static void main(String[] args)
        {
                JFrame frame = new JFrame();
                frame.add(new JComboxTable(), BorderLayout.NORTH);
                frame.setVisible(true);
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
}

class JComboxTable extends JTable
{

        public static JComboBox cb;

        public JComboxTable()
        {
                TableModel tree = new TableModel();
                TableRenderer render = new TableRenderer();
                setModel(tree);
                setDefaultRenderer(Object.class, render);
                tree.fireTableDataChanged();
        }

}

class TableModel extends AbstractTableModel
{

        public int getColumnCount()
        {
                return 2;
        }

        public int getRowCount()
        {
                return 5;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex)
        {
                if (columnIndex == 0)
                {
                        return String.class;
                }
                else
                {
                        return Color.class;
                }
        }

        public Object getValueAt(int rowIndex, int columnIndex)
        {
                if (columnIndex == 0)
                {
                        return "Salut" + rowIndex;
                }
                else
                {
                        if( ( rowIndex % 2 ) == 0 )
                        {
                                return  Color.RED ;
                        }else
                        {
                                return  Color.BLUE ;
                        }
                }
        }

}

class TableRenderer extends DefaultTableCellRenderer
{

        public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column)
        {
                if (value instanceof Color)
                {
                        setBackground((Color) value);
                        setText("");
                }
                else
                {
                        setBackground( (Color)table.getModel().getValueAt(row, 1) );
                        setText(  (String)value );
                }

                return this;
        }

}
}

Use a custom renderer.

Read the section from the Swing tutorial on Using Custom Renderers for a working example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM