Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

Custom Search
Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 8/9 



Previous Page Previous Page (7/9) - Next Page (9/9) Next Page

18.8  Custom models, editors, and renderers

In constructing our StocksTable application we talked mostly about displaying and retrieving data in JTable. In this section we will construct a basic expense report application, and in doing so we will concentrate on table cell editing. We will also see how to implement dynamic addition and removal of table rows.

The editing of data generally follows this scheme:

Create an instance of the TableCellEditor interface. We can use the DefaultCellEditor class or implement our own. The DefaultCellEditor class takes a GUI component as a parameter to its constructor: JTextField, JCheckBox or JComboBox. This component will be used for editing.

If we are developing a custom editor, we need to implement the getTableCellEditorComponent() method which will be called each time a cell is about to be edited.

In our table model we need to implement the setValueAt(Object value, int nRow, int nCol) method which will be called to change a value in the table when an edit ends. This is where we can perform any necessary data processing and validation.

The data model for this example is designed as follows (where each row represents a column in our JTable):

Name                      Type                       Description

Date                        String                Date of expense

Amount                 Double                Amount of expense

Category                Integer             Category from pre-defined list

Approved              Boolean             Sign of approval for this expense.

Description           String                Brief description

Figure 18.7 An expense report app illustrating custom cell editing, rendering, and row addition/removal.

<<file figure18-7.gif>>

Note: Since the only math that is done with our "Amount" values is addition, using Doubles is fine. However, in more professional implementations we may need to use rounding techniques or a custom renderer to remove unneccessary fractional amounts.

The Code: ExpenseReport.java

see \Chapter18\7

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

import java.text.SimpleDateFormat;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.event.*;

import javax.swing.table.*;

public class ExpenseReport extends JFrame

{

  protected JTable m_table;

  protected ExpenseReportData m_data;

  protected JLabel m_title;

  public ExpenseReport() {

    super("Expense Report");

    setSize(570, 200);

    m_data = new ExpenseReportData(this);

    m_table = new JTable();

    m_table.setAutoCreateColumnsFromModel(false);

    m_table.setModel(m_data);

    m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    for (int k = 0; k < ExpenseReportData.m_columns.length; k++) {

      TableCellRenderer renderer;

      if (k==ExpenseReportData.COL_APPROVED)

        renderer = new CheckCellRenderer();

      else {

        DefaultTableCellRenderer textRenderer =

          new DefaultTableCellRenderer();

        textRenderer.setHorizontalAlignment(

          ExpenseReportData.m_columns[k].m_alignment);

        renderer = textRenderer;

      }

      TableCellEditor editor;

      if (k==ExpenseReportData.COL_CATEGORY)

        editor = new DefaultCellEditor(new JComboBox(

          ExpenseReportData.CATEGORIES));

      else if (k==ExpenseReportData.COL_APPROVED)

        editor = new DefaultCellEditor(new JCheckBox());

      else

        editor = new DefaultCellEditor(new JTextField());

      TableColumn column = new TableColumn(k,

        ExpenseReportData.m_columns[k].m_width,

          renderer, editor);

        m_table.addColumn(column);  

    }

    JTableHeader header = m_table.getTableHeader();

    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();

    ps.setSize(550, 150);

    ps.getViewport().add(m_table);

    getContentPane().add(ps, BorderLayout.CENTER);

    JPanel p = new JPanel();

    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    ImageIcon penny = new ImageIcon("penny.gif");

    m_title = new JLabel("Total: $",

      penny, JButton.LEFT);

    m_title.setForeground(Color.black);

    m_title.setAlignmentY(0.5f);

    p.add(m_title);

    p.add(Box.createHorizontalGlue());

    JButton bt = new JButton("Insert before");

    bt.setMnemonic('b');

    bt.setAlignmentY(0.5f);

    ActionListener lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        int row = m_table.getSelectedRow();

        m_data.insert(row);

        m_table.tableChanged(new TableModelEvent(

          m_data, row, row, TableModelEvent.ALL_COLUMNS,

          TableModelEvent.INSERT));

        m_table.repaint();

      }

    };

    bt.addActionListener(lst);

    p.add(bt);

    bt = new JButton("Insert after");

    bt.setMnemonic('a');

    bt.setAlignmentY(0.5f);

    lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        int row = m_table.getSelectedRow();

        m_data.insert(row+1);

        m_table.tableChanged(new TableModelEvent(

          m_data, row+1, row+1, TableModelEvent.ALL_COLUMNS,

          TableModelEvent.INSERT));

        m_table.repaint();

      }

    };

    bt.addActionListener(lst);

    p.add(bt);

    bt = new JButton("Delete row");

    bt.setMnemonic('d');

    bt.setAlignmentY(0.5f);

    lst = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        int row = m_table.getSelectedRow();

        if (m_data.delete(row)) {

          m_table.tableChanged(new TableModelEvent(

            m_data, row, row, TableModelEvent.ALL_COLUMNS,

            TableModelEvent.INSERT));

          m_table.repaint();

          calcTotal();

        }

      }

    };

    bt.addActionListener(lst);

    p.add(bt);

    getContentPane().add(p, BorderLayout.SOUTH);

    calcTotal();

    WindowListener wndCloser = new WindowAdapter() {

      public void windowClosing(WindowEvent e) {

        System.exit(0);

      }

    };

    addWindowListener(wndCloser);

    setVisible(true);

  }

  public void calcTotal() {

    double total = 0;

    for (int k=0; k<m_data.getRowCount(); k++) {

      Double amount = (Double)m_data.getValueAt(k,

        ExpenseReportData.COL_AMOUNT);

      total += amount.doubleValue();

    }

    m_title.setText("Total: $"+total);

  }

  public static void main(String argv[]) {

    new ExpenseReport();

  }

}

