/*
 * (swing1.1beta3)
 */
package jp.gr.java_conf.tame.swing.table; 
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
/**
 * @version 1.0 11/22/98
 */
public class AttributiveCellRenderer extends JLabel
    implements TableCellRenderer {
  protected static Border noFocusBorder; 
 
  public AttributiveCellRenderer() {
    noFocusBorder = new EmptyBorder(1, 2, 1, 2);
    setOpaque(true);
    setBorder(noFocusBorder);  
  }  public Component getTableCellRendererComponent(JTable table, Object value,
                 boolean isSelected, boolean hasFocus, int row, int column) {
    Color foreground = null;
    Color background = null;
    Font font = null;
    TableModel model = table.getModel();
    if (model instanceof AttributiveCellTableModel) {
      CellAttribute cellAtt = ((AttributiveCellTableModel)model).getCellAttribute();
      if (cellAtt instanceof ColoredCell) {
foreground = ((ColoredCell)cellAtt).getForeground(row,column);
background = ((ColoredCell)cellAtt).getBackground(row,column);
      }
      if (cellAtt instanceof CellFont) {
font = ((CellFont)cellAtt).getFont(row,column);
      }
    }
    if (isSelected) {
      setForeground((foreground != null) ? foreground
                          : table.getSelectionForeground());
      setBackground(table.getSelectionBackground());
    } else {
      setForeground((foreground != null) ? foreground 
  : table.getForeground());
      setBackground((background != null) ? background 
  : table.getBackground());
    }
    setFont((font != null) ? font : table.getFont());
    
    if (hasFocus) {
      setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
      if (table.isCellEditable(row, column)) {
setForeground((foreground != null) ? foreground
              : UIManager.getColor("Table.focusCellForeground") );
setBackground( UIManager.getColor("Table.focusCellBackground") );
      }
    } else {
      setBorder(noFocusBorder);
    }
    setValue(value);        
    return this;
  }
    
  protected void setValue(Object value) {
    setText((value == null) ? "" : value.toString());
  }
}

解决方案 »

  1.   

    /*
     * (swing1.1beta3)
     */
     
    package jp.gr.java_conf.tame.swing.colorchooser;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /**
     * @version 1.0 11/22/98
     */
    class TextPreviewLabel extends JLabel {
      private String sampleText = "  Sample Text  Sample Text  ";
      boolean isForgroundSelection;
      
      public TextPreviewLabel() {
        this(Color.black, Color.white, true);
      }
      
      public TextPreviewLabel(Color fore, Color back, boolean isForgroundSelection) {
        setOpaque(true);
        setForeground(fore);
        setBackground(back);
        this.isForgroundSelection = isForgroundSelection;
        setText(sampleText);
      }
      
      public void setForeground(Color col) {
        if (isForgroundSelection) {
          super.setForeground(col);
        } else {
          super.setBackground(col);
        }
      }
      
    }
    class ColorChooserDialog extends JDialog {
        private Color initialColor;
        private Color retColor;
        private JColorChooser chooserPane;    public ColorChooserDialog(Component c, String title,
                              final    JColorChooser chooserPane) {
            super(JOptionPane.getFrameForComponent(c), title, true);
            setResizable(false);        this.chooserPane = chooserPane; String okString = UIManager.getString("ColorChooser.okText");
    String cancelString = UIManager.getString("ColorChooser.cancelText");
    String resetString = UIManager.getString("ColorChooser.resetText");        Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());
            contentPane.add(chooserPane, BorderLayout.CENTER);        JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
            JButton okButton = new JButton(okString);
    getRootPane().setDefaultButton(okButton);
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    retColor = chooserPane.getColor();
                    setVisible(false);
                }
            });
            buttonPane.add(okButton);        JButton cancelButton = new JButton(cancelString);
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    retColor = null;
                    setVisible(false);
                }
            });
            buttonPane.add(cancelButton);        JButton resetButton = new JButton(resetString);
            resetButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   chooserPane.setColor(initialColor);
               }
            });
            buttonPane.add(resetButton);
            contentPane.add(buttonPane, BorderLayout.SOUTH);        pack();
            setLocationRelativeTo(c);
            addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                setVisible(false);
              }
            });
        }
        
      public Color getColor() {
        return retColor;
      }}
    public class TextColorChooser extends JColorChooser {
     
      public TextColorChooser(Color target, Color reference, boolean isForgroundSelection) {
        super(target);
        if (isForgroundSelection) {
          setPreviewPanel(new TextPreviewLabel(target, reference,isForgroundSelection));
        } else {
          setPreviewPanel(new TextPreviewLabel(reference,target, isForgroundSelection));
        }
        updateUI();
      }
      
      public Color showDialog(Component component, String title) {
        ColorChooserDialog dialog = new ColorChooserDialog(component, title, this);
        dialog.show();
        Color col = dialog.getColor();
        dialog.dispose();
        return col;
      }
      
    }
      

  2.   

    MixedExample.javaAttributiveCellTableModel.java
    DefaultCellAttribute.java
    CellAttribute.java
    ColoredCell.java
    CellFont.java
    CellSpan.java
    MultiSpanCellTable.java
    MultiSpanCellTableUI.java
    AttributiveCellRenderer.java
    TextColorChooser.java
      

  3.   

    superjames(均) :
    老兄,文章开头已经注明了啊!
    何况那个网站现在已经上不了了.........