Saturday, May 12, 2012

How to customize jTable using Netbeans

2 comments
Learn how to customize jtable using Netbeans. In this post we learn how to set color grid line,Visibility of vertical and horizontal grid line, and how to set foreground color to selected cell. A simple single line code tell us how to customize jtable.
  • First create global object for DefaultTableModel and create settable() method in constructor.
  • See my previous post How to set color in jTable column header using NetBeans for how to call settable() method.
  • Code for settable() method :
 private void settable() {
        String values[][] = {{"Arun", "www.java4projects.blogspot.in"}, {"Digvijay", "www.tutorialdata.com"}, {"Raj", "www.how2java.com"}};
        String title[] = new String[]{"Blogger Name", "Blogger website"};
        dm = new DefaultTableModel(values, title);
        dm.setColumnIdentifiers(title);
        jTable1.getTableHeader().setBackground(Color.PINK);
        jTable1.getTableHeader().setForeground(Color.BLUE);
        jTable1.setModel(dm);
    }
  • Out put :
  • Setting grid line color add single line code given below in the settable() method. As we know jtable has default color which is black.
    jTable1.setGridColor(Color.RED);
    
  • Out Put :

  • Display jTable without grid layout by adding single line code in settable() method.
    jTable1.setShowGrid(false);
    
  • Out Put :
  • Showing only horizontal and vertical grid lines.Change the code alternatively below you get out puts likes this.
  • For Displaying only horizontal lines.
     jTable1.setShowVerticalLines(false);
    
  • Out put :
  • Display only Vertical grid lines.
    jTable1.setShowHorizontalLines(false);
    
  • Setting foreground color to selected cell in jTable.
    jTable1.setSelectionForeground(Color.blue);
    

    Thanks for reading this post. Hope you find best.

2 Responses so far

  1. can the background color of the remaining grey area shown in the screenshoot be painted to a particular color?

  2. Thanks for that really nice

Leave a Reply