class CheckCellRenderer extends JCheckBox implements TableCellRenderer

{

  protected static Border m_noFocusBorder;

  public CheckCellRenderer() {

    super();

    m_noFocusBorder = new EmptyBorder(1, 2, 1, 2);

    setOpaque(true);

    setBorder(m_noFocusBorder);

  }

  public Component getTableCellRendererComponent(JTable table,

   Object value, boolean isSelected, boolean hasFocus,

   int row, int column)

  {

    if (value instanceof Boolean) {

      Boolean b = (Boolean)value;

      setSelected(b.booleanValue());

    }

    setBackground(isSelected && !hasFocus ?

      table.getSelectionBackground() : table.getBackground());

    setForeground(isSelected && !hasFocus ?

      table.getSelectionForeground() : table.getForeground());

    setFont(table.getFont());

    setBorder(hasFocus ? UIManager.getBorder(

      "Table.focusCellHighlightBorder") : m_noFocusBorder);

    return this;

  }

}

class ExpenseData

{

  public Date    m_date;

  public Double  m_amount;

  public Integer m_category;

  public Boolean m_approved;

  public String  m_description;

  public ExpenseData() {

    m_date = new Date();

    m_amount = new Double(0);

    m_category = new Integer(1);

    m_approved = new Boolean(false);

    m_description = "";

  }

  public ExpenseData(Date date, double amount, int category,

   boolean approved, String description)

  {

    m_date = date;

    m_amount = new Double(amount);

    m_category = new Integer(category);

    m_approved = new Boolean(approved);

    m_description = description;

  }

}

class ColumnData

{

  public String  m_title;

  int m_width;

  int m_alignment;

  public ColumnData(String title, int width, int alignment) {

    m_title = title;

    m_width = width;

    m_alignment = alignment;

  }

}

class ExpenseReportData extends AbstractTableModel

{

  public static final ColumnData m_columns[] = {

    new ColumnData( "Date", 80, JLabel.LEFT ),

    new ColumnData( "Amount", 80, JLabel.RIGHT ),

    new ColumnData( "Category", 130, JLabel.LEFT ),

    new ColumnData( "Approved", 80, JLabel.LEFT ),

    new ColumnData( "Description", 180, JLabel.LEFT )

  };

