Java - How To Create a JTable With Multi-Line Header In Java Netbeans

How to Create Multiline JTable Header In Java Netbeans

How to Create Multiline JTable Header In Java Netbeans




In this Java Tutorial we will see How To Create a multi-line header table using Java JTable Swing component. 

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.

What This Project Do:

- Create JFrame containing a JTable with multi-line headers, providing a visual representation of how to handle and display such headers.





Project Source Code:


package new_tutorials;

import java.awt.Component;
import java.awt.Dimension;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

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

    public MultiLineHeaderTable()
    {
        setTitle("MultiLine Header Table");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        String[] columnNames = {
                                    "<html>Column 1<br>Line 1</html>",
                                    "<html>Column 2<br>Line 1<br>Line 2</html>",
                                    "<html>Column 3<br>Line 1<br>Line 2<br>Line 3</html>"
                                };
        
        Object[][] data = {
                            {"Data 1","Data 2","Data 3"},
                            {"Data 4","Data 5","Data 6"}
                           };
        
        JTable table = new JTable(data, columnNames);
        
        resizeHeaderHeight(table);
        
        JScrollPane scrollPane = new JScrollPane(table);
        
        add(scrollPane);
        
        setSize(400,300);
        
        setLocationRelativeTo(null);
    }
    
    
    // Method to dynamically adjust the height of the table header 
    // based on the content of multi-line headers
    private void resizeHeaderHeight(JTable table)
    {
        // Get the table header
        JTableHeader header = table.getTableHeader();
        // Get the default renderer for the header
        DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) header.getDefaultRenderer();
        // Initialize a variable to store the maximum height of the header
        int height = 0;
        // Iterate over each column in the table model
        for(Enumeration<TableColumn> columns = table.getColumnModel().getColumns(); columns.hasMoreElements();)
        {
            // Get the current column
            TableColumn column = columns.nextElement();
            // Get a component (renderer) for the header cell
            Component comp = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, 0);
            // Get the preferred height of the header cell
            int columnHeight = comp.getPreferredSize().height;
            // Update the maximum height if the current column's header is taller
            height = Math.max(height, columnHeight);
        }
        
        // Set the preferred size of the header to the maximum height, maintaining its width
        header.setPreferredSize(new Dimension(header.getWidth(), height));
        
    }
    
    
    
    public static void main(String[] args) {
        new MultiLineHeaderTable().setVisible(true);
    }
    
}


The Final Result:

How To Create a JTable With Multi-Line Header In Java Netbeans





Aucun commentaire:

Enregistrer un commentaire