Java UserInterface Class For Easy Swing UI’s
During my studies at university I've constantly needed to create simple little GUI's to make things nicer and easier to get things done. Sadly, with Java, creating graphical user interfaces can be somewhat complex. Because of this, I created this UserInterface class that easily allows the creation of nice Java Swing windows.
How to use
There are three steps to use the UserInterface class.
- Create a class that inherits the Userinterface class (see the example)
- In the constructor, call the super class with what window text you want
- e.g. super("Window Text")
- Create a drawUI() method that adds all the components to the window, and calls the inherited show() method.
Basic functionality
The basic thing I wanted to be able to do is just to have a label with a textbox beside it. Most of my projects accepted user input, had a button, and did some stuff based on that. So to create a row that has a label and a textbox (or any other two components side-by-side) the following code would do it:
addLabeledRow(new JLabel("Enter some text"), new JTextField(10));
It's as simple as that. There are, of course, more advanced functions that don't just limit you to 2 columns of components.
AddItemToColumn
The addItemToColumn function accepts two parameters, which are the column to place the component in, and the component itself. This function needs to be used along with the advanceRow function, which sets the active row to the next one. Code:
addItemToColumn(0, new JLabel("Username")); addItemToColumn(1, new JTextField(10)); addItemToColumn(2, new JLabel("Password")); addItemToColumn(3, new JTextField(10)); advanceRow(); //go to the next row addItemToColumn(0, new JButton("Submit"));
So the code given up until now would create this window:

The complete code is available for this example, and the UserInterface.java is also available. It's pretty simple and UserInterface.java is fairly well documented - it shouldn't be too hard to create a window.
