JSortTable : Grid Table « Swing Components « 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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Swing Components » Grid TableScreenshots 
JSortTable
   
package org.tn5250j.gui;
/*


  JSortTable.java

  Created by Claude Duguay
  Copyright (c) 2002
   This was taken from a Java Pro magazine article
   http://www.fawcette.com/javapro/codepage.asp?loccode=jp0208

   I have NOT asked for permission to use this.


*/

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class JSortTable extends JTable implements MouseListener {
   protected int sortedColumnIndex = -1;
   protected boolean sortedColumnAscending = true;

   public JSortTable() {
      this(new DefaultSortTableModel());
   }

   public JSortTable(int rows, int cols) {
      this(new DefaultSortTableModel(rows, cols));
   }

   public JSortTable(Object[][] data, Object[] names) {
      this(new DefaultSortTableModel(data, names));
   }

   public JSortTable(Vector data, Vector names) {
      this(new DefaultSortTableModel(data, names));
   }

   public JSortTable(SortTableModel model) {
      super(model);
      initSortHeader();
   }

   public JSortTable(SortTableModel model, TableColumnModel colModel) {
      super(model, colModel);
      initSortHeader();
   }

   public JSortTable(SortTableModel model, TableColumnModel colModel,
                           ListSelectionModel selModel) {
      super(model, colModel, selModel);
      initSortHeader();
   }

   protected void initSortHeader() {
      JTableHeader header = getTableHeader();
      header.setDefaultRenderer(new SortHeaderRenderer());
      header.addMouseListener(this);
   }

   public int getSortedColumnIndex() {
      return sortedColumnIndex;
   }

   public boolean isSortedColumnAscending() {
      return sortedColumnAscending;
   }

   public void mouseReleased(MouseEvent event) {
      TableColumnModel colModel = getColumnModel();
      int index = colModel.getColumnIndexAtX(event.getX());
      int modelIndex = colModel.getColumn(index).getModelIndex();

      SortTableModel model = (SortTableModel)getModel();
      if (model.isSortable(modelIndex)) {
         // toggle ascension, if already sorted
         if (sortedColumnIndex == index) {
            sortedColumnAscending = !sortedColumnAscending;
         }
         sortedColumnIndex = index;

         model.sortColumn(modelIndex, sortedColumnAscending);
      }
   }

   public void mousePressed(MouseEvent event) {}
   public void mouseClicked(MouseEvent event) {}
   public void mouseEntered(MouseEvent event) {}
   public void mouseExited(MouseEvent event) {}
}

/*


  DefaultSortTableModel.java

  Created by Claude Duguay
  Copyright (c) 2002
   This was taken from a Java Pro magazine article
   http://www.fawcette.com/javapro/codepage.asp?loccode=jp0208

   I have NOT asked for permission to use this.


*/


 class DefaultSortTableModel  extends DefaultTableModel
                                    implements SortTableModel {

   public DefaultSortTableModel() {}

   public DefaultSortTableModel(int rows, int cols) {
      super(rows, cols);
   }

   public DefaultSortTableModel(Object[][] data, Object[] names) {
      super(data, names);
   }

   public DefaultSortTableModel(Object[] names, int rows) {
      super(names, rows);
   }

   public DefaultSortTableModel(Vector names, int rows) {
      super(names, rows);
   }

   public DefaultSortTableModel(Vector data, Vector names) {
      super(data, names);
   }

   public boolean isSortable(int col) {
      return true;
   }

   public void sortColumn(int col, boolean ascending) {
      Collections.sort(getDataVector(),
         new ColumnComparator(col, ascending));
   }
}

 /*
 

   SortTableModel.java

   Created by Claude Duguay
   Copyright (c) 2002
    This was taken from a Java Pro magazine article
    http://www.fawcette.com/javapro/codepage.asp?loccode=jp0208

    I have NOT asked for permission to use this.
 
 */

  interface SortTableModel extends TableModel {
    public boolean isSortable(int col);
    public void sortColumn(int col, boolean ascending);
 }

  /*
  

    ColumnComparator.java

    Created by Claude Duguay
    Copyright (c) 2002
     This was taken from a Java Pro magazine article
     http://www.fawcette.com/javapro/codepage.asp?loccode=jp0208

     I have NOT asked for permission to use this.

  
  */

   class ColumnComparator implements Comparator {
     protected int index;
     protected boolean ascending;

     public ColumnComparator(int index, boolean ascending) {
        this.index = index;
        this.ascending = ascending;
     }

     public int compare(Object one, Object two) {
        if (one instanceof Vector && two instanceof Vector) {
           Vector vOne = (Vector)one;
           Vector vTwo = (Vector)two;
           Object oOne = vOne.elementAt(index);
           Object oTwo = vTwo.elementAt(index);
           if (oOne instanceof Comparable && oTwo instanceof Comparable) {
              Comparable cOne = (Comparable)oOne;
              Comparable cTwo = (Comparable)oTwo;
              if (ascending) {
                 return cOne.compareTo(cTwo);
              }
              else {
                 return cTwo.compareTo(cOne);
              }
           }
        }

        return 1;
     }
  }






   /*
   

     SortHeaderRenderer.java

     Created by Claude Duguay
     Copyright (c) 2002
      This was taken from a Java Pro magazine article
      http://www.fawcette.com/javapro/codepage.asp?loccode=jp0208

      I have NOT asked for permission to use this.

   
   */

    class SortHeaderRenderer extends DefaultTableCellRenderer {

      public static Icon NONSORTED =  new SortArrowIcon(SortArrowIcon.NONE);
      public static Icon ASCENDING =  new SortArrowIcon(SortArrowIcon.ASCENDING);
      public static Icon DECENDING =  new SortArrowIcon(SortArrowIcon.DECENDING);

      public SortHeaderRenderer() {
         setHorizontalTextPosition(LEFT);
         setHorizontalAlignment(CENTER);
      }

      public Component getTableCellRendererComponentJTable table,
                                 Object value,
                                 boolean isSelected,
                                 boolean hasFocus, int row, int col) {

         int index = -1;
         boolean ascending = true;
         if (table instanceof JSortTable) {
            JSortTable sortTable = (JSortTable)table;
            index = sortTable.getSortedColumnIndex();
            ascending = sortTable.isSortedColumnAscending();
         }
         if (table != null) {
            JTableHeader header = table.getTableHeader();
            if (header != null) {
               setForeground(header.getForeground());
               setBackground(header.getBackground());
               setFont(header.getFont());
            }
         }

         Icon icon = ascending ? ASCENDING : DECENDING;
         setIcon(col == index ? icon : NONSORTED);
         setText((value == null"" : value.toString());
         setBorder(UIManager.getBorder("TableHeader.cellBorder"));
         return this;
      }
   }


    /*
    

      SortArrowIcon.java

      Created by Claude Duguay
      Copyright (c) 2002
       This was taken from a Java Pro magazine article
       http://www.fawcette.com/javapro/codepage.asp?loccode=jp0208

       I have NOT asked for permission to use this.

    
    */

    class SortArrowIcon implements Icon {

       public static final int NONE = 0;
       public static final int DECENDING = 1;
       public static final int ASCENDING = 2;

       protected int direction;
       protected int width = 8;
       protected int height = 8;

       public SortArrowIcon(int direction) {
          this.direction = direction;
       }

       public int getIconWidth() {
          return width;
       }

       public int getIconHeight() {
          return height;
       }

       public void paintIcon(Component c, Graphics g, int x, int y) {
          Color bg = c.getBackground();
          Color light = bg.brighter();
          Color shade = bg.darker();

          int w = width;
          int h = height;
          int m = w / 2;
          if (direction == ASCENDING) {
             g.setColor(shade);
             g.drawLine(x, y, x + w, y);
             g.drawLine(x, y, x + m, y + h);
             g.setColor(light);
             g.drawLine(x + w, y, x + m, y + h);
          }
          if (direction == DECENDING) {
             g.setColor(shade);
             g.drawLine(x + m, y, x, y + h);
             g.setColor(light);
             g.drawLine(x, y + h, x + w, y + h);
             g.drawLine(x + m, y, x + w, y + h);
          }
       }
    }

   
    
    
  
