In the previous tutorials, we have implemented a basic JTable using Java. In this example, we are going to see how to integrate different swing components with JTable as Advanced JTable.

Java Advanced JTable :

Here I am going to add a combo box, checkboxes and apply background, foreground colors to table cell values like below.

Advanced JTable Example

AdvJTableDemo.java
package com.swing.examples;

import javax.swing.*;
import javax.swing.table.*;

import java.awt.event.*;
import java.awt.*;
import java.util.Vector;

class AdvJTableDemo extends JFrame {
    JTable table;
    JComboBox comboBox;
    TableModel dataModel;

    Container ContentPane;

    AdvJTableDemo() {
        super("Advanced JTable Demo");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final String[] colHeader = { "Name", "Proficient In", "Experience",
                "Relocate" };
        final Object[][] tableData = {
                { "kranthi", "Java", new Integer(3), new Boolean(false) },
                { "Santosh", "JSP", new Integer(2), new Boolean(true) },
                { "Ram", "VB", new Integer(4), new Boolean(false) },
                { "Raju", "Java", new Integer(5), new Boolean(true) } };

        table = new JTable(tableData,colHeader);

        TableColumn proficient = table.getColumn("Proficient In");

        comboBox = new JComboBox();
        comboBox.addItem("C");
        comboBox.addItem("CPP");
        comboBox.addItem("VB");
        comboBox.addItem("Oracle");
        comboBox.addItem("Java");
        comboBox.addItem("JSP");

        proficient.setCellEditor(new DefaultCellEditor(comboBox));

        TableColumn experiance = table.getColumn("Experience");

        DefaultTableCellRenderer defaultRenderer = new DefaultTableCellRenderer() {
            public void setValue(Object value) {
                int cellValue = (value instanceof Number) ? ((Number) value)
                        .intValue() : 0;
                setForeground((cellValue > 3) ? Color.green : Color.black);
                setBackground((cellValue > 3) ? Color.black : Color.white);
                setText(value.toString());
            }
        };

        
        getRenderer().setHorizontalAlignment(JLabel.RIGHT);

        // Adding DefaultTableCellRenderer to experiance column
        experiance.setCellRenderer(getRenderer());

        ContentPane = getContentPane();

        ContentPane.add(new JScrollPane(table));

        pack();
        setVisible(true);
    }

    public static DefaultTableCellRenderer getRenderer() {
        // Applying background color to cell
        return new DefaultTableCellRenderer() {
            public void setValue(Object value) {
                int cellValue = (value instanceof Number) ? ((Number) value)
                        .intValue() : 0;
                setForeground((cellValue > 3) ? Color.green : Color.black);
                setBackground((cellValue > 3) ? Color.black : Color.white);
                setText(value.toString());
            }
        };
    }
}

class AdvancedJTable {

    public static void main(String args[]) throws Exception {
        new AdvJTableDemo();
    }
}

Output :

Advanced JTable Example

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment