Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main extends JPanel {
    public Main() {
        DefaultTableModel model = new DefaultTableModel(0, 5) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        JTable table = new JTable(model);
        for (int i = 0; i < 20; i++) {
            model.addRow(new String[] { i + ".1", i + ".2", i + ".3", i + ".4", i + ".5", });
        }
        add(table, BorderLayout.CENTER);
        table.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                System.out.println("pressed");
                char key = e.getKeyChar();
                int selectedColumn = table.getSelectedColumn();
                for (int i = 0; i < model.getRowCount(); i++) {
                    String value = (String) model.getValueAt(i, selectedColumn);
                    model.setValueAt(value + key, i, selectedColumn);
                }
            }
        });
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Main());
        frame.pack();
        frame.setVisible(true);
    }
}