More Widgets


The SIS application should allow users to add new courses. To support this, CoursesPanel can contain a couple of entry fields in which the user may type the course department and course number. The user should be able to click an "Add" button that adds a new course, created using the entered department and number, to the list.


The test for the view:

 package sis.ui; import junit.framework.*; import javax.swing.*; import java.awt.*; import static sis.ui.CoursesPanel.*; public class CoursesPanelTest extends TestCase {    public void testCreate() {       CoursesPanel panel = new CoursesPanel();       JLabel label =          (JLabel)Util.getComponent(panel, LABEL_NAME);       assertEquals(LABEL_TEXT, label.getText());       JList list =          (JList)Util.getComponent(panel, COURSES_LIST_NAME);       assertEquals(0, list.getModel().getSize());       JButton button =          (JButton)Util.getComponent(panel, ADD_BUTTON_NAME);       assertEquals(ADD_BUTTON_TEXT, button.getText());       JLabel departmentLabel =          (JLabel)Util.getComponent(panel, DEPARTMENT_LABEL_NAME);       assertEquals(DEPARTMENT_LABEL_TEXT, departmentLabel.getText());       JTextField departmentField =          (JTextField)Util.getComponent(panel, DEPARTMENT_FIELD_NAME);       assertEquals("", departmentField.getText());       JLabel numberLabel =          (JLabel)Util.getComponent(panel, NUMBER_LABEL_NAME);       assertEquals(NUMBER_LABEL_TEXT, numberLabel.getText());       JTextField numberField =          (JTextField)Util.getComponent(panel, NUMBER_FIELD_NAME);       assertEquals("", numberField.getText());    } } 

The qualified class constants were getting a bit unwieldy, so I decided to do a static import of the CoursesPanel class. In this case, there is little possibility for confusion about the origin of the class constants.

The role of new component types JButton, JTextField, and JList should be apparent. Code for most of the assertions against the new widgets is similar to the previously coded test for the label.

The assertion against the JList object is different. It proves that the JList on a newly constructed CoursesPanel is empty. You can determine how many elements a JList contains by first obtaining its model, then asking the model for its size. A JList uses a model object to contain its data. You will learn more about list models in the upcoming section, List Models.

The modified view class:

 package sis.ui; import javax.swing.*; import java.awt.*; public class CoursesPanel extends JPanel {    static final String NAME = "coursesPanel";    static final String LABEL_TEXT = "Courses";    static final String LABEL_NAME = "coursesLabel";    static final String COURSES_LIST_NAME = "coursesList";    static final String ADD_BUTTON_TEXT = "Add";    static final String ADD_BUTTON_NAME = "addButton";    static final String DEPARTMENT_FIELD_NAME = "deptField";    static final String NUMBER_FIELD_NAME = "numberField";    static final String DEPARTMENT_LABEL_NAME = "deptLabel";    static final String NUMBER_LABEL_NAME = "numberLabel";    static final String DEPARTMENT_LABEL_TEXT = "Department";    static final String NUMBER_LABEL_TEXT = "Number";    public static void main(String[] args) {       show(new CoursesPanel());    }    private static void show(JPanel panel) {       JFrame frame = new JFrame();       frame.setSize(300, 200);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.getContentPane().add(panel); // 1       frame.setVisible(true);    }    public CoursesPanel() {       setName(NAME);       createLayout();    }    private void createLayout() {       JLabel label = new JLabel(LABEL_TEXT);       label.setName(LABEL_NAME);       JList list = new JList();       list.setName(COURSES_LIST_NAME);       JButton addButton = new JButton(ADD_BUTTON_TEXT);       addButton.setName(ADD_BUTTON_NAME);       int columns = 20;       JLabel departmentLabel = new JLabel(DEPARTMENT_LABEL_TEXT);       departmentLabel.setName(DEPARTMENT_LABEL_NAME);       JTextField departmentField = new JTextField(columns);       departmentField.setName(DEPARTMENT_FIELD_NAME);       JLabel numberLabel = new JLabel(NUMBER_LABEL_TEXT);       numberLabel.setName(NUMBER_LABEL_NAME);       JTextField numberField = new JTextField(columns); numberField.setName(NUMBER_FIELD_NAME);       add(label);       add(list);       add(addButton);       add(departmentLabel);       add(departmentField);       add(numberLabel);       add(numberField);    } } 

The tests should pass. You should see a window similar to that in Figure 3 when you compile and execute the view class.

Figure 3.


The user interface is a mess. You'll rectify this in the upcoming section entitled Layout. Also, nothing that would seem to be a JList appears in the window. One reason is that you have added no elements to the listit is empty. You'll fix this in time as well.

Even with only seven widgets, you've written a good amount of tedious code to test and build the user interface. Let's see what we can do to tighten it up.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net