devdaily home | java | perl | unix | directory | blog


What this is

This file is part of the Tame Swing collection that we have compiled. All of the Java source code in these examples was created by Nobuo Tamemasa. As these examples have become hard to find on the internet, we have put them in one place here, supported by minimal advertising. In time we will include images of each running sample Java/Swing application.

The intent of having these files here is to help you learn Java and Swing by example, and Tame certainly created some terrific examples of the power of the Swing framework.

Other links

Keywords/tags

java, swing, JTable, table, cell, renderer, editor, custom, JLabel, JCheckBox, JComboBox, JTextField, text, textfield, label, ImageIcon, table, icon, image, animated, model, TableModel, example, sample, source

The source code

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

import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import tame.table.EachRowEditor;
import tame.table.EachRowRenderer;

/*
I made some changes to the constructor of this in order
to get it to compile.  AT
@author Nobuo Tamemasa, Andrew Thompson
@version 1.0 11/09/98, 1.1 2004/03/05
*/
public class MultiComponentTable2 extends JFrame {

  public MultiComponentTable2(){
    super("MultiComponent Table");

    DefaultTableModel dm = new DefaultTableModel() {
      public boolean isCellEditable(int row, int column) {
        if (column == 0) {
          return true;
        }
        return false;
      }
    };
    dm.setDataVector(
      new Object[][]{
        {"true"             ,"String"  ,"JLabel"   ,"JComboBox"},
        {"false"            ,"String"  ,"JLabel"   ,"JComboBox"},
        {new Boolean(true)  ,"Boolean" ,"JCheckBox","JCheckBox"},
        {new Boolean(false) ,"Boolean" ,"JCheckBox","JCheckBox"},
        {"true"             ,"String"  ,"JLabel"   ,"JTextField"},
        {"false"            ,"String"  ,"JLabel"   ,"JTextField"}},
      new Object[]{"Component","Data","Renderer","Editor"});

    CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
    EachRowRenderer  rowRenderer         = new EachRowRenderer();
    rowRenderer.add(2, checkBoxRenderer);
    rowRenderer.add(3, checkBoxRenderer);
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("true");
    comboBox.addItem("false");
    JCheckBox checkBox = new JCheckBox();
    checkBox.setHorizontalAlignment(JLabel.CENTER);
    DefaultCellEditor comboBoxEditor = new DefaultCellEditor(comboBox);
    DefaultCellEditor checkBoxEditor = new DefaultCellEditor(checkBox);

    // moved to before rowEditor instantiate
    JTable table = new JTable(dm);

    EachRowEditor rowEditor = new EachRowEditor( table );
	// I think Nobuo was thinking of the rowRenderer above
	//  I commented this out and it compiles, at least
    /*
    rowEditor.add(0, comboBoxEditor);
    rowEditor.add(1, comboBoxEditor);
    rowEditor.add(2, checkBoxEditor);
    rowEditor.add(3, checkBoxEditor);
    */

    table.getColumn("Component").setCellRenderer(rowRenderer);
    table.getColumn("Component").setCellEditor(rowEditor);

    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add( scroll );
    setSize( 400, 160 );
    setVisible(true);
  }

  public static void main(String[] args) {
    MultiComponentTable2 frame = new MultiComponentTable2();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
  }
}

/**
@author Nobuo Tamemasa
@version 1.0 11/09/98
*/
class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {

  CheckBoxRenderer() {
    setHorizontalAlignment(JLabel.CENTER);
  }

  public Component getTableCellRendererComponent(JTable table, Object value,
                         boolean isSelected, boolean hasFocus,
                          int row, int column) {
    if (isSelected) {
      setForeground(table.getSelectionForeground());
      //super.setBackground(table.getSelectionBackground());
      setBackground(table.getSelectionBackground());
    } else {
      setForeground(table.getForeground());
      setBackground(table.getBackground());
    }
    setSelected((value != null && ((Boolean)value).booleanValue()));
    return this;
  }
}

Copyright © 1998-2009 DevDaily.com
All Rights Reserved.