  public static final int COL_DATE = 0;

  public static final int COL_AMOUNT = 1;

  public static final int COL_CATEGORY = 2;

  public static final int COL_APPROVED = 3;

  public static final int COL_DESCR = 4;

  public static final String[] CATEGORIES = {

    "Fares", "Logging", "Business meals", "Others"

  };

  protected ExpenseReport m_parent;

  protected SimpleDateFormat m_frm;

  protected Vector m_vector;

  public ExpenseReportData(ExpenseReport parent) {

    m_parent = parent;

    m_frm = new SimpleDateFormat("MM/dd/yy");

    m_vector = new Vector();

    setDefaultData();

  }

  public void setDefaultData() {

    m_vector.removeAllElements();

    try {

      m_vector.addElement(new ExpenseData(

      m_frm.parse("04/06/99"), 200, 0, true,

        "Airline tickets"));

      m_vector.addElement(new ExpenseData(

        m_frm.parse("04/06/99"), 50,  2, false,

        "Lunch with client"));

      m_vector.addElement(new ExpenseData(

        m_frm.parse("04/06/99"), 120, 1, true,

        "Hotel"));

    }

    catch (java.text.ParseException ex) {}

  }

  public int getRowCount() {

    return m_vector==null ? 0 : m_vector.size();

  }

  public int getColumnCount() {

    return m_columns.length;

  }

  public String getColumnName(int column) {

    return m_columns[column].m_title;

  }

  public boolean isCellEditable(int nRow, int nCol) {

    return true;

  }

  public Object getValueAt(int nRow, int nCol) {

    if (nRow < 0 || nRow>=getRowCount())

      return "";

    ExpenseData row = (ExpenseData)m_vector.elementAt(nRow);

    switch (nCol) {

      case COL_DATE: return m_frm.format(row.m_date);

      case COL_AMOUNT: return row.m_amount;

      case COL_CATEGORY: return CATEGORIES[row.m_category.intValue()];

      case COL_APPROVED: return row.m_approved;

      case COL_DESCR: return row.m_description;

    }

    return "";

  }

  public void setValueAt(Object value, int nRow, int nCol) {

    if (nRow < 0 || nRow>=getRowCount())

      return;

    ExpenseData row = (ExpenseData)m_vector.elementAt(nRow);

    String svalue = value.toString();

    switch (nCol) {

      case COL_DATE:

        Date  date = null;

        try {

          date = m_frm.parse(svalue);

        }

        catch (java.text.ParseException ex) {

          date = null;

        }

        if (date == null) {

          JOptionPane.showMessageDialog(null,

            svalue+" is not a valid date",

            "Warning", JOptionPane.WARNING_MESSAGE);

          return;

        }

        row.m_date = date;

        break;

      case COL_AMOUNT:

        try {

          row.m_amount = new Double(svalue);

        }

        catch (NumberFormatException e) { break; }

        m_parent.calcTotal();

        break;

      case COL_CATEGORY:

        for (int k=0; k<CATEGORIES.length; k++)

          if (svalue.equals(CATEGORIES[k])) {

            row.m_category = new Integer(k);

            break;

          }

        break;

      case COL_APPROVED:

        row.m_approved = (Boolean)value;

        break;

      case COL_DESCR:

        row.m_description = svalue;

        break;

    }

  }

  public void insert(int row) {

    if (row < 0)

      row = 0;

    if (row > m_vector.size())

      row = m_vector.size();

    m_vector.insertElementAt(new ExpenseData(), row);

  }

  public boolean delete(int row) {

    if (row < 0 || row >= m_vector.size())

      return false;

    m_vector.remove(row);

      return true;

  }

}

Understanding the Code

Class ExpenseReport

Class ExpenseReport extends JFrame and defines three instance variables:

JTable m_table: table to edit data.

