import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.UnsupportedEncodingException; public class RequestJFrame extends StandardJFrame { String baseURL="http://userpages.umbc.edu/~emurian/cgi-bin/ShowData?"; JApplet myJApplet; MyJButton myButton; MyJButton submit, clear, exit; SubmitButtonHandler submitlistener; ExitButtonHandler exitlistener; ClearButtonHandler clearlistener; JPanel holdbuttons; String firstName, mI, lastName, address, favoritePresident, politicalParty, leastFavoritePresident, firstYearVoted; String city, state, zip; JTextField firstNameInput, miInput, lastNameInput, addressInput, favoritePresidentInput, politicalPartyInput, leastFavoritePresidentInput, firstYearVotedInput; JTextField cityInput, stateInput, zipInput; JLabel L_First_Name, L_MI, L_Last_Name, L_Address, L_Favorite_President, L_Political_Party, L_Least_Favorite_President, L_First_Year_Voted; JLabel L_City, L_State, L_Zip; //This stands for the Name JLabel. I used "L" to identify the labels on the input fields. JLabel top; JPanel bigPanel; JScrollPane scroller; /* Pass down the JApplet and button b4 from MainJProgram.java */ public RequestJFrame(JApplet a, MyJButton mjb) { myJApplet=a; myButton = mjb; setTitle("Request Information"); setSize(600,650); setLocationRelativeTo(null); getContentPane().setLayout(new BorderLayout()); /* bigPanel will be used to hold all the objects. You must use pixel placements for the objects. bigPanel will be used as an argument for the JScrollPane object. */ bigPanel = new JPanel(); bigPanel.setLayout(null); bigPanel.setBackground(Color.BLACK); //To see it. Change to white later. bigPanel.setOpaque(true); //The x dimension will be determined by the JFrame and BorderLayout size. bigPanel.setPreferredSize(new Dimension(400,800)); /* The minimum size of bigPanel will be the size of the JScollPane object that is determined by the BorderLayout manager. When the preferred height of bigPanel is larger than the JScollPane object, scrollbars will be generated automatically. The width should be less than the JFrame, and it will be automatically adjusted. */ firstNameInput = new JTextField(); firstNameInput.setLocation( 160, 50 ); firstNameInput.setSize( 100, 25 ); firstNameInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( firstNameInput ); miInput = new JTextField(); miInput.setLocation( 160, 100 ); miInput.setSize( 30, 25 ); miInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( miInput ); lastNameInput = new JTextField(); lastNameInput.setLocation( 160, 150 ); lastNameInput.setSize( 100, 25 ); lastNameInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( lastNameInput ); addressInput = new JTextField(); addressInput.setLocation( 160, 200 ); addressInput.setSize( 300, 25 ); addressInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( addressInput ); favoritePresidentInput = new JTextField(); favoritePresidentInput.setLocation( 160, 400 ); favoritePresidentInput.setSize( 150, 25 ); favoritePresidentInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( favoritePresidentInput ); politicalPartyInput = new JTextField(); politicalPartyInput.setLocation( 160, 500 ); politicalPartyInput.setSize( 100, 25 ); politicalPartyInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( politicalPartyInput ); leastFavoritePresidentInput = new JTextField(); leastFavoritePresidentInput.setLocation( 160, 450 ); leastFavoritePresidentInput.setSize( 150, 25 ); leastFavoritePresidentInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( leastFavoritePresidentInput ); firstYearVotedInput = new JTextField(); firstYearVotedInput.setLocation( 160, 550 ); firstYearVotedInput.setSize( 50, 25 ); firstYearVotedInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( firstYearVotedInput ); cityInput = new JTextField(); cityInput.setLocation( 160, 250 ); cityInput.setSize( 200, 25 ); cityInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( cityInput ); stateInput = new JTextField(); stateInput.setLocation( 160, 300 ); stateInput.setSize( 35, 25 ); stateInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( stateInput ); zipInput = new JTextField(); zipInput.setLocation( 160, 350 ); zipInput.setSize( 60, 25 ); zipInput.setFont( new Font("Arial", 1, 12) ); bigPanel.add( zipInput ); // "TRAILING" applies to Right Justification L_First_Name = new JLabel("First Name", JLabel.TRAILING); L_First_Name.setLocation( 5, 50 ); L_First_Name.setSize( 140, 25 ); L_First_Name.setForeground(Color.WHITE); bigPanel.add( L_First_Name); L_First_Name.setFont( new Font("Arial", 1, 12) ); L_MI = new JLabel("Middle Initial", JLabel.TRAILING); L_MI.setLocation( 5, 100 ); L_MI.setSize( 140, 25 ); L_MI.setForeground(Color.WHITE); bigPanel.add( L_MI); L_MI.setFont( new Font("Arial", 1, 12) ); L_Last_Name = new JLabel("Last Name", JLabel.TRAILING); L_Last_Name.setLocation( 5, 150 ); L_Last_Name.setSize( 140, 25 ); L_Last_Name.setForeground(Color.WHITE); bigPanel.add( L_Last_Name); L_Last_Name.setFont( new Font("Arial", 1, 12) ); L_Address = new JLabel("Address", JLabel.TRAILING); L_Address.setLocation( 5, 200 ); L_Address.setSize( 140, 25 ); L_Address.setForeground(Color.WHITE); bigPanel.add( L_Address); L_Address.setFont( new Font("Arial", 1, 12) ); L_Favorite_President = new JLabel("Best President", JLabel.TRAILING); L_Favorite_President.setLocation( 5, 400 ); L_Favorite_President.setSize( 140, 25 ); L_Favorite_President.setForeground(Color.WHITE); bigPanel.add( L_Favorite_President); L_Favorite_President.setFont( new Font("Arial", 1, 12) ); L_Political_Party = new JLabel("Political Party", JLabel.TRAILING); L_Political_Party.setLocation( 5, 500 ); L_Political_Party.setSize( 140, 25 ); L_Political_Party.setForeground(Color.WHITE); bigPanel.add( L_Political_Party); L_Political_Party.setFont( new Font("Arial", 1, 12) ); L_Least_Favorite_President = new JLabel("Worst President", JLabel.TRAILING); L_Least_Favorite_President.setLocation( 5, 450 ); L_Least_Favorite_President.setSize( 140, 25 ); L_Least_Favorite_President.setForeground(Color.WHITE); bigPanel.add( L_Least_Favorite_President); L_Least_Favorite_President.setFont( new Font("Arial", 1, 12) ); L_First_Year_Voted = new JLabel("First Year Voted", JLabel.TRAILING); L_First_Year_Voted.setLocation( 5, 550 ); L_First_Year_Voted.setSize( 140, 25 ); L_First_Year_Voted.setForeground(Color.WHITE); bigPanel.add( L_First_Year_Voted); L_First_Year_Voted.setFont( new Font("Arial", 1, 12) ); L_City = new JLabel("City", JLabel.TRAILING); L_City.setLocation( 5, 250 ); L_City.setSize( 140, 25 ); L_City.setForeground(Color.WHITE); bigPanel.add( L_City); L_City.setFont( new Font("Arial", 1, 12) ); L_State = new JLabel("State", JLabel.TRAILING); L_State.setLocation( 5, 300 ); L_State.setSize( 140, 25 ); L_State.setForeground(Color.WHITE); bigPanel.add( L_State); L_State.setFont( new Font("Arial", 1, 12) ); L_Zip = new JLabel("Zip Code", JLabel.TRAILING); L_Zip.setLocation( 5, 350 ); L_Zip.setSize( 140, 25 ); L_Zip.setForeground(Color.WHITE); bigPanel.add( L_Zip); L_Zip.setFont( new Font("Arial", 1, 12) ); holdbuttons = new JPanel(); holdbuttons.setBackground(Color.BLACK); holdbuttons.setOpaque(true); submitlistener = new SubmitButtonHandler(); clearlistener = new ClearButtonHandler(); exitlistener = new ExitButtonHandler(); submit = new MyJButton(); submit.setPreferredSize(new Dimension(150,80)); submit.setText("Submit"); submit.addActionListener(submitlistener); holdbuttons.add(submit); clear = new MyJButton(); clear.setPreferredSize(new Dimension(150,80)); clear.setText("Clear"); clear.addActionListener(clearlistener); holdbuttons.add(clear); exit = new MyJButton(); exit.setPreferredSize(new Dimension(150,80)); exit.setText("Exit"); exit.addActionListener(exitlistener); holdbuttons.add(exit); /* When you create more buttons, you only need one line to add holdbuttons to the content pane. */ getContentPane().add(holdbuttons, BorderLayout.PAGE_END); top = new JLabel("Enter Contact Information and Submit", JLabel.CENTER); top.setBackground(Color.BLACK); top.setForeground(Color.WHITE); top.setFont(new Font("Arial", Font.BOLD+Font.ITALIC, 16)); top.setOpaque(true); getContentPane().add(top, BorderLayout.PAGE_START); scroller = new JScrollPane(bigPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroller.setVisible(true); getContentPane().add(scroller, BorderLayout.CENTER); }//end of constructor method /******************** Inner class *************************/ private class SubmitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { /* Your project should also have an Exit button and a Clear button. The Exit button clears all the input fields and makes RequestJFrame invisible. It does not process the input that the user has typed. The Exit button also reset the scrollbars. The Clear button clears all the input fields. It does not process the input, and it does not make RequestJFrame invisible. */ RequestJFrame.this.setVisible(false); //Make this instance of RequestJFrame invisible. /* The below try-catch blocks are required to make sure that there are no spaces in the user's input. The encode method replaces spaces with a + symbol. The encode method throws an exception, and for that reason the method must be used inside a try-catch block. Using one try-catch may risk losing input when the catch is invoked. I have never observed a failure in this replacement, however. You will need a try-catch for each field of input. */ firstName = firstNameInput.getText(); //Get the input from the JTestField and assign it to a String. try { //Replace all spaces in the String input with "+" firstName = URLEncoder.encode(firstName, "UTF-8"); } catch (UnsupportedEncodingException uee) { firstName ="NoInput"; } mI = miInput.getText(); try { mI = URLEncoder.encode(mI, "UTF-8"); } catch (UnsupportedEncodingException uee) { mI ="NoInput"; } lastName = lastNameInput.getText(); try { lastName = URLEncoder.encode(lastName, "UTF-8"); } catch (UnsupportedEncodingException uee) { lastName ="NoInput"; } address = addressInput.getText(); try { address = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { address ="NoInput"; } favoritePresident = favoritePresidentInput.getText(); try { favoritePresident = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { favoritePresident ="NoInput"; } politicalParty = politicalPartyInput.getText(); try { politicalParty = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { politicalParty ="NoInput"; } leastFavoritePresident = leastFavoritePresidentInput.getText(); try { leastFavoritePresident = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { leastFavoritePresident ="NoInput"; } firstYearVoted = firstYearVotedInput.getText(); try { firstYearVoted = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { firstYearVoted ="NoInput"; } city = cityInput.getText(); try { city = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { city ="NoInput"; } state = firstYearVotedInput.getText(); try { state = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { state ="NoInput"; } zip = firstYearVotedInput.getText(); try { zip = URLEncoder.encode(address, "UTF-8"); } catch (UnsupportedEncodingException uee) { zip ="NoInput"; } /* The below lines put together the query string of name-value pairs. You will need to add to this String for each JTextField object that you add to your interface. */ String toSend = "First_Name" + "=" + firstName + "&" + "MI" + "=" + mI + "&" + "Last_Name" + "=" + lastName + "&" + "Address" + "=" + address + "&" + "City" + "=" + city + "&" + "State" + "=" + state + "&" + "Zip" + "="+ zip + "&" + "Favorite_President" + "=" + favoritePresident + "&" + "Political_Party" + "=" + politicalParty + "&" + "Least_Favorite_President" + "=" + leastFavoritePresident + "&" + "First_Year_Voted" + "=" + firstYearVoted; sendIt(toSend); /* The below lines clear the text that was typed into the JTextField objects. */ firstNameInput.setText(null); miInput.setText(null); lastNameInput.setText(null); addressInput.setText(null); favoritePresidentInput.setText(null); politicalPartyInput.setText(null); leastFavoritePresidentInput.setText(null); firstYearVotedInput.setText(null); zipInput.setText(null); stateInput.setText(null); cityInput.setText(null); scroller.getVerticalScrollBar().setValue(0); //reset scroller.getHorizontalScrollBar().setValue(0); //reset submit.setPreferredSize(new Dimension(150,80)); clear.setPreferredSize(new Dimension(150,80)); exit.setPreferredSize(new Dimension(150,80)); myButton.setEnabled(true); }//end of actionPerformaed }/************ End of SubmitButtonHandler inner class **********/ private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { firstNameInput.setText(null); miInput.setText(null); lastNameInput.setText(null); addressInput.setText(null); favoritePresidentInput.setText(null); politicalPartyInput.setText(null); leastFavoritePresidentInput.setText(null); firstYearVotedInput.setText(null); zipInput.setText(null); stateInput.setText(null); cityInput.setText(null); submit.setPreferredSize(new Dimension(150,80)); clear.setPreferredSize(new Dimension(150,80)); exit.setPreferredSize(new Dimension(150,80)); RequestJFrame.this.setVisible(false); scroller.getVerticalScrollBar().setValue(0); //reset scroller.getHorizontalScrollBar().setValue(0); //reset myButton.setEnabled(true); } } private class ClearButtonHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { firstNameInput.setText(null); miInput.setText(null); lastNameInput.setText(null); addressInput.setText(null); favoritePresidentInput.setText(null); politicalPartyInput.setText(null); leastFavoritePresidentInput.setText(null); firstYearVotedInput.setText(null); zipInput.setText(null); stateInput.setText(null); cityInput.setText(null); submit.setPreferredSize(new Dimension(150,80)); clear.setPreferredSize(new Dimension(150,80)); exit.setPreferredSize(new Dimension(150,80)); scroller.getVerticalScrollBar().setValue(0); //reset scroller.getHorizontalScrollBar().setValue(0); //reset myButton.setEnabled(true); } } /* You will need two more inner classes, one for ClearButtonHandler and one for ExitButtonHandler. */ public void sendIt(String a) { /* The showDocument method sends the query string via the internet. */ try{ URL url = new URL(baseURL + a); myJApplet.getAppletContext().showDocument(url, "_blank"); } catch (MalformedURLException mue){} }//end of sendIt /*************** insert windowClosing method *****************/ public void windowClosing (WindowEvent we) { this.setVisible(false); firstNameInput.setText(null); miInput.setText(null); lastNameInput.setText(null); addressInput.setText(null); favoritePresidentInput.setText(null); politicalPartyInput.setText(null); leastFavoritePresidentInput.setText(null); firstYearVotedInput.setText(null); zipInput.setText(null); stateInput.setText(null); cityInput.setText(null); submit.setPreferredSize(new Dimension(150,80)); clear.setPreferredSize(new Dimension(150,80)); exit.setPreferredSize(new Dimension(150,80)); scroller.getVerticalScrollBar().setValue(0); //reset scroller.getHorizontalScrollBar().setValue(0); //reset myButton.setEnabled(true); } }//end of RequestJFrame