Tuesday , November 21 2017
Home / java / Java Swing Advanced JTable Example

Java Swing Advanced JTable Example

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 combobox, checkboxes and apply background, foreground colors to table cell values like below.

Advanced JTable Example


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 chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

How to add dynamic files to JTree

In the previous tutorial, we learn about a simple JTree. This tutorial is for adding …

Leave a Reply

Your email address will not be published. Required fields are marked *