NOTE: UPDATED CODE A FEW POSTS DOWN
So a few months ago I got really annoyed with the stupidity of all of the layouts Java supplies for organizing components. I said to myself, why cant I just tell a GUI element to be placed at this specific coordinate? Then I found SpringLayout. After a few minutes of that, I had a minor stroke. SpringLayout is the most versatile of the Java supplied layouts, but it is incredibly ridiculous and confusing for any normal human to understand. So, I figured I'd create my own layout that would use the versatility of SpringLayout and provide me with my desired coordinate placing. So, for those who have gotten really pissed off at Java Layouts like I have, here is my Coordinate Layout code and a small test program for you to see how it works. It is very simple to use and works beautifully. If there are any questions, I'll be happy to answer them.
Layout Class:
import javax.swing.SpringLayout; import java.awt.Container; import java.awt.Component; public class CoordinateLayout extends SpringLayout { //Container Variable Container cont; //Constructor public CoordinateLayout(Container ct) { //Extend SpringLayout super(); //Set Layout to Container ct.setLayout(this); //Initialize Container Variable cont = ct; } //Method to Add Components -- (0,0) is top left of container public void addComponent(Component comp, int x, int y) { //Adds Component to Container cont.add(comp); //Set both x and y SpringLayout contraints super.putConstraint(SpringLayout.WEST, comp, x, SpringLayout.WEST, cont); super.putConstraint(SpringLayout.NORTH, comp, y, SpringLayout.NORTH, cont); } }
Test Class:
import javax.swing.*; import java.awt.*; public class CoordinateLayoutTest { public static void main(String[] args) { //Create Frame and Panel JFrame mainFrame = new JFrame("TEST"); JPanel first = new JPanel(); //Create Layout and send it the Panel CoordinateLayout layout = new CoordinateLayout(first); //Create components JLabel l1 = new JLabel("First Name:"); JTextField b1 = new JTextField(10); JLabel l2 = new JLabel("Last Name:"); JTextField b2 = new JTextField(10); JLabel l3 = new JLabel("Home City:"); JTextField b3 = new JTextField(10); JLabel l4 = new JLabel("Home State:"); JTextField b4 = new JTextField(10); JLabel l5 = new JLabel("Account Number:"); JTextField b5 = new JTextField(10); //Add components layout.addComponent(l1,5,5); layout.addComponent(b1,105,5); layout.addComponent(l2,5,35); layout.addComponent(b2,105,35); layout.addComponent(l3,5,65); layout.addComponent(b3,105,65); layout.addComponent(l4,5,95); layout.addComponent(b4,105,95); layout.addComponent(l5,5,125); layout.addComponent(b5,105,125); //Simplify adding components by creating JLabels when you use them /* JTextField b1 = new JTextField(10); JTextField b2 = new JTextField(10); JTextField b3 = new JTextField(10); JTextField b4 = new JTextField(10); JTextField b5 = new JTextField(10); layout.addComponent(new JLabel("First Name:"),5,5); layout.addComponent(b1,105,5); layout.addComponent(new JLabel("Last Name:"),5,35); layout.addComponent(b2,105,35); layout.addComponent(new JLabel("Home City:"),5,65); layout.addComponent(b3,105,65); layout.addComponent(new JLabel("Home State:"),5,95); layout.addComponent(b4,105,95); layout.addComponent(new JLabel("Account Number:"),5,125); layout.addComponent(b5,105,125); */ //Finish creating Frame mainFrame.setSize(300,300); mainFrame.setContentPane(first); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setVisible(true); } }