The Calendar program. : JTable Model « Swing « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing » JTable Model 
14.59.10.The Calendar program.Previous/Next
The Calendar program.
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

public class Calendar extends JFrame {
  String[] years = "2008""2009""2010" };

  JComboBox comboBox = new JComboBox(years);

  String[] months = "January""February""March""April""May""June""July""August",
      "September""October""November""December" };

  JList list = new JList(months);

  JScrollPane scrollPane = new JScrollPane(list);

  CalendarModel model = new CalendarModel();

  JTable table = new JTable(model);

  public Calendar() {
    super();
    
    getContentPane().setLayout(null);
    comboBox.setBounds(101010030);
    comboBox.setSelectedIndex(0);
    comboBox.addItemListener(new ComboHandler());
    scrollPane.setBounds(20010150100);
    list.setSelectedIndex(3);
    list.addListSelectionListener(new ListHandler());
    table.setBounds(10150550200);
    model.setMonth(comboBox.getSelectedIndex() 1998, list.getSelectedIndex());
    getContentPane().add(comboBox);
    getContentPane().add(scrollPane);
    table.setGridColor(Color.black);
    table.setShowGrid(true);
    getContentPane().add(table);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(500500);
    setVisible(true);
  }

  public static void main(String[] args) {
    Calendar app = new Calendar();
  }
  public class ComboHandler implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
      model.setMonth(comboBox.getSelectedIndex() 1998, list.getSelectedIndex());
      table.repaint();
    }
  }

  public class ListHandler implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent e) {
      model.setMonth(comboBox.getSelectedIndex() 1998, list.getSelectedIndex());
      table.repaint();
    }
  }
}
class CalendarModel extends AbstractTableModel {
  String[] days = "Sun""Mon""Tue""Wed""Thu""Fri""Sat" };

  int[] numDays = 312831303130313130313031 };

  String[][] calendar = new String[7][7];

  public CalendarModel() {
    for (int i = 0; i < days.length; ++i)
      calendar[0][i= days[i];
    for (int i = 1; i < 7; ++i)
      for (int j = 0; j < 7; ++j)
        calendar[i][j" ";
  }

  public int getRowCount() {
    return 7;
  }

  public int getColumnCount() {
    return 7;
  }

  public Object getValueAt(int row, int column) {
    return calendar[row][column];
  }

  public void setValueAt(Object value, int row, int column) {
    calendar[row][column(Stringvalue;
  }

  public void setMonth(int year, int month) {
    for (int i = 1; i < 7; ++i)
      for (int j = 0; j < 7; ++j)
        calendar[i][j" ";
    java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
    cal.set(year, month, 1);
    int offset = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK1;
    offset += 7;
    int num = daysInMonth(year, month);
    for (int i = 0; i < num; ++i) {
      calendar[offset / 7][offset % 7= Integer.toString(i + 1);
      ++offset;
    }
  }

  public boolean isLeapYear(int year) {
    if (year % == 0)
      return true;
    return false;
  }

  public int daysInMonth(int year, int month) {
    int days = numDays[month];
    if (month == && isLeapYear(year))
      ++days;
    return days;
  }
}
14.59.JTable Model
14.59.1.Create DefaultTableModel
14.59.2.Creating simple JTable using AbstractTableModel
14.59.3.Extending AbstractTableModelExtending AbstractTableModel
14.59.4.Creates tables that allow rows and columns to be added or deleted
14.59.5.Set table valueSet table value
14.59.6.Add logic to get value methodAdd logic to get value method
14.59.7.Subclass AbstractTableModel to reuse an existing data structure: Treat an array as an AbstractTableModelSubclass AbstractTableModel to reuse an existing data structure: Treat an array as an AbstractTableModel
14.59.8.Sparsely Populated Table ModelSparsely Populated Table Model
14.59.9.Listening to JTable Events with a TableColumnModelListenerListening to JTable Events with a TableColumnModelListener
14.59.10.The Calendar program.The Calendar program.
14.59.11.Sample Sorting Table ModelSample Sorting Table Model
14.59.12.Add columns to a table through DefaultTableModel
14.59.13.Insert a row to a table through DefaultTableModel
14.59.14.Insert a row to a table through DefaultTableModel at specified row
14.59.15.Append a row to a table through DefaultTableModel at specified row
14.59.16.Remove the first row from a table with DefaultTableModel
14.59.17.Remove the last row from a table with DefaultTableModel
14.59.18.Move the first row to the end of the table
14.59.19.Move the first two rows to the end of the table
14.59.20.Move the last two rows to the start of the table
14.59.21.Move the last row to the beginning of the table
14.59.22.Get all the table data from DefaultTableModel
14.59.23.Copy (clone) the data from the second row
14.59.24.Overwrite the date from the first row with DefaultTableModel
14.59.25.Add rows to a Table
14.59.26.Sharing a Table Model Between JTable Components
14.59.27.Disabling User Edits in a JTable with DefaultTableModel
14.59.28.Appending a Row to a JTable Component
14.59.29.Copy data from a table to a list
14.59.30.Map TableModel
14.59.31.Bean Property Table Model
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.