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, font, text, string, custom, renderer, editor, color, model, TableModel, example, sample, source

The source code

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

import java.awt.FlowLayout;
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.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.table.AttributiveCellRenderer;
import tame.table.AttributiveCellTableModel;
import tame.table.CellFont;

/**
<pre>
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.table.DefaultTableModel.getColumnCount(DefaultTableModel.java:575)
        at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:242)
        at javax.swing.table.DefaultTableModel.setNumRows(DefaultTableModel.java:298)
        at tame.table.AttributiveCellTableModel.<init>(AttributiveCellTableModel.java:32)
</pre>
@author Nobuo Tamemasa
@version 1.0 11/22/98
*/
public class MultiFontCellTableExample extends JFrame {

  public MultiFontCellTableExample() {
    super( "Multi-Font Cell Example" );

    AttributiveCellTableModel ml = new AttributiveCellTableModel(8,3);
    CellFont cellAtt =(CellFont)ml.getCellAttribute();
    JTable table = new JTable( ml );
    table.setRowHeight(26);
    table.setCellSelectionEnabled(true);
    table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    table.setDefaultRenderer(Object.class ,new AttributiveCellRenderer());
    JScrollPane scroll = new JScrollPane( table );

    FontPanel fontPanel = new FontPanel(table, cellAtt);

    Box box = new Box(BoxLayout.X_AXIS);
    box.add(scroll);
    box.add(new JSeparator(SwingConstants.HORIZONTAL));
    box.add(fontPanel);
    getContentPane().add( box );
    setSize( 400, 200 );
    setVisible(true);
  }

  class FontPanel extends JPanel {
    String[] str_size  = {"10","12","14","16","20","24"};
    String[] str_style = {"PLAIN","BOLD","ITALIC"};
    JComboBox name,style,size;

    FontPanel(final JTable table, final CellFont cellAtt) {
      setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
      Box box = new Box(BoxLayout.X_AXIS);
      JPanel p2 = new JPanel(new GridLayout(3,1));
      JPanel p3 = new JPanel(new GridLayout(3,1));
      JPanel p4 = new JPanel(new FlowLayout());
      p2.add(new JLabel("Name:"));
      p2.add(new JLabel("Style:"));
      p2.add(new JLabel("Size:"));
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      name  = new JComboBox(toolkit.getFontList());
      style = new JComboBox(str_style);
      size  = new JComboBox(str_size);
      size.setEditable(true);
      JButton b_apply   = new JButton("Apply");
      b_apply.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          int[] columns = table.getSelectedColumns();
          int[] rows    = table.getSelectedRows();
          if ((rows == null) || (columns == null)) return;
          if ((rows.length<1)||(columns.length<1)) return;
          Font font = new Font((String)name.getSelectedItem(),
                              style.getSelectedIndex(),
              Integer.parseInt((String)size.getSelectedItem()));
          cellAtt.setFont(font, rows, columns);
          table.clearSelection();
          table.revalidate();
          table.repaint();
        }
      });
      p3.add(name);
      p3.add(style);
      p3.add(size);
      p4.add(b_apply);
      box.add(p2);
      box.add(p3);
      add(box);
      add(p4);
    }
  }

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

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