import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; import java.util.*; public class UserData extends JFrame implements ActionListener { //initialize array String zipCode[] = {"96782", "96783", "96784", "96785", "96786"}; String states[] = {"AL", "AK","AZ","AR","CA","CO","CT","DE","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","MA","MD","MS","MI","MN","MI","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY"}; //construct a panel for each row JPanel firstRow = new JPanel(); JPanel secondRow = new JPanel(); JPanel thirdRow = new JPanel(); JPanel fourthRow = new JPanel(); JPanel fifthRow = new JPanel(); //construct components JComboBox zipCombo = new JComboBox(zipCode); JComboBox stateCombo = new JComboBox(states); //construct a panel for the fields and buttons JPanel fieldPanel = new JPanel(); //construct labels and text boxes JLabel firstNameLabel = new JLabel("First Name: "); JTextField firstName = new JTextField(10); JLabel lastNameLabel = new JLabel("Last Name: "); JTextField lastName= new JTextField(15); JLabel cityLabel = new JLabel("City: "); JTextField city = new JTextField(10); JLabel stateLabel = new JLabel("State: "); JTextField state = new JTextField(2); JLabel zipLabel = new JLabel("Zip:"); JTextField zip = new JTextField(9); //create the content pane public void createContentPane() { //populate the JComboBox stateCombo.addActionListener(this); zipCombo.addActionListener(this); zipCombo.setEditable(true); } public static void main(String args[]) { UserData f= new UserData(); f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); f.setSize(350,200); f.setTitle("User Information"); f.setResizable(false); f.setLocation(200,200); f.setVisible(true); } public UserData() { Container c = getContentPane(); c.setLayout((new BorderLayout())); fieldPanel.setLayout(new GridLayout(8,1)); FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,0); firstRow.setLayout(rowSetup); secondRow.setLayout(rowSetup); thirdRow.setLayout(rowSetup); fourthRow.setLayout(rowSetup); fifthRow.setLayout(rowSetup); JButton submitButton = new JButton("Submit"); // buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); //add fields to rows firstRow.add(firstNameLabel); firstRow.add(lastNameLabel); secondRow.add(firstName); secondRow.add(lastName); thirdRow.add(cityLabel); thirdRow.add(stateLabel); thirdRow.add(zipLabel); fourthRow.add(city); fourthRow.add(stateCombo); fourthRow.add(zip); //add rows to panel fieldPanel.add(firstRow); fieldPanel.add(secondRow); fieldPanel.add(thirdRow); fieldPanel.add(fourthRow); fieldPanel.add(fifthRow); //add button to panel fifthRow.add(submitButton); //buttonPanel.add(submitButton); //add panels to frame c.add(fieldPanel, BorderLayout.CENTER); //c.add(buttonPanel, BorderLayout.SOUTH); //add functionality to buttons //submitButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { //user clicks the combo box if (e.getSource() == stateCombo); if (e.getSource() == zipCombo); if (e.getSource() == submitButton) { } } }
JCOMBOBOXCan't get submit button to work
Page 1 of 1
1 Replies - 1098 Views - Last Post: 27 October 2007 - 03:37 AM
#1
JCOMBOBOX
Posted 26 October 2007 - 05:55 PM
can anyone please tell me why my submit button doesn't work. I've been playing around with it for a while. Without the submit button info, I can compile correctly but I need to have it as part of the program. Thanks in advance.
Replies To: JCOMBOBOX
#2
Re: JCOMBOBOX
Posted 27 October 2007 - 03:37 AM
Cut the line where you declare submitButton and paste it before the method createContentPane wherever you like
Page 1 of 1