ExpenseReportData m_data: data model for this table.

JLabel m_total: label to dynamically display total amount of expenses.

The ExpenseReport constructor first instantiates our table model, m_data, and then instantiates our table, m_table. The selection mode is set to single selection and we iterate through the number of columns creating cell renderers and editors based on each specific column. The "Approved" column uses an instance of our custom CheckCellRenderer class as renderer. All other columns use a DefaultTableCellRenderer. All columns also use a DefaultCellEditor. However, the component used for editing varies: the "Category" column uses a JComboBox, the "Approved" column uses a JCheckBox, and all other columns use a JTextField. These components are passed to the DefaultTableCellRenderer constructor.

Several components are added to the bottom of our frame: JLabel m_total, used to display the total amount of expenses, and three JButtons used to manipulate tables rows. (Note that the horizontal glue component added between the label and the button pushes buttons to the right side of the panel, so they remain glued to the right when our frame is resized.)

These three buttons, titled "Insert before," "Insert after," and "Delete row," behave as their titles imply. The first two use the insert() method from the ExpenseReportData model to insert a new row before or after the currently selected row. The last one deletes the currently selected row by calling the delete() method. In all cases the modified table is updated and repainted.

Method calcTotal() calculates the total amount of expenses in column COL_AMOUNT using our table's data model, m_data.

Class CheckCellRenderer

Since we use check boxes to edit our table's "Approved" column, to be consistent we also need to use check boxes for that column's cell renderer (recall that cell renderers just act as "rubber stamps" and are not at all interactive). The only GUI component which can be used in the existing DefaultTableCellRenderer is JLabel, so we have to provide our own implementation of the TableCellRenderer interface. This class, CheckCellRenderer, uses JCheckBox as a super-class. Its constructor sets the border to indicate whether the component has the focus and sets its opaque property to true to indicate that the component's background will be filled with the background color.

The only method which must be implemented in the TableCellRenderer interface is getTableCellRendererComponent(). This method will be called each time the cell is about to be rendered to deliver new data to the renderer. It takes six parameters:

JTable table: reference to table instance.

Object value: data object to be sent to the renderer.

boolean isSelected: true if the cell is currently selected.

boolean hasFocus: true if the cell currently has the focus.

int row: cell's row.

int column: cell's column.

Our implementation sets whether the JCheckBox is checked depending on the value passed as Boolean. Then it sets the background, foreground, font, and border to ensure that each cell in the table has a similar appearance.

Class ExpenseData

Class ExpenseData represents a single row in the table. It holds five variables corresponding to our data structure described in the beginning of this section.

Class ColumnData

Class ColumnData holds each column's title, width, and header alignment.

Class ExpenseReportData

ExpenseReportData extends AbstractTableModel and should look somewhat familiar from previous examples in this chapter (e.g. StockTableData), so we will not discuss this class in complete detail. However, we need to take a closer look at the setValueAt() method, which is new for this example (all previous examples did not accept new data). This method is called each time an edit is made to a table cell. First we determine which ExpenseData instance (table's row) is affected, and if it is invalid we simply return. Otherwise, depending on the column of the changed cell, we define several cases in a switch structure to accept and store a new value, or to reject it:

For the "Date" column the input string is parsed using our SimpleDateFormat instance. If parsing is successful, a new date is saved as a Date object, otherwise an error message is displayed.

For the "Amount" column the input string is parsed as a Double and stored in the table if parsing is successful. Also a new total amount is recalculated and displayed in the "Total" JLabel.

For the "Category" column the input string is placed in the CATEGORIES array at the corresponding index and is stored in the table model.

For the "Approved" column the input object is cast to a Boolean and stored in the table model.

For the "Description" column the input string is directly saved in our table model.

Running the Code

Try editing different columns and note how the corresponding cell editors work. Experiment with adding and removing table rows and note how the total amount is updated each time the "Amount" column is updated. Figure 18.8 shows ExpenseReport with a combo box opened to change a cell's value.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy