This program shows how to build a table from a table model : Table Model « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Table Model 




This program shows how to build a table from a table model
This program shows how to build a table from a table model
 
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 * This program shows how to build a table from a table model.
 @version 1.02 2007-08-01
 @author Cay Horstmann
 */
public class InvestmentTable
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               JFrame frame = new InvestmentTableFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

/**
 * This frame contains the investment table.
 */
class InvestmentTableFrame extends JFrame
{
   public InvestmentTableFrame()
   {
      setTitle("InvestmentTable");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      TableModel model = new InvestmentTableModel(30510);
      JTable table = new JTable(model);
      add(new JScrollPane(table));
   }

   private static final int DEFAULT_WIDTH = 600;
   private static final int DEFAULT_HEIGHT = 300;
}

/**
 * This table model computes the cell entries each time they are requested. The table contents shows
 * the growth of an investment for a number of years under different interest rates.
 */
class InvestmentTableModel extends AbstractTableModel
{
   /**
    * Constructs an investment table model.
    @param y the number of years
    @param r1 the lowest interest rate to tabulate
    @param r2 the highest interest rate to tabulate
    */
   public InvestmentTableModel(int y, int r1, int r2)
   {
      years = y;
      minRate = r1;
      maxRate = r2;
   }

   public int getRowCount()
   {
      return years;
   }

   public int getColumnCount()
   {
      return maxRate - minRate + 1;
   }

   public Object getValueAt(int r, int c)
   {
      double rate = (c + minRate100.0;
      int nperiods = r;
      double futureBalance = INITIAL_BALANCE * Math.pow(+ rate, nperiods);
      return String.format("%.2f", futureBalance);
   }

   public String getColumnName(int c)
   {
      return (c + minRate"%";
   }

   private int years;
   private int minRate;
   private int maxRate;

   private static double INITIAL_BALANCE = 100000.0;
}

   
  














Related examples in the same category
1.Append a row to a table through DefaultTableModel at specified row
2.Remove the first row from a table with DefaultTableModel
3.Remove the last row from a table with DefaultTableModel
4.Move the first row to the end of the table
5.Move the last row to the beginning of the table
6.Move the first two rows to the end of the table
7.Move the last two rows to the start of the table
8.Get all the table data from DefaultTableModel
9.Copy (clone) the data from the second row
10.Overwrite the date from the first row with DefaultTableModel
11.Copy data from a table to a list
12.Insert a new column to a table
13.JTable class using default table models and a convenienceJTable class using default table models and a convenience
14.extends DefaultTableModel to create your own table model and build table from thatextends DefaultTableModel to create your own table model and build table from that
15.Table with a custom TableModelTable with a custom TableModel
16.extends AbstractTableModel to create custom modelextends AbstractTableModel to create custom model
17.Table model is based on call backTable model is based on call back
18.Hard code data in array for TableModelHard code data in array for TableModel
19.A JTable class using default table models and a convenience constructorA JTable class using default table models and a convenience constructor
20.Custom model, POJO and JTableCustom model, POJO and JTable
21.Custom table model, File data based ModelCustom table model, File data based Model
22.Paging or pagable JTable(Table) Model for large data setPaging or pagable JTable(Table) Model for large data set
23.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.
24.File data Table: file name, size, type File data Table: file name, size, type
25.Stocks data Table: illustrate the TableModel Stocks data Table: illustrate the TableModel
26.AbstractTableModel backed by HashtableAbstractTableModel backed by Hashtable
27.Fixed data vs dynamic data TableFixed data vs dynamic data Table
28.Use model to control the Editable ColumnsUse model to control the Editable Columns
29.Converts a visible column index to a column index in the model.
30.Converts a column index in the model to a visible column index
31.Returns the visible columns in the order that they appear in the model
32.Appending a Column to a JTable Component using DefaultTableModel
33.Add a column with values.
34.Disable autoCreateColumnsFromModel
35.Add a column without affecting existing columns
36.Remove the first visible column without removing the underlying data
37.Sharing a Table Model Between JTable Components
38.Creating simple JTable using AbstractTableModel
39.Map TableModel
40.TableSorter extends AbstractTableModel
41.TableSorter is a decorator for TableModels
42.Bean Property Table Model
43.A simple extension of JTable that supports the use of a SortableTableModel.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.