Related examples in the same category
1.HyperLink in TableHyperLink in Table
2.Column popup menuColumn popup menu
3.Tree TableTree Table
4.Swing Table in ComboBoxSwing Table in ComboBox
5.Tabbable Currency TableTabbable Currency Table
6.Icon Currency TableIcon Currency Table
7.MultiLine Header TableMultiLine Header Table
8.ToolTip TableToolTip Table
9.Striped Currency TableStriped Currency Table
10.CurrencyTableCurrencyTable
11.Calculated Column TableCalculated Column Table
12.Table Utilities
13.Fraction Currency TableFraction Currency Table
14.Highlight Currency TableHighlight Currency Table
15.Highlight Currency Table 2Highlight Currency Table 2
16.MultiLine TableMultiLine Table
17.Updatable Highlight Currency TableUpdatable Highlight Currency Table
18.Editable Highlight Currency TableEditable Highlight Currency Table
19.ComboBox TableComboBox Table
20.Groupable(Group) Header ExampleGroupable(Group) Header Example
21.MultiWidth Header ExampleMultiWidth Header Example
22.MultiLine Header ExampleMultiLine Header Example
23.Table Row Header ExampleTable Row Header Example
24.Fixed Table Column ExampleFixed Table Column Example
25.Button Table ExampleButton Table Example
26.Radio Button Table ExampleRadio Button Table Example
27.RadioButton Table Example 2RadioButton Table Example 2
28.MultiLine Cell ExampleMultiLine Cell Example
29.Each Row with different Editor ExampleEach Row with different Editor Example
30.Multiple Component Table: Checkbox and ComboboxMultiple Component Table: Checkbox and Combobox
31.multiple Component Table 2: checkboxmultiple Component Table 2: checkbox
32.Union Data Table ExampleUnion Data Table Example
33.Total(Calculate) Row ExampleTotal(Calculate) Row Example
34.Colored Cell Table Example
35.multiple Font Cell Table Example
36.Multi Span Cell Table Example
37.Mixed Table Example
38.Pushable Table Header ExamplePushable Table Header Example
39.Sortable Table ExampleSortable Table Example
40.ToolTip Header Table ExampleToolTip Header Table Example
41.Indicator Table ExampleIndicator Table Example
42.Fixed Table Row ExampleFixed Table Row Example
43.multiple Row Header Example
44.Column Border Table ExampleColumn Border Table Example
45.Cell Border Table ExampleCell Border Table Example
46.Hide Column Table ExampleHide Column Table Example
47.Animated Icon Table ExampleAnimated Icon Table Example
48.Animated Icon Header Example
49.Editable Header Table ExampleEditable Header Table Example
50.Editable Header Table Example 2Editable Header Table Example 2
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.