Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class Main {
    public static void main(String[] args) {
        String[] headers = { "column 1", "column 2", "column 3", "column 4" };
        int cols = 4;
        int rows = 6;
        String[][] data = new String[rows][cols];
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                data[row][col] = "item " + (row * cols + col + 1);
            }
        }
        JTable table = new JTable(data, headers);
        DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) table.getDefaultRenderer(String.class);
        renderer.setHorizontalAlignment(JLabel.RIGHT);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(table));
        f.setSize(400, 400);
        f.setLocation(200, 200);
        f.setVisible(true);
    }
}