import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;

public class JTextFieldManyColumnTable {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            // Create the main frame
            JFrame frame = new JFrame("JTable Grid Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 200); // Set frame size
            frame.setLocationRelativeTo(null); // Center the frame

            // Define column names
            String[] columnNames = new String[30];
            for (int i = 0; i < 30; i++) {
                columnNames[i] = "Column " + (i + 1);
            }

            // Create data for the table (6 rows, 30 columns)
            Object[][] data = new Object[6][30];
            for (int row = 0; row < 6; row++) {
                for (int col = 0; col < 30; col++) {
                    data[row][col] = new JTextField("Cell " + (row * 30 + col + 1)); // Set "Cell 1", "Cell 2", etc.
                }
            }

            // Create the table model and JTable
            DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);
            JTable table = new JTable(tableModel);

            // Disable automatic column resizing
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

            // Set the width of each column to a fixed value
            for (int i = 0; i < table.getColumnCount(); i++) {
                TableColumn column = table.getColumnModel().getColumn(i);
                column.setPreferredWidth(100); // Set each column width to 100 pixels
            }

            // Set the minimum size for the table to ensure it exceeds the visible area
            table.setMinimumSize(new Dimension(3000, table.getPreferredSize().height));

            // Set a custom cell renderer to add accessibleName and accessibleDescription
            table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

                    if (value instanceof JTextField) {
                        JTextField textField = (JTextField) value;
                        // Set accessible parent for accessibility
                        textField.getAccessibleContext().setAccessibleParent(table);

                        // Enhance accessibility - Set original value as accessible name
                        textField.getAccessibleContext().setAccessibleName(textField.getText());
                        textField.getAccessibleContext().setAccessibleDescription("Text field in row " + (row + 1) + ", column " + (column + 1));

                        return textField;
                    }
                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                }
            });
            table.revalidate();
            table.repaint();
            // Add the table to a JScrollPane to allow horizontal scrolling
            JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

            // Add the scroll pane to the frame
            frame.add(scrollPane);

            // Make the frame visible
            frame.setVisible(true);
        });
    }
}
