1 /*
   2  * Copyright (c) 2007, 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 import javax.swing.*;
  25 import javax.swing.table.AbstractTableModel;
  26 import javax.swing.table.TableModel;
  27 import java.awt.*;
  28 import java.util.Locale;
  29 
  30 /*
  31  * @test
  32  * @summary Check that JTable constructor and methods do not throw unexpected
  33  *          exceptions in headless mode
  34  * @run main/othervm -Djava.awt.headless=true HeadlessJTable
  35  */
  36 
  37 public class HeadlessJTable {
  38     public static void main(String args[]) {
  39         JTable t;
  40         t = new JTable();
  41         final Object[][] data = {
  42                 {"cell_1_1", "cell_1_2", "cell_1_3"},
  43                 {"cell_2_1", "cell_2_2", "cell_2_3"},
  44                 {"cell_3_1", "cell_3_2", "cell_3_3"},
  45                 {"cell_4_1", "cell_4_2", "cell_4_3"},
  46         };
  47 
  48         TableModel dataModel = new AbstractTableModel() {
  49             public int getColumnCount() { return 3; }
  50             public int getRowCount() { return data.length;}
  51             public Object getValueAt(int row, int col) {return data[row][col];}
  52         };
  53         t = new JTable(dataModel);
  54 
  55         t.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  56         t.getAutoResizeMode();
  57         t.getColumnModel().getColumn(1).setMinWidth(5);
  58         t.getTableHeader().setReorderingAllowed(false);
  59         t.getTableHeader().setResizingAllowed(false);
  60 
  61         t.getAccessibleContext();
  62         t.isFocusTraversable();
  63         t.setEnabled(false);
  64         t.setEnabled(true);
  65         t.requestFocus();
  66         t.requestFocusInWindow();
  67         t.getPreferredSize();
  68         t.getMaximumSize();
  69         t.getMinimumSize();
  70         t.contains(1, 2);
  71         Component c1 = t.add(new Component(){});
  72         Component c2 = t.add(new Component(){});
  73         Component c3 = t.add(new Component(){});
  74         Insets ins = t.getInsets();
  75         t.getAlignmentY();
  76         t.getAlignmentX();
  77         t.getGraphics();
  78         t.setVisible(false);
  79         t.setVisible(true);
  80         t.setForeground(Color.red);
  81         t.setBackground(Color.red);
  82         for (String font : Toolkit.getDefaultToolkit().getFontList()) {
  83             for (int j = 8; j < 17; j++) {
  84                 Font f1 = new Font(font, Font.PLAIN, j);
  85                 Font f2 = new Font(font, Font.BOLD, j);
  86                 Font f3 = new Font(font, Font.ITALIC, j);
  87                 Font f4 = new Font(font, Font.BOLD | Font.ITALIC, j);
  88 
  89                 t.setFont(f1);
  90                 t.setFont(f2);
  91                 t.setFont(f3);
  92                 t.setFont(f4);
  93 
  94                 t.getFontMetrics(f1);
  95                 t.getFontMetrics(f2);
  96                 t.getFontMetrics(f3);
  97                 t.getFontMetrics(f4);
  98             }
  99         }
 100         t.enable();
 101         t.disable();
 102         t.reshape(10, 10, 10, 10);
 103         t.getBounds(new Rectangle(1, 1, 1, 1));
 104         t.getSize(new Dimension(1, 2));
 105         t.getLocation(new Point(1, 2));
 106         t.getX();
 107         t.getY();
 108         t.getWidth();
 109         t.getHeight();
 110         t.isOpaque();
 111         t.isValidateRoot();
 112         t.isOptimizedDrawingEnabled();
 113         t.isDoubleBuffered();
 114         t.getComponentCount();
 115         t.countComponents();
 116         t.getComponent(1);
 117         t.getComponent(2);
 118         Component[] cs = t.getComponents();
 119         t.getLayout();
 120         t.setLayout(new FlowLayout());
 121         t.doLayout();
 122         t.layout();
 123         t.invalidate();
 124         t.validate();
 125         t.remove(0);
 126         t.remove(c2);
 127         t.removeAll();
 128         t.preferredSize();
 129         t.minimumSize();
 130         t.getComponentAt(1, 2);
 131         t.locate(1, 2);
 132         t.getComponentAt(new Point(1, 2));
 133         t.isFocusCycleRoot(new Container());
 134         t.transferFocusBackward();
 135         t.setName("goober");
 136         t.getName();
 137         t.getParent();
 138         t.getGraphicsConfiguration();
 139         t.getTreeLock();
 140         t.getToolkit();
 141         t.isValid();
 142         t.isDisplayable();
 143         t.isVisible();
 144         t.isShowing();
 145         t.isEnabled();
 146         t.enable(false);
 147         t.enable(true);
 148         t.enableInputMethods(false);
 149         t.enableInputMethods(true);
 150         t.show();
 151         t.show(false);
 152         t.show(true);
 153         t.hide();
 154         t.getForeground();
 155         t.isForegroundSet();
 156         t.getBackground();
 157         t.isBackgroundSet();
 158         t.getFont();
 159         t.isFontSet();
 160         Container c = new Container();
 161         c.add(t);
 162         t.getLocale();
 163         for (Locale locale : Locale.getAvailableLocales())
 164             t.setLocale(locale);
 165 
 166         t.getColorModel();
 167         t.getLocation();
 168 
 169         boolean exceptions = false;
 170         try {
 171             t.getLocationOnScreen();
 172         } catch (IllegalComponentStateException e) {
 173             exceptions = true;
 174         }
 175         if (!exceptions)
 176             throw new RuntimeException("IllegalComponentStateException did not occur when expected");
 177 
 178         t.location();
 179         t.setLocation(1, 2);
 180         t.move(1, 2);
 181         t.setLocation(new Point(1, 2));
 182         t.getSize();
 183         t.size();
 184         t.setSize(1, 32);
 185         t.resize(1, 32);
 186         t.setSize(new Dimension(1, 32));
 187         t.resize(new Dimension(1, 32));
 188         t.getBounds();
 189         t.bounds();
 190         t.setBounds(10, 10, 10, 10);
 191         t.setBounds(new Rectangle(10, 10, 10, 10));
 192         t.isLightweight();
 193         t.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
 194         t.getCursor();
 195         t.isCursorSet();
 196         t.inside(1, 2);
 197         t.contains(new Point(1, 2));
 198         t.isFocusable();
 199         t.setFocusable(true);
 200         t.setFocusable(false);
 201         t.transferFocus();
 202         t.getFocusCycleRootAncestor();
 203         t.nextFocus();
 204         t.transferFocusUpCycle();
 205         t.hasFocus();
 206         t.isFocusOwner();
 207         t.toString();
 208         t.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 209         t.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
 210         t.setComponentOrientation(ComponentOrientation.UNKNOWN);
 211         t.getComponentOrientation();
 212     }
 213 }