Java - Flat JTable Design

How to Make a JTable Look Flat And Modern In Java Netbeans

How to Make a JTable Look Flat In Java


In this Java Tutorial we will see How To Create a JFrame with a JTable featuring a flat design, custom cell rendering for alternating row colors and selected cell appearance, and a modern look and feel using the FlatLaf library. 
The cells in the JTable are set to be uneditable.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.
FlatLaf library.





Project Source Code:


package new_tutorials;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 1BestCsharp
 */
public class FlatJTableFrame extends JFrame{

    private JTable table;
    
    public FlatJTableFrame(){
        
        initFrameUI();
        initTable();
        
    }
    
    // Method to initialize the main components of the JFrame
    private void initFrameUI(){
     
        setTitle("Flat JTable");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        table = new JTable();
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
        pack();
        setLocationRelativeTo(null);
    }
    
    // Method to initialize the JTable with data and custom rendering
    private void initTable(){
        
        String[] columnNames = {"Name","Age","Job"};
        Object[][] data = {{"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           {"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           {"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           {"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           {"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           {"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           {"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           {"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},{"John Doe", 30, "Engineer"}, 
                           {"Jane Smith", 25, "Designer"}, 
                           {"Michael Johnson", 40, "Manager"},
                           
                        };
        
        DefaultTableModel model = new DefaultTableModel(data, columnNames){
            // make the cells uneditable
            @Override
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        
        table.setModel(model);
        
        // Set custom cell renderer for alternate row colors and selected cell appearance
        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
        
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
            
                Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                
                if(isSelected){
                    // Set background and text color for selected cell
                    component.setBackground(Color.ORANGE);
                    component.setForeground(Color.WHITE);
                }
                else{
                    // Set background color based on alternating rows
                    component.setBackground(row%2 == 0 ? new Color(113,88,226) : new Color(58,227,255));
                    component.setForeground(Color.BLACK);
                }
                
                return component;
                
            }
            
        });
        
        
    }

    public static void main(String[] args) {
        try
        {
            UIManager.setLookAndFeel(new com.formdev.flatlaf.FlatDarculaLaf());
        }
        catch(UnsupportedLookAndFeelException ex){
            System.out.println(ex.getMessage()); 
        }
        
        FlatJTableFrame frame = new FlatJTableFrame();
        frame.setVisible(true);
    }
    
}


The Final Result:

Flat JTable Design In Java










Share this

Related Posts

Latest
Previous
Next Post »