GUI form that can edit an address entry :: Jython
Adding an Input Form for an Address Entry let's create a GUI form that can edit an address entry. We want a frame that holds text field components, which will be used to gather the address information. We also want an OK button, which signifies that the user is done entering the current address. To get the layout for the frame components, we'll use a technique that layers components in a container. For this we'll put the text fields in one panel and the OK button in another. The panels themselves are components that we'll put in the frame. The first thing we need to do is import the JFrame, JButton, and JTextField classes from javax.swing. >>> from javax.swing import JFrame >>> from javax.swing import JButton, JTextField Next we create a frame with the titlebar set to "Address Entry," create an OK button and a panel to hold it, set the frame's layout to BorderLayout, and add the panel to the south region of the frame. Then we pack the frame and look at the results. >>> frame = JFrame("Address Entry", visible=1) >>> okay = JButton("OK") >>> from javax.swing import JPanel >>> buttonbar = JPanel() >>> buttonbar.add(okay) >>> from java.awt import BorderLayout >>> frame.contentPane.add(buttonbar, BorderLayout.SOUTH) >>> frame.pack() Adding Text fields Now we want to add text fields to the frame. There may be many of them, and the frame is using BorderLayout, which can hold only five components (EAST, WEST, NORTH, SOUTH, and CENTER). Therefore, we add the fields to another panel, which we place in the CENTER region of the frame. We'll use GridLayout for the layout manager of the panel, which will lay the components out in a grid. As we add fields, we'll label them so that the user knows what each one represents. We'll do this by adding instances of the Label class near the corresponding text field. Import GridLayout, and create an instance of it that lays out components in a 2-by-2 grid. Create a panel to hold the text fields called contentPane. Set the layout of contentPane to the GridLayout instance. >>> from java.awt import GridLayout >>> grid = GridLayout(2,2) >>> contentPane = JPanel() >>> contentPane.setLayout(grid) Create a JTextField instance called name. Import the Label class, create an instance of it, and add it to contentPane. Add the name JTextField to the panel. Then pack the frame, and look at the results. >>> name = JTextField() >>> from java.swing import JLabel >>> contentPane.add(JLabel("Name")) >>> contentPane.add(name) >>> frame.pack() >>> frame.contentPane.add(contentPane, BorderLayout.CENTER) >>> frame.pack() Now use the same technique to add phone number and email text fields. Add the phone number field. >>> phoneNumber = JTextField() >>> contentPane.add(JLabel("Phone Number")) >>> contentPane.add(phoneNumber) Add the email field. >>> email = JTextField() >>> contentPane.add(JLabel("eMail")) >>> contentPane.add(email) >>> frame.pack() Adding an Input Form for an Address Entry At this point, you probably feel comfortable with the concepts the prototype introduces. To incorporate these concepts into the address book program, let's organize our code into a class called AddressForm. This class extends the javax.swing.JFrame class, which makes it a JFrame. AddressForm is passed an instance of the Address class in its constructor. okayHandler() populates the fields of the instance with values extracted from AddressForm's text fields. Look at Address6.py below and make sure to read all of the comments. from javax.swing import JFrame, JButton, JTextField, JLabel, JPanel from java.awt import BorderLayout, GridLayout # AddressForm Class. # This class is used to edit an instance of the Address class class AddressForm(JFrame): def __init__(self, address): """Constructor for the AddressForm class \n """ # Call constuctor JFrame.__init__(self, "Address Entry", visible=1) # Declare private variables self.__address = address #hold address to be edited self.__name = NONE #hold the private name textfield self.__email = NONE #hold the private email textfield self.__phoneNumber = NONE #hold the private phone# field self.__init__CreateButtonBar() self.__init__CreateTextFieldPanel() def __init__CreateButtonBar(self): """ Create okay button, a panel for the button. \n """ """ Add the button panel to this frame. """ okay = JButton("OK") buttonbar = JPanel() buttonbar.add(okay) self.contentPane.add(buttonbar, BorderLayout.SOUTH) # Set the event handler for the okay button. okay.actionPerformed = self.__okayHandler def __init__CreateTextFieldPanel(self): """ Set up the email, phoneNumber and name text fields """ # Create the panel and set the grid layout. # The grid has a layout of a # 3 row by 2 column grid. editPane = JPanel() editPane.setLayout(GridLayout(3,2)) self.contentPane.add(editPane, BorderLayout.CENTER) # Create the name textfield and add it and its # associated label to the contentPane. # Note that name is a member of AddressForm self.__name = JTextField() editPane.add(JLabel("Name")) editPane.add(self.__name) # Create the phoneNumber textfield and add it and its # associated label to the contentPane. # Note that phoneNumber is a member of AddressForm. self.__phoneNumber = JTextField() editPane.add(JLabel("Phone Number")) editPane.add(self.__phoneNumber) # Create the email textfield and add it and its # associated label to the contentPane. # Note that email is a member of AddressForm. self.__email = JTextField() editPane.add(JLabel("eMail")) editPane.add(self.__email) self.pack() #Defines the event handler for the okay button def __okayHandler(self, event): """Event handler for the okay button""" print (self.__name.text, self.__email.text, self.__phoneNumber.text) Address.__init__(self.__address, self.__name.text, self.__phoneNumber.text, self.__email.text) print self.__address Stay tuned to our blog on further modifications on this program (e.g. handlers, main etc.) www.proexcellers.com Read the full article













