Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright (c) 2011 Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany.
   *  All rights reserved. This program and the accompanying materials
   *  are made available under the terms of the GNU Lesser Public License v2.1
   *  which accompanies this distribution, and is available at
   *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
   *
   *  Contributors:
   *      Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany - RMI Client, FileChooser and WebDAV
  */
 package de.ipk_gatersleben.bit.bi.edal.rmi.client.util;
 
 import java.awt.Color;
 
 
 
 public class EdalTable extends JTable{
 	private static final long serialVersionUID = 1L;
 	private static final Color EVEN_ROW_COLOR = new Color(241, 245, 250);
     private static final Color TABLE_GRID_COLOR = new Color(0xd9d9d9);
 
     private static final CellRendererPane CELL_RENDER_PANE = new CellRendererPane();
 
     public EdalTable(TableModel dm) {
         super(dm);
         init();
     }
 
     private void init() {
         //setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         setTableHeader(createTableHeader());
         getTableHeader().setReorderingAllowed(false);
         setOpaque(false);
         setGridColor();
         setIntercellSpacing(new Dimension(0, 0));
         // turn off grid painting as we'll handle this manually in order to paint
         // grid lines over the entire viewport.
         setShowGrid(false);
     }

    
Creates a JTableHeader that paints the table header background to the right of the right-most column if neccesasry.
 
     private JTableHeader createTableHeader() {
         return new JTableHeader(getColumnModel()) {
             @Override
             protected void paintComponent(Graphics g) {
                 super.paintComponent(g);
                 // if this JTableHEader is parented in a JViewport, then paint the
                 // table header background to the right of the last column if
                 // neccessary.
                 JViewport viewport = (JViewport.getParent();
                 if (viewport != null && .getWidth() < viewport.getWidth()) {
                     int x = .getWidth();
                     int width = viewport.getWidth() - .getWidth();
                     paintHeader(ggetTable(), xwidth);
                 }
             }
         };
     }

    
Paints the given JTable's table default header background at given x for the given width.
 
     private static void paintHeader(Graphics gJTable tableint xint width) {
         TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer();
         Component component = renderer.getTableCellRendererComponent(
                 table""falsefalse, -1, 2);
 
         component.setBounds(0,0,widthtable.getTableHeader().getHeight());
 
         ((JComponent)component).setOpaque(false);
         .paintComponent(gcomponentnullx, 0,
                 widthtable.getTableHeader().getHeight(), true);
     }
 
     @Override
     public Component prepareRenderer(TableCellRenderer rendererint row,
                                      int column) {
         Component component = super.prepareRenderer(rendererrowcolumn);
         // if the rendere is a JComponent and the given row isn't part of a
         // selection, make the renderer non-opaque so that striped rows show
        // through.
        if (component instanceof JComponent) {
            ((JComponent)component).setOpaque(getSelectionModel().isSelectedIndex(row));
        }
        return component;
    }
    // Stripe painting Viewport. //////////////////////////////////////////////

    
Creates a JViewport that draws a striped backgroud corresponding to the row positions of the given JTable.
    private static class StripedViewport extends JViewport {
        private final JTable fTable;
        public StripedViewport(JTable table) {
             = table;
            setOpaque(false);
            initListeners();
        }
        private void initListeners() {
            // install a listener to cause the whole table to repaint when
            // a column is resized. we do this because the extended grid
            // lines may need to be repainted. this could be cleaned up,
            // but for now, it works fine.
            PropertyChangeListener listener = createTableColumnWidthListener();
            for (int i=0; i<.getColumnModel().getColumnCount(); i++) {
                .getColumnModel().getColumn(i).addPropertyChangeListener(listener);
            }
        }
            return new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    repaint();
                }
            };
        }
        @Override
        protected void paintComponent(Graphics g) {
            paintStripedBackground(g);
            paintVerticalGridLines(g);
            super.paintComponent(g);
        }
        private void paintStripedBackground(Graphics g) {
            // get the row index at the top of the clip bounds (the first row
            // to paint).
            int rowAtPoint = .rowAtPoint(g.getClipBounds().getLocation());
            // get the y coordinate of the first row to paint. if there are no
            // rows in the table, start painting at the top of the supplied
            // clipping bounds.
            int topY = rowAtPoint < 0
                    ? g.getClipBounds(). : .getCellRect(rowAtPoint,0,true).;
            // create a counter variable to hold the current row. if there are no
            // rows in the table, start the counter at 0.
            int currentRow = rowAtPoint < 0 ? 0 : rowAtPoint;
            while (topY < g.getClipBounds(). + g.getClipBounds().) {
                int bottomY = topY + .getRowHeight();
                g.setColor(getRowColor(currentRow));
                g.fillRect(g.getClipBounds().topYg.getClipBounds().bottomY);
                topY = bottomY;
                currentRow ++;
            }
        }
        private Color getRowColor(int row) {
            return row % 2 == 0 ?  : getBackground();
        }
        private void paintVerticalGridLines(Graphics g) {
            // paint the column grid dividers for the non-existent rows.
            int x = 0;
            for (int i = 0; i < .getColumnCount(); i++) {
                TableColumn column = .getColumnModel().getColumn(i);
                // increase the x position by the width of the current column.
                x += column.getWidth();
                g.setColor();
                // draw the grid line (not sure what the -1 is for, but BasicTableUI
                // also does it.
                g.drawLine(x - 1, g.getClipBounds().x - 1, getHeight());
            }
        }
    }
    public static JScrollPane createStripedJScrollPane(JTable table) {
        JScrollPane scrollPane =  new JScrollPane(table);
        scrollPane.setViewport(new StripedViewport(table));
        scrollPane.getViewport().setView(table);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.setCorner(.,
                createCornerComponent(table));
        return scrollPane;
    }

    
Creates a component that paints the header background for use in a JScrollPane corner.
    private static JComponent createCornerComponent(final JTable table) {
        return new JComponent() {
            @Override
            protected void paintComponent(Graphics g) {
                paintHeader(gtable, 0, getWidth());
            }
        };
    }