CS365 Homework Assignment 6

I have created a number of executables for this homework assignment which you can use to both clarify the problem description and to test your solutions. To access the executables you should create your own directory and then copy ~cs365/www-home/hw/hw6/*.class to your directory. You should not directly execute the class files from the cs365 directory because they will try to pop up windows on your display and that will cause problems because the files will not be running locally (if the files are not running locally then security issues arise because an application should not be allowed to pop up windows in a random location without the permission of the user).

  1. (10 points) Fill in the blanks for each of the following questions:

    1. The command you must call to get a layout manager to recompute the layout of its components is the _____________ command.

    2. The number of items that a person can hold in short-term memory is _____________, plus or minus ______________.

    3. If you wanted to create a horizontal row of equally-spaced, equal-width widgets, you would want to use a ______________ layout manager.

    4. If you are creating a menu that has a number of commands, the maximum number of commands on the menu should be from _______________ to _______________.

    5. The command that you must call to get a window to invoke its layout managers and potentially re-calibrate its size is the ________________ command.

  2. (25 points) Write a java class called MyMenu that has a menu bar with two menus entitled "File" and "Widgets." The file menu should have a single menu item entitled "Quit" and the widgets menu should have two menu items labeled "Enable slider" and "Enable text box" respectively. The initial display should have a slider and text box displayed vertically using a grid layout. The text field should be 5 characters wide and should be prefixed with the label "value". The slider should have a range of values between 0 and 100 with an initial value of 50. Both the slider and the text field should be initially disabled.

    If the "Enable slider" item is selected, then your application should enable the slider and disable the text field. Similarly if the "Enable text box" item is selected, then your application should enable the text field and disable the slider. The slider's value and the text field's value should always be kept in sync so if the slider is moved the text field's value should be updated and if the user types a new value into the text field then the slider's value should be updated. If the user enters either a non-integer or an integer value that is not between 0 and 100 into the text field then your application should display an error dialog box with an appropriate message and it should revert to the previous value.

    If you have questions about what your solution should do, you can execute my MyMenu executable.

    Extra Credit: The following two items can be done separately

    1. (10 points): Use a JFormattedTextBox to ensure that the user always enters an integer. You will still have to check to see whether the value is between 0 and 100. If the user enters a value such as "5a" the JFormattedTextBox will revert the value to 5, which is fine.

    2. (10 points): Add a "Set slider values" item to your widgets menu. When this option is chosen, pop up a window that allows the user to enter a minimum, maximum, and starting value using text fields with appropriate labels. Use your imagination in designing the window. Do not perform any error checking. You may assume that the user enters integers, that the max value is greater than the min value, and that the starting value is between the min and max values.

  3. (20 points) Write a java class called TextLayout that vertically stacks four text fields labeled "Name:", "Fax:", "Email:", and "Address:". The labels for the four text fields should be left aligned and each text label should be separated from its text field by 5 pixels. The text fields do not have to be left aligned. However, the text fields should be 10 columns wide and should remain this size regardless of how large the window is made.

    If you have questions about what your solution should do, you can execute my TextLayout executable.

  4. (10 points) Write a java class called TextLayout1 that repeats the previous exercise with the following two changes:

    1. The text labels should be right-aligned with one another and they should be vertically centered with respect to their text field.

    2. The text fields should be left aligned with one another. They should also grow to accommodate the new size of the window when the window is re-sized. This way I can be sure that your texty labels stay centered with respect to the text fields.

    If you have questions about what your solution should do, you can execute my TextLayout1 executable.

  5. (35 points) Write a java class called Invoice that allows a user to enter items into an invoice form. The invoice form should consist of three parts:

    1. "Add Item" Button: This button should be placed at the top of window and should add items to the invoice. You will need to call the pack command in order to get the window to re-size itself properly.

    2. Invoice: The invoice should be a panel in the center of the window which is laid out using grid layout. Each item in the invoice will have three JTextFields which should be 10 columns each: 1) a name, 2) a quantity, and 3) a price per item, and a JLabel, or a custom subclass of JLabel that you declare, which represents the subtotal. The subtotal is equal to the quantity multiplied by the price per item. The subtotal should be right-justified. The first row of the invoice should have the labels for the fields, which should be: 1) name, 2) quantity, 3) price, 4) subtotal. The swing elements in this first row should all be JLabels.

    3. Total Label: At the bottom of the screen you should have two elements, a JLabel with the label "total:" and a a JLabel, or custom subclass of JLabel that you declare, whose label is the sum of the subtotal fields. You should use a flow layout to layout these two elements.

    Your program should allow the user to edit the name, quantity, and price per item fields. The quantity and price fields should be integers. For this lab assume that the user enters integers. Whenever the quantity or price per item fields are updated, the corresponding subtotal field should be updated and the total label should also be updated.

    If you have questions about how your interface should look or operate you can check out the executable in Invoice