Results 1 to 3 of 3
  1. #1
    daniel2008 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default TableCellRender problem

    The question is simple:
    How can I set up a JTable TableCellRender so it can act for table cells like "fill color" property in excel. In other ways, if I select some cells from the table and I push a button to change their background with the desired background color.
    I've tryed for days to find out the solution but it seems that, besides setSelectedBackground() method I have found nothing. With the setSelectedBackgound method, the cells background can be changed but only until I select other cells again. So... as I said before I'm stuck!
    Thanks for any future answer I'll get from you!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    First of all, from the button's Action, you'll need to store the (row, column) coordinates of the cells to be colored, (along with the color, if multiple custom colors are to be supported) in some kind of data structure -- probably a List or a Map.

    Then you can either create a custom renderer or override prepareCellRenderer that queries the data structure and sets the renderer's background accordingly.

    db

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class RenderState {
        JTable table;
    
        private JScrollPane getTableComponent() {
            table = new JTable(getModel());
            Dimension d = table.getPreferredSize();
            table.setPreferredScrollableViewportSize(d);
            table.setDefaultRenderer(String.class, new ColorRenderer());
            table.setRowSelectionAllowed(true);
            table.setColumnSelectionAllowed(true);
            table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            return new JScrollPane(table);
        }
    
        private AbstractTableModel getModel() {
            return new AbstractTableModel() {
                public int getRowCount() { return 5; }
    
                public int getColumnCount() { return 4; }
    
                public Class getColumnClass(int col) { return String.class; }
    
                public Object getValueAt(int row, int col) {
                    return String.valueOf((row+1)) + (col+1);
                }
            };
        }
    
        private JPanel getColorPanel() {
            Color[] colors = { Color.pink, new Color(220,210,200) };
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JButton button = (JButton)e.getSource();
                    Color color = button.getBackground();
                    int[] rows = table.getSelectedRows();
                    int[] cols = table.getSelectedColumns();
                    ColorRenderer renderer =
                        (ColorRenderer)table.getDefaultRenderer(String.class);
                    for(int i = 0; i < rows.length; i++) {
                        for(int j = 0; j < cols.length; j++) {
                            if(table.isCellSelected(rows[i], cols[j])) {
                                int index = rows[i]*table.getColumnCount() + cols[j];
                                renderer.addCell(index, color);
                            }
                        }
                    }
                }
            };
            JPanel panel = new JPanel();
            for(int i = 0; i < colors.length; i++) {
                JButton button = new JButton();
                button.setBackground(colors[i]);
                button.setPreferredSize(new Dimension(100,20));
                button.addActionListener(al);
                panel.add(button);
            }
            return panel;
        }
    
        public static void main(String[] args) {
            RenderState test = new RenderState();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getTableComponent());
            f.add(test.getColorPanel(), "Last");
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class ColorRenderer extends DefaultTableCellRenderer {
        Map<Integer, Color> map = new HashMap<Integer, Color>();
    
        public Component getTableCellRendererComponent(JTable table,
                                                       Object value,
                                                       boolean isSelected,
                                                       boolean hasFocus,
                                                       int row, int column) {
            // Assuming rows and columns have not
            // been added or removed from table.
            int index = row*table.getColumnCount() + column;
            if(map.containsKey(Integer.valueOf(index))) {
                setBackground((Color)map.get(Integer.valueOf(index)));
            } else {
                setBackground(table.getBackground());
            }
            super.getTableCellRendererComponent(table, value, isSelected,
                                                hasFocus, row, column);
            return this;
        }
    
        public void addCell(int index, Color color) {
            map.put(Integer.valueOf(index), color);
        }
    
        public void removeCell(int index) {
            map.remove(Integer.valueOf(index));
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •