Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main extends JPanel {
    int PREF_W = 800;

    public Main() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("Foo"));
        buttonPanel.add(Box.createHorizontalStrut(10));
        buttonPanel.add(new JButton("Bar"));

        String[] columnNames = { "Mon", "Tues", "Wed" };
        DefaultTableModel model = new DefaultTableModel(columnNames, 25);
        JTable table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.getViewport().setPreferredSize(table.getPreferredSize());

        JLabel southLabel = new JLabel("OK!");
        southLabel.setForeground(Color.white);
        JPanel southPanel = new JPanel();
        southPanel.add(southLabel);

        setLayout(new BorderLayout(5, 5));
        add(buttonPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
    }

    public Dimension getPreferredSize() {
        Dimension superSize = super.getPreferredSize();
        int width = PREF_W > superSize.width ? PREF_W : superSize.width;
        return new Dimension(width, superSize.height);
    }

    public static void main(String[] args) {
        Main paintEg = new Main();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(paintEg);
        frame.pack();
        frame.setVisible(true);
    }
}