1 /*
   2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 6937798
  26    @summary Nimbus: Issues with JTable grid
  27    @author Alexander Potochkin
  28    @modules java.desktop/com.sun.java.swing.plaf.nimbus
  29    @run main bug6937798
  30 */
  31 
  32 import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  33 
  34 import javax.swing.*;
  35 import javax.swing.table.AbstractTableModel;
  36 import javax.swing.table.TableModel;
  37 import java.awt.*;
  38 import java.awt.image.BufferedImage;
  39 
  40 public class bug6937798 {
  41 
  42     public static void main(String... args) throws Exception {
  43         UIManager.setLookAndFeel(new NimbusLookAndFeel());
  44         SwingUtilities.invokeAndWait(new Runnable() {
  45             public void run() {
  46                 new bug6937798();
  47             }
  48         });
  49     }
  50 
  51     public bug6937798() {
  52         final JTable table = createCountryTable();
  53         table.setShowGrid(true);
  54         table.setSize(100, 100);
  55 
  56         BufferedImage im = new BufferedImage(table.getWidth(), table.getHeight(), BufferedImage.TYPE_INT_ARGB);
  57         Graphics g = im.getGraphics();
  58         table.print(g);
  59         g.dispose();
  60 
  61         for (int i = 0; i < im.getHeight(); i++) {
  62             for (int j = 0; j < im.getWidth(); j++) {
  63                 if (im.getRGB(i, j) == table.getGridColor().getRGB()) {
  64                     System.out.println("got it!");
  65                     return;
  66                 }
  67             }
  68         }
  69         throw new RuntimeException("no table's grid detected....");
  70     }
  71 
  72     protected JTable createCountryTable() {
  73         // Column headers
  74         final String
  75                 [] headers = {
  76                 "Name", "Capital City", "Language(s)", "Monetary Unit(s)", "EC Member"
  77         };
  78 
  79         // Table data
  80         final Object[][] data = {
  81                 {"Albania", "Tirane", "Albanian, Greek", "Lek", new Boolean(false)},
  82                 {"Andorra", "Andorra la Vella", "Catalan, French, Spanish", "French Franc, Spanish Peseta", new Boolean(false)},
  83                 {"Austria", "Vienna", "German, Slovenian, Croatian", "Schilling", new Boolean(false)},
  84                 {"Belarus", "Minsk", "Byelorussian, Russian", "Belarusian Rubel", new Boolean(false)},
  85                 {"Belgium", "Brussels", "French, Flemish, German", "Belgian Franc", new Boolean(true)},
  86                 {"Bosnia & Herzegovina", "Sarajevo", "Serbo-Croatian", "Dinar", new Boolean(false)},
  87                 {"Bulgaria", "Sofia", "Bulgarian, Turkish", "Lev", new Boolean(false)},
  88                 {"Croatia", "Zagreb", "Serbo-Croatian", "Croatian Kuna", new Boolean(false)},
  89                 {"Czech Republic", "Prague", "Czech, Slovak", "Koruna", new Boolean(false)},
  90                 {"Denmark", "Copenhagen", "Danish", "Krone", new Boolean(true)},
  91                 {"Estonia", "Tallinn", "Estonian, Latvian, Lithuanian, Russian", "Estonian Kroon", new Boolean(false)},
  92                 {"Finland", "Helsinki", "Finnish, Swedish, Lappish", "Markka", new Boolean(false)},
  93                 {"France", "Paris", "French", "Franc", new Boolean(true)},
  94                 {"Germany", "Berlin", "German", "Deutsche Mark", new Boolean(true)},
  95                 {"Greece", "Athens", "Greek, English, French", "Drachma", new Boolean(true)},
  96                 {"Hungary", "Budapest", "Hungarian", "Forint", new Boolean(false)},
  97                 {"Iceland", "Reykjavik", "Icelandic", "Icelandic Krona", new Boolean(false)},
  98                 {"Ireland", "Dublin", "Irish, English", "Pound", new Boolean(true)},
  99                 {"Italy", "Rome", "Italian", "Lira", new Boolean(true)},
 100                 {"Latvia", "Riga", "Lettish, Lithuanian, Russian", "Lat", new Boolean(false)},
 101                 {"Liechtenstein", "Vaduz", "German", "Swiss Franc", new Boolean(false)},
 102                 {"Lithuania", "Vilnius", "Lithuanian, Polish, Russian", "Lita", new Boolean(false)},
 103                 {"Luxembourg", "Luxembourg", "French, German, Letzeburgesch", "Luxembourg Franc", new Boolean(true)},
 104                 {"Macedonia", "Skopje", "Macedonian, Albanian, Turkish, Serbo-Croatian", "Denar", new Boolean(false)},
 105                 {"Malta", "Valletta", "Maltese, English", "Maltese Lira", new Boolean(false)},
 106                 {"Moldova", "Chisinau", "Moldovan, Russian", "Leu", new Boolean(false)},
 107                 {"Monaco", "Monaco", "French, English, Italian", "French Franc", new Boolean(false)},
 108                 {"the Netherlands", "Amsterdam", "Dutch", "Guilder", new Boolean(true)},
 109                 {"Norway", "Oslo", "Norwegian", "Krone", new Boolean(false)},
 110                 {"Poland", "Warsaw", "Polish", "Zloty", new Boolean(false)},
 111                 {"Portugal", "Lisbon", "Portuguese", "Escudo", new Boolean(true)},
 112                 {"Romania", "Bucharest", "Romanian", "Leu", new Boolean(false)},
 113                 {"Russia", "Moscow", "Russian", "Ruble", new Boolean(false)},
 114                 {"San Marino", "San Marino", "Italian", "Italian Lira", new Boolean(false)},
 115                 {"Slovakia", "Bratislava", "Slovak, Hungarian", "Koruna", new Boolean(false)},
 116                 {"Slovenia", "Ljubljana", "Slovenian, Serbo-Croatian", "Tolar", new Boolean(false)},
 117                 {"Spain", "Madrid", "Spanish", "Peseta", new Boolean(true)},
 118                 {"Sweden", "Stockholm", "Swedish", "Krona", new Boolean(false)},
 119                 {"Switzerland", "Bern", "German, French, Italian", "Swiss Franc", new Boolean(false)},
 120                 {"Turkey", "Ankara", "Turkish", "Turkish Lira", new Boolean(false)},
 121                 {"Ukraine", "Kiev", "Ukranian, Russian, Romanian, Polish, Hungarian", "Hryvnia", new Boolean(false)},
 122                 {"United Kingdom", "London", "English, Welsh", "British Pound", new Boolean(true)},
 123                 {"Yugoslavia", "Belgrade", "Serbo-Croatian, Slovenian, Macedonian", "Dinar", new Boolean(false)},
 124         };
 125 
 126         // Table model
 127         TableModel dataModel = new AbstractTableModel() {
 128 
 129             public int getColumnCount() {
 130                 return headers.length;
 131             }
 132 
 133             public int getRowCount() {
 134                 return data.length;
 135             }
 136 
 137             public Object getValueAt(int row, int col) {
 138                 return data[row][col];
 139             }
 140 
 141             public String getColumnName(int column) {
 142                 return headers[column];
 143             }
 144 
 145             public Class getColumnClass(int col) {
 146                 return getValueAt(0, col).getClass();
 147             }
 148 
 149             public void setValueAt(Object aValue, int row, int column) {
 150                 data[row][column] = aValue;
 151             }
 152 
 153             public boolean isCellEditable(int row, int col) {
 154                 return (col == 4);
 155             }
 156         };
 157 
 158         // Create table with table model
 159         JTable countryTable = new JTable(dataModel);
 160         countryTable.setGridColor(Color.pink);
 161         countryTable.setBackground(Color.white);
 162         countryTable.setForeground(Color.black);
 163         return countryTable;
 164     }
 165 }