Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Dimension;
import java.text.NumberFormat;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Main {
    static Icon ICON = UIManager.getIcon("html.pendingImage");

    private static JPanel createPanel() {
        JPanel panel = new JPanel();
        DefaultTableModel model = new DefaultTableModel() {
            @Override
            public Class<?> getColumnClass(int col) {
                if (col == 0) {
                    return Icon.class;
                } else {
                    return Double.class;
                }
            }
        };
        model.setColumnIdentifiers(new Object[] { "Book", "Cost" });
        for (int i = 0; i < 42; i++) {
            model.addRow(new Object[] { ICON, Double.valueOf(i) });
        }
        JTable table = new JTable(model);
        table.setDefaultRenderer(Double.class, new DefaultTableCellRenderer() {
            @Override
            protected void setValue(Object value) {
                NumberFormat format = NumberFormat.getCurrencyInstance();
                setText((value == null) ? "" : format.format(value));
            }
        });
        table.setRowHeight(ICON.getIconHeight());
        panel.add(new JScrollPane(table) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        });
        return panel;
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTabbedPane jtp = new JTabbedPane();
        jtp.addTab("Main1", createPanel());
        jtp.addTab("Main2", createPanel());
        f.add(jtp);
        f.pack();
        f.setVisible(true);
    }
}