Swing GUI and Flowcharts - Java
Swing basics
- Swing is a package that lets you create applications that use a flashy Graphical User Interface (or GUI) instead of a dull console interface.
- The Swing API provides many different classes for creating various types of user interface elements.
Understanding what Swing is
- Three classes: JFrame, JPanel, and JLabel.
- These classes are part of a larger collection of classes that are all related through inheritance.
- The Swing family tree splits the Component class into one group of classes that are derived from the JComponent class, and another branch that descends from the Window class.
The Swing class hierarchy
- Object: All classes ultimately derive from Object, thus this class is at the top of the tree.
- Component: represents an object that has a visual representation that can be shown on-screen and that can interact with users. This class defines some basic methods that are available to all Swing classes.
- Container: builds on the basic visual capabilities of the Component class by adding the ability to hold other containers.
- Window: a specialized type of container object that has a border, a title bar, buttons that minimize, maximize, and close the window, and that can be repositioned and possibly even resized by the user.
- Frame: a type of Window that serves as the basis for Java GUI applications.
- Frame is an AWT class that has been improved upon by the JFrame class.
- JFrame: the Swing version of the older Frame class. Most of the Swing applications include at least one JFrame object.
- JComponent: is the basis for all other Swing components except for frames.
- JPanel: used to organize and control the layout of other components such as labels, buttons, text fields, etc.
Using the JPanel class
- Panel is a type of container that's designed to hold a group of components so they can be displayed on a frame.
- The normal way to display a group of controls such as text fields, labels, buttons, and other GUI widgets is to add those controls to a panel, and then add the panel to the frame.
- You can bypass the panel and add the controls directly to the frame if you want, but using a separate panel to hold the frame's controls is almost always a good idea.
JLabel
- JLabel creates a label that displays a simple text value.
- A label is a component that simply displays text.
- Labels are used for a variety of purposes:
- To display captions for other controls such as text fields or combo boxes,
- To display informational messages,
- To show the results of a calculation or a database lookup.
- A label can also display an image, or it can display both an image and some text.
- Introduction to Java
Using labels (additional detail)
- A label can display text or image as described above.
- Next to labels, the Swing component used most is the JButton component which creates a button the user can click.
- You can either create an empty button or a button with text.
What is a Flowchart?
- A diagram that shows the flow of a program.
- Example: A pay-calculating program (inputs hours and rate, multiplies, and displays gross pay).
- The formula for gross pay is gross pay=hours×rate.
Basic Flowchart Symbols
- Terminal (Rounded Rectangle): Start or end point.
- Input/Output (Parallelogram): Data input or output.
- Process (Rectangle): Computation or variable assignment.
- Decision (Diamond): Yes/No branch (e.g., if statements).
- Connector (Circle with letter): Links flowchart parts.
- Module (Rectangle with double bars): Represents a separate procedure or function.
Flowchart Structures
- Sequence
- Actions performed one after another.
- Example: Pay-calculating program.
- Decision
- Uses conditions (if/else).
- Example: if (x < y) choose between two actions.
- Repetition
- Looping structures (while, for).
- Example: while (x < y) x++;
- Must update condition to avoid infinite loop.
- Case
- Multi-way branching.
- Example: years_employed determines different bonus values.
Controlling Loops
- A loop must eventually terminate.
- Example: If variable is not updated inside the loop, it causes an infinite loop.
Combining Structures
- Complex flowcharts often mix sequence + decision + repetition.
- Example: Checking if x is within min and max range (two decisions combined).