Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Main {
    static int NUM = 20;
    JTextField[] fields = new JTextField[NUM];
    JLabel[] labels = new JLabel[NUM];

    private JPanel create() {
        JPanel panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.ParallelGroup parallel = layout.createParallelGroup();
        layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(parallel));
        GroupLayout.SequentialGroup sequential = layout.createSequentialGroup();
        layout.setVerticalGroup(sequential);
        for (int i = 0; i < NUM; i++) {
            labels[i] = new JLabel(String.valueOf(i + 1), JLabel.RIGHT);
            fields[i] = new JTextField(String.valueOf("" + (i + 1)));
            labels[i].setLabelFor(fields[i]);
            parallel.addGroup(layout.createSequentialGroup().addComponent(labels[i]).addComponent(fields[i]));
            sequential.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(labels[i])
                    .addComponent(fields[i]));
            layout.linkSize(SwingConstants.HORIZONTAL, labels[i], labels[0]);
        }
        return panel;
    }

    public static void main(String[] args) {
        JPanel panel = new Main().create();
        JScrollPane jsp = new JScrollPane(panel) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        JOptionPane.showMessageDialog(null, jsp, "Data", JOptionPane.PLAIN_MESSAGE);

    }
}