/* * WComboGridDemo.java * * Copyright 2000 Eliad Technologies, Inc. All Rights Reserved. * * This software is the proprietary information of Eliad Technologies, Inc. */ package example06; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import com.eliad.swing.*; import com.eliad.model.*; import com.eliad.model.defaults.*; /** * This class shows a simple JSmartGrid with combo boxes to edit particular data, * and icons. * @author Jean Hadji-Alilou * @author Daniel Rosenblatt * @version 1.0 */ public class WComboGridDemo extends JFrame { private JSmartGrid grid_; /** Creates a new WComboGridDemo */ public WComboGridDemo() { // title super("Grid Example with Combo boxes"); // Exit the application on a window closing event addWindowListener ( new WindowAdapter () { public void windowClosing(WindowEvent evt) { System.exit(0); } } ); final String path = "../images/"; ImageIcon bean = new ImageIcon(getClass().getResource(path+"Bean24.gif"), "bean"); ImageIcon ComposeMail= new ImageIcon(getClass().getResource(path+"ComposeMail24.gif"), "ComposeMail"); ImageIcon Cut = new ImageIcon(getClass().getResource(path+"Cut24.gif"), "Cut"); ImageIcon Delete = new ImageIcon(getClass().getResource(path+"Delete24.gif"), "Delete"); ImageIcon Edit = new ImageIcon(getClass().getResource(path+"Edit24.gif"), "Edit"); ImageIcon Find = new ImageIcon(getClass().getResource(path+"Find24.gif"), "Find"); ImageIcon Help = new ImageIcon(getClass().getResource(path+"Help24.gif"), "Help"); ImageIcon History = new ImageIcon(getClass().getResource(path+"History24.gif"), "History"); ImageIcon Home = new ImageIcon(getClass().getResource(path+"Home24.gif"), "Home"); ImageIcon Host = new ImageIcon(getClass().getResource(path+"Host24.gif"), "Host"); ImageIcon Information= new ImageIcon(getClass().getResource(path+"Information24.gif"), "Information"); ImageIcon Jar = new ImageIcon(getClass().getResource(path+"Jar24.gif"), "Jar"); ImageIcon Movie = new ImageIcon(getClass().getResource(path+"Movie24.gif"), "Movie"); ImageIcon Paste = new ImageIcon(getClass().getResource(path+"Paste24.gif"), "Paste"); ImageIcon Print = new ImageIcon(getClass().getResource(path+"Print24.gif"), "Print"); ImageIcon Save = new ImageIcon(getClass().getResource(path+"Save24.gif"), "Save"); ImageIcon TipOfTheDay= new ImageIcon(getClass().getResource(path+"TipOfTheDay24.gif"), "TipOfTheDay"); ImageIcon Volume = new ImageIcon(getClass().getResource(path+"Volume24.gif"), "Volume"); ImageIcon War = new ImageIcon(getClass().getResource(path+"War24.gif"), "War"); ImageIcon WebComponent= new ImageIcon(getClass().getResource(path+"WebComponent24.gif"), "WebComponent"); ImageIcon Zoom = new ImageIcon(getClass().getResource(path+"Zoom24.gif"), "Zoom"); NamedColor aqua = new NamedColor(new Color(127, 255, 212), "aqua"); NamedColor beige = new NamedColor(new Color(245, 245, 220), "beige"); NamedColor black = new NamedColor(Color.black, "black"); NamedColor blue = new NamedColor(new Color(0, 0, 222), "blue"); NamedColor eblue = new NamedColor(Color.blue, "eblue"); NamedColor jfcblue = new NamedColor(new Color(204, 204, 255), "jfcblue"); NamedColor jfcblue2 = new NamedColor(new Color(153, 153, 204), "jfcblue2"); NamedColor cybergreen = new NamedColor(Color.green.darker().brighter(), "cybergreen"); NamedColor darkgreen = new NamedColor(new Color(0, 100, 75), "darkgreen"); NamedColor forestgreen = new NamedColor(Color.green.darker(), "forestgreen"); NamedColor gray = new NamedColor(Color.gray, "gray"); NamedColor green = new NamedColor(Color.green, "green"); NamedColor orange = new NamedColor(new Color(255, 165, 0), "orange"); NamedColor purple = new NamedColor(new Color(160, 32, 240), "purple"); NamedColor red = new NamedColor(Color.red, "red"); NamedColor rustred = new NamedColor(Color.red.darker(), "rustred"); NamedColor sunpurple = new NamedColor(new Color(100, 100, 255), "sunpurple"); NamedColor suspectpink = new NamedColor(new Color(255, 105, 180), "suspectpink"); NamedColor turquoise = new NamedColor(new Color(0, 255, 255), "turquoise"); NamedColor violet = new NamedColor(new Color(238, 130, 238), "violet"); NamedColor yellow = new NamedColor(Color.yellow, "yellow"); // Create the dummy data (a few rows of names) final Object[][] data = { {green, War}, {blue, History}, {black, TipOfTheDay}, {red, War}, {blue, Movie}, {black, Delete}, {darkgreen, ComposeMail}, {blue, Home}, {yellow, War}, {violet, TipOfTheDay}, {purple, TipOfTheDay}, {blue, Zoom}, {blue, Print}, {green, Find}, {green, bean}, {blue, War}, {turquoise, Movie}, {blue, Save}, {orange, Delete}, {sunpurple, Volume}, {black, TipOfTheDay}, {jfcblue, Jar}, {beige, Help}, {green, War}, {forestgreen, Movie}, {suspectpink, Cut}, {cybergreen, Movie}, {rustred, Save}, {blue, Paste}, {jfcblue2, Delete}, {green, War}, {black, Cut}, {aqua, Edit}, {blue, Paste}, {blue, Paste}, {green, Zoom}, {darkgreen, Zoom}, {eblue, Delete}, {red, WebComponent}, {violet, Cut}, {blue, Jar}, {black, Host}, {green, Information}, {gray, History} }; final int maxRow = data.length; grid_ = new JSmartGrid(new GenericGridModel(maxRow,10) { public boolean isCellEditable(int row,int column) { return column != 1; } }); for (int col = 0; col < 2; col++) { if (col == 0) grid_.setColumnWidth(col, 130); else grid_.setColumnWidth(col, 30); for (int row = 0; row < maxRow; row++) grid_.setValueAt(data[row][col], row, col); } for (int col = 2; col < grid_.getColumnCount(); col++) { grid_.setColumnWidth(col, 50); for (int row = 0; row < grid_.getRowCount(); row++) grid_.setValueAt("" + row + col, row, col); } grid_.setAutoCreateColumnHeader(true); grid_.setAutoCreateRowHeader(true); grid_.setSelectionMode(GridSelectionModel.MULTIPLE_ROW_SELECTION); // Show colors by rendering them in their own color. class NamedColorCellRenderer extends DefaultGridCellRenderer { public NamedColorCellRenderer() { setHorizontalAlignment(JLabel.CENTER); } public void setValue(Object value) { NamedColor c = (NamedColor) value; setBackground(c); setForeground(c.getTextColor()); setText(c.toString()); } }; DefaultStyleModel styleModel = new DefaultStyleModel(); styleModel.setRenderer(NamedColor.class, new NamedColorCellRenderer()); // Create a combo box to show that you can use one in a table. JComboBox comboBox = new JComboBox(); comboBox.addItem(aqua); comboBox.addItem(beige); comboBox.addItem(black); comboBox.addItem(blue); comboBox.addItem(eblue); comboBox.addItem(jfcblue); comboBox.addItem(jfcblue2); comboBox.addItem(cybergreen); comboBox.addItem(darkgreen); comboBox.addItem(forestgreen); comboBox.addItem(gray); comboBox.addItem(green); comboBox.addItem(orange); comboBox.addItem(purple); comboBox.addItem(red); comboBox.addItem(rustred); comboBox.addItem(sunpurple); comboBox.addItem(suspectpink); comboBox.addItem(turquoise); comboBox.addItem(violet); comboBox.addItem(yellow); // Use the combo box as the editor in the "Favorite Color" column. styleModel.setEditor(NamedColor.class, new DefaultGridCellEditor(comboBox)); grid_.setStyleModel(styleModel); JScrollPane js = new JScrollPane(grid_); getContentPane().add(js, BorderLayout.CENTER); ((JComponent)getContentPane()).setPreferredSize(new Dimension(500, 400)); pack(); } class NamedColor extends Color { String name; public NamedColor(Color color, String name) { super(color.getRGB()); this.name = name; } public Color getTextColor() { int r = getRed(); int g = getGreen(); int b = getBlue(); if (r > 240 || g > 240) return Color.black; else return Color.white; } public String toString() { return name; } } /** Creates and displays a SimpleGrid frame */ public static void main(String[] args) { WComboGridDemo sg = new WComboGridDemo(); Dimension big=Toolkit.getDefaultToolkit().getScreenSize(); Dimension small=sg.getSize(); sg.setLocation((big.width-small.width)/2, (big.height-small.height)/2); sg.setVisible(true); } }