www.pudn.com > tame.rar > MixedExample.java


/* (swing1.1beta3) */
package tame.examples;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

import tame.colorchooser.TextColorChooser;
import tame.table.AttributiveCellRenderer;
import tame.table.AttributiveCellTableModel;
import tame.table.CellAttribute;
import tame.table.CellFont;
import tame.table.CellSpan;
import tame.table.ColoredCell;
import tame.table.MultiSpanCellTable;

/**
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.table.DefaultTableModel.getColumnCount(DefaultTableModel.java:575)
@author Nobuo Tamemasa @version 1.0 11/22/98 */ public class MixedExample extends JFrame { public MixedExample() { super( "Mixed Example" ); AttributiveCellTableModel ml = new AttributiveCellTableModel(20,5) { public Object getValueAt(int row, int col) { return "" + row + ","+ col; } }; CellAttribute cellAtt = ml.getCellAttribute(); MultiSpanCellTable table = new MultiSpanCellTable( ml ); table.setCellSelectionEnabled(true); table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); table.setDefaultRenderer(Object.class ,new AttributiveCellRenderer()); JScrollPane scroll = new JScrollPane( table ); ColorPanel colorPanel = new ColorPanel(table,(ColoredCell)cellAtt); FontPanel fontPanel = new FontPanel(table, (CellFont)cellAtt); SpanPanel spanPanel = new SpanPanel(table, (CellSpan)cellAtt); Box boxAtt = new Box(BoxLayout.Y_AXIS); boxAtt.add(colorPanel); boxAtt.add(fontPanel); boxAtt.add(spanPanel); Box box = new Box(BoxLayout.X_AXIS); box.add(scroll); box.add(new JSeparator(SwingConstants.HORIZONTAL)); box.add(boxAtt); getContentPane().add( box ); setSize( 400, 300 ); setVisible(true); } class ColorPanel extends JPanel { JTable table; ColoredCell cellAtt; ColorPanel(final JTable table, final ColoredCell cellAtt) { this.table = table; this.cellAtt = cellAtt; setLayout(new GridLayout(2,1)); setBorder(BorderFactory.createTitledBorder("Color")); JButton b_fore = new JButton("Foreground"); b_fore.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { changeColor(true); } }); JButton b_back = new JButton("Background"); b_back.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { changeColor(false); } }); JPanel p_buttons = new JPanel(); add(b_fore); add(b_back); } private final void changeColor(boolean isForeground) { int[] columns = table.getSelectedColumns(); int[] rows = table.getSelectedRows(); if ((rows == null) || (columns == null)) return; if ((rows.length<1)||(columns.length<1)) return; Color target = cellAtt.getForeground(rows[0], columns[0]); Color reference = cellAtt.getBackground(rows[0], columns[0]); for (int i=0;i