Java Swing Application Flow
This document provides an in-depth look at the flow of a Java Swing application using the SnowFall Assignment as a case study. It explores how a Swing application starts from the main method, creating objects and initializing constructors, as well as detailing the role of the `jbInit` method in setting up the user interface. Key concepts like GUI component layout, action commands, and event handling are illustrated through code snippets, making it an essential guide for those looking to grasp the foundational elements of Java Swing application development.
Java Swing Application Flow
E N D
Presentation Transcript
Java Swing Application Flow By mr. Hanley 11/7/2005
How does a swing app flow? • In our Swing Applications, we have used two files, an app file and a frame file • Let’s take the SnowFall Assignment as an Example
How does a swing app flow? • 1. A java application looks for a main method • import java.awt.*; • import javax.swing.*; • public class Proj2App { • public static void main(String[] args) { • Proj2Frame p = new Proj2Frame(); • p.setSize(500,400); • p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); • p.setTitle("Project 2 Basic Mathematical Problems"); • p.setVisible(true); • }
How does a swing app flow? • 2. When the first line of the main is encountered, a new Proj2Frame object is created • Proj2Frame p = new Proj2Frame(); • 3. Since we are creating a new object, a constructor is called • A constructor is a method with the same name of the class
How does a swing app flow? • public Proj2Frame() { • try { • jbInit(); • } • catch (Exception ex) { • ex.printStackTrace(); • } • } • 4. All global variables are created and initialized to either 0 or null • 5. The constructor calls a method called jbInit, which stands for jbuilder initialize
How does a swing app flow? • private void jbInit() throws Exception { • this.getContentPane().setLayout(null); • tabPane.setBounds(new Rectangle(30, 20, 348, 273)); • snowAvgPAN.setLayout(null); • sodaCompPAN.setLayout(null); • areaCirPAN.setLayout(null); • areaLBL.setText("Area"); • areaLBL.setBounds(new Rectangle(24, 93, 90, 24)); • areaTF.setEnabled(false); • areaTF.setEditable(false); • areaTF.setText(""); • areaTF.setBounds(new Rectangle(23, 121, 90, 25)); • cirLBL.setText("Circumference"); • cirLBL.setBounds(new Rectangle(25, 164, 90, 24));
How does a swing app flow? • cirTF.setEditable(false); • cirTF.setBounds(new Rectangle(23, 190, 90, 25)); • radiusLBL.setText("Radius"); • radiusLBL.setBounds(new Rectangle(24, 21, 90, 24)); • radiusTF.setBounds(new Rectangle(23, 50, 90, 25)); • acBUT.setBounds(new Rectangle(154, 47, 90, 29)); • acBUT.setText("Find A+C"); • cost2lTF.setEditable(false); • cost2lTF.setText(""); • cost2lTF.setBounds(new Rectangle(162, 42, 112, 26)); • price6packLBL.setText("Price in $ for a 6 pack"); • price6packLBL.setBounds(new Rectangle(21, 82, 145, 18)); • cost6packTF.setEditable(false); • cost6packTF.setBounds(new Rectangle(161, 107, 112, 26)); • cost2lLBL.setText("Cost in $/L"); • cost2lLBL.setBounds(new Rectangle(161, 16, 145, 18)); • price2lTF.setBounds(new Rectangle(23, 43, 112, 26)); • cost6packLBL.setText("Cost in $/L"); • cost6packLBL.setBounds(new Rectangle(160, 81, 145, 18));
How does a swing app flow? • price2lLBL.setText("Price in $ for 2 Liter"); • price2lLBL.setBounds(new Rectangle(22, 17, 145, 18)); • price6packTF.setText(""); • price6packTF.setBounds(new Rectangle(22, 108, 112, 26)); • twolRB.setText(""); • twolRB.setBounds(new Rectangle(295, 40, 107, 27)); • sixPackRB.setText("jRadioButton1"); • sixPackRB.setBounds(new Rectangle(295, 105, 16, 19)); • includeDepCBX.setText(""); • includeDepCBX.setBounds(new Rectangle(20, 154, 19, 14)); • includeDepLBL.setText("Include Deposit"); • includeDepLBL.setBounds(new Rectangle(48, 152, 100, 18)); • compareBUT.setBackground(Color.orange); • compareBUT.setBounds(new Rectangle(160, 148, 97, 26)); • compareBUT.setActionCommand("compBUT"); • compareBUT.setText("Compare"); • monTF.setText(""); • monTF.setBounds(new Rectangle(21, 32, 75, 24)); • monLBL.setText("Snow Monday"); • monLBL.setBounds(new Rectangle(17, 9, 88, 20));
How does a swing app flow? • tuesTF.setBounds(new Rectangle(20, 85, 75, 24)); • tuesLBL.setText("Snow Tuesday"); • tuesLBL.setBounds(new Rectangle(17, 62, 88, 20)); • wedTF.setBounds(new Rectangle(20, 143, 75, 24)); • wedLBL.setText("Snow Wednesday"); • wedLBL.setBounds(new Rectangle(18, 120, 106, 20)); • thurTF.setBounds(new Rectangle(18, 197, 75, 24)); • thurLBL.setText("Snow Thursday"); • thurLBL.setBounds(new Rectangle(20, 174, 110, 20)); • friTF.setBounds(new Rectangle(165, 32, 75, 24)); • friLBL.setText("Snow Friday"); • friLBL.setBounds(new Rectangle(166, 8, 88, 20)); • avgBUT.setBounds(new Rectangle(160, 68, 90, 74)); • avgBUT.setIcon(flakeIC); • avgBUT.setText("Average"); • avgTF.setEditable(false); • avgTF.setBounds(new Rectangle(164, 195, 75, 24));
How does a swing app flow? • avgLBL.setText("Average"); • avgLBL.setBounds(new Rectangle(166, 172, 88, 20)); • this.getContentPane().add(tabPane); • snowAvgPAN.add(tuesLBL); • snowAvgPAN.add(tuesTF); • snowAvgPAN.add(monTF); • snowAvgPAN.add(wedLBL); • snowAvgPAN.add(wedTF); • snowAvgPAN.add(thurLBL); • snowAvgPAN.add(thurTF); • snowAvgPAN.add(friTF); • snowAvgPAN.add(friLBL); • snowAvgPAN.add(avgBUT); • snowAvgPAN.add(avgTF); • snowAvgPAN.add(avgLBL); • snowAvgPAN.add(monLBL); • tabPane.add(areaCirPAN, "Area/Cir");
How does a swing app flow? • areaCirPAN.add(areaTF); • areaCirPAN.add(radiusTF); • areaCirPAN.add(cirLBL); • areaCirPAN.add(radiusLBL); • areaCirPAN.add(cirTF); • areaCirPAN.add(areaLBL); • areaCirPAN.add(acBUT); • sodaCompPAN.add(price6packLBL); • sodaCompPAN.add(price2lTF); • sodaCompPAN.add(price2lLBL); • sodaCompPAN.add(cost2lLBL); • sodaCompPAN.add(cost6packLBL); • sodaCompPAN.add(cost6packTF); • sodaCompPAN.add(cost2lTF); • sodaCompPAN.add(price6packTF);
How does a swing app flow? • sodaCompPAN.add(twolRB); • sodaCompPAN.add(sixPackRB); • sodaCompPAN.add(includeDepCBX); • sodaCompPAN.add(includeDepLBL); • sodaCompPAN.add(compareBUT); • tabPane.add(sodaCompPAN, "Compare Sodas"); • tabPane.add(snowAvgPAN, "Snow Fall"); • acBUT.addActionListener(this); • compareBUT.addActionListener(this); • avgBUT.addActionListener(this); • }
How does a swing app flow? • 6, Now that the initialize method is done, the app sends various messages to the frame • P.setsize • P.setTitle • P.setVisible • P.setDefaultCloseOp
How does a swing app flow? • 7. The application waits for user initiated events • 8. These trigger the actionPerformed method • 9. The app file is essentially done at this point in time