Simple Graphic Table

This source code to create a graphic table by using java.awt.Graphics and java.swing.JPanel....

import java.awt.*;
import javax.swing.*;

public class Table extends JPanel{
    Color bColor=new Color(238,238,238);
    Color hColor=Color.WHITE;
    Color fColor=Color.BLACK;
   
    Font hFont=new Font( "Calibri" ,Font.BOLD,17);  
    Font bFont=new Font( "Arial" ,Font.PLAIN,15);

    String []name;
    String [][]list;


    int row=0;
    int col=0;   
   
    int rowSize=30;
    int []colSize;
   
    public Table(String []n,String [][]l){
        setLayout(null);
        name=n;
        list=l;
        row=list.length;
        col=name.length;
       
        colSize=new int[col];
        for(int i=0;i<col;i++){
            colSize[i]=100;
        }
    }
   
    public void paint(Graphics g) throws OutOfMemoryError{
        super.paint(g);
        int xSize=0;
        for(int i=0;i<col;i++){            
            xSize+=colSize[i];
        }
                  
        g.setColor(Color.BLACK);
        g.fillRect(0,0,xSize,5);
        g.setColor(Color.BLACK);
        g.fillRect(0,0,xSize,5+(rowSize*(row+1)));
        g.setColor(hColor);
        g.fillRect(5,5,xSize,rowSize);
       
        g.setColor(bColor);
        g.fillRect(5,rowSize,xSize,rowSize*row);
        g.setColor(Color.BLACK);
        g.fillRect(0,rowSize-2,xSize,2);
        g.fillRect(xSize,0,5,5+rowSize*(row+1));
      
        xSize=0;
        for(int i=0;i<col;i++){  
            try{
                g.setFont(hFont);
                g.drawString(name[i],10+xSize,rowSize/5*4);
            }
           
            catch(ArrayIndexOutOfBoundsException aioobe){
            }
            catch(NullPointerException npe){
            }
            finally{   
                xSize+=colSize[i];
            }
        }
                    
        for(int i=0;i<row;i++){
            g.setColor(Color.BLACK);
            g.drawLine(0,rowSize*(i+1),xSize,rowSize*(i+1));
           
            xSize=0;
            for(int j=0;j<col;j++){
                try{
                    g.setColor(Color.BLACK);
                    g.drawLine(0+xSize,0,0+xSize,rowSize*(row+1)); 
               
                    g.setColor(fColor);
                    g.setFont(bFont);
                    g.drawString(list[i][j],10+xSize,rowSize/5*4+rowSize*(i+1));
                   
                }
                catch(ArrayIndexOutOfBoundsException aioobe){
                }
                catch(NullPointerException npe){
                }
                finally{
                    xSize+=colSize[j];
                }
            }
        }
        g.setColor(Color.BLACK);
        g.fillRect(0,rowSize+rowSize*row,xSize,5);
        this.setPreferredSize(new Dimension(xSize,rowSize*row));
       
    }
   
    public void setColumnSize(int index,int xSize){
        colSize[index]=xSize;
        repaint();
    }
    public void setRowSize(int ySize){
        rowSize=ySize;
        repaint();
    }

    public void setBackground(Color c){
        bColor=c;
    }
    public void setForeground(Color c){
        fColor=c;
    }

    public void setHeader(Color c){
        hColor=c;
    }
    public void setHeader(Font f){
        hFont=f;
    }

    public void setFont(Font f){
        bFont=f;
    }
   
}


Now we try use this class in JFrame class....

import javax.swing.*;
import java.awt.*;


public class SimpleGraphicTable extends JFrame{

    public SimpleGraphicTable(){
        String []str1={ "No." , "col 1" , "col 2" , "col 3" , "col 4" ,  "col 5" , "col 6" };
        String [][]str2={{ "1" , "aaaa" , "aaa" , "aaaa" , "aaa" , "aaaa" , "aaa" }
                               ,{ "2""aaaa" , "aaa" , "aaaa" , "aaa" , "aaaa" , "aaa" }
                               ,{ "3""aaaa" , "aaa" , "aaaa" , "aaa" , "aaaa" , "aaa" }
                               ,{ "4""aaaa" , "aaa" , "aaaa" , "aaa" , "aaaa" , "aaa" } };                      
                            
        Container canvas=getContentPane();
        canvas.setLayout(new BorderLayout());
       
        Table t=new Table(str1,str2);
        t.setColumnSize(0,45);
        t.setRowSize(40);
       
        t.setBackground(Color.WHITE);
        t.setForeground(Color.BLUE);
        t.setHeader(new Color(251,175,23));
       
        t.setHeader(new Font( "Arial" , Font.BOLD,22));
        t.setFont(new Font( "Arial" , Font.PLAIN,22));
       
        canvas.add(t);
       
        this.setBounds(100,100,660,245);
        this.show();
       
    }
    public static void main(String [] args){
        SimpleGraphicTable gt=new SimpleGraphicTable();
        gt.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Sample Running : 


P/S : Hope this can help u...and please give some comment...Thank You...

                                                                                                                                                                                Created By : Z-man, 2011

No comments:

Post a Comment