Java – Create and Customize Tables Using JTable

Course Outline  |  Previous Lesson

Introduction to JTable

Java makes it easy to create tables for any program because there are libraries like JTable available for creating and customizing them. Tables can be used for displaying data or collecting inputs from users. Please see the video below to learn how to create and customize tables. Source code from that video is posted below the video itself.

 

Source Code

The code below is the same code represented in the video above.

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class Tutorial extends JPanel
{
     JTable jt;
 
     // Constructors are usually used to initialize components in classes. 
     // That's it. This example is a bad practice. Use functions instead for 
     // production code
     public Tutorial()
     {
          // Columns for table
          String[] columns = {"Name", "Age", "Gender"};
          
          // 2D array is used for data in table
          String[][] data = {{"John", "18", "Male"},
                  {"Daisy", "19", "Female"},
                  {"Dave", "23", "Male"},
                  {"Jake", "30", "Male"}};
  
          // Creates Table
          jt = new JTable(data, columns)
          {
               // Determines if data can be entered by users
               public boolean isCellEditable(int data, int columns)
               {
                   return false;
               }
   
               //  Creates cells for the table         
               public Component prepareRenderer(
                            TableCellRenderer r, int data, int columns)
               {
                   Component c = super.prepareRenderer(r, data, columns);
                  
                   // Every even numbers
                   if (data % 2 == 0)
                       c.setBackground(Color.WHITE);
    
                   else
                       c.setBackground(Color.LIGHT_GRAY);
    
                   return c;
               }
         };
  
         // Set size of table     
         jt.setPreferredScrollableViewportSize(new Dimension(450, 63));

         // This will resize the height of the table automatically 
         // to all data without scrolling. 
         jt.setFillsViewportHeight(true);

         JScrollPane jps = new JScrollPane(jt);
         add(jps);
     }
 
     // Creates Window
     public static void main(String[] args)
     {
         JFrame jf = new JFrame("Tutorial");
         Tutorial t = new Tutorial();
         jf.setSize(500, 500);
         jf.setVisible(true);
         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jf.add(t);
     }
}
Course Outline  |  Previous Lesson

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz