Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Combo/TextField Help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Combo/TextField Help

    Hi, I've created a Swing program that uses content pane to display components. I have two problems - My zipCombo is supposed to be editable but it isn't and I need to know how to align my TextFields so they're even (at runtime). Any help would be greatly appreciated.

     
    public class PersonalData extends JFrame implements ActionListener
    {
    	        //Initialize Array
    	        String zipCode[] = {"38001", "38006", "38343", "38305", "38301"};
    	        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();
    	        	JPanel sixthRow = 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(20);
    	        	JLabel lastNameLabel = new JLabel("Last Name:");
    	            	     JTextField lastName= new JTextField(20);
    	        	JLabel addressLabel = new JLabel("Address:");
    	                          JTextField address = new JTextField(30);
    	        	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);
     
     
    	        	//Construct Button
    	        	JButton submitButton = new JButton("Submit");
     
    	    	//Create Content Pane
    	   	public void createContentPane()
    	   	{
    	    	        //Populate the JComboBox
    	       	        stateCombo.addActionListener(this);
    	       	        zipCombo.addActionListener(this);
    	        	        [B]zipCombo.setEditable(true);[/B]
    		}
     
    		public static void main(String args[])
    	        	{
    	            	     PersonalData f= new PersonalData();
    	            	     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	            	     f.setSize(500,400);
    	            	     f.setTitle("Personal Data");
    	            	     f.setResizable(false);
    	            	     f.setLocation(200,200);
    	            	     f.setVisible(true);
    	        	}
     
    	    	     public PersonalData()
    	    	     {
    	        	          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);
     
     
     
    	              	     //Add fields to rows
    	                	     firstRow.add(firstNameLabel);
    	                          firstRow.add(firstName);
     
    	                	     secondRow.add(lastNameLabel);
    	                	     secondRow.add(lastName);
     
    	                	     thirdRow.add(addressLabel);
    	                	     thirdRow.add(address);
     
    		     fourthRow.add(cityLabel);
    		     fourthRow.add(city);
    		     fourthRow.add(stateLabel);
    		     fourthRow.add(stateCombo);
    		     fourthRow.add(zipLabel);
    	                          fourthRow.add(zipCombo);
     
    	                	     fifthRow.add(submitButton);
     
    	                	      //Add rows to panel
    	                	      fieldPanel.add(firstRow);
    	                	      fieldPanel.add(secondRow);
    	                	      fieldPanel.add(thirdRow);
    	                           fieldPanel.add(fourthRow);
    	                	      fieldPanel.add(fifthRow);
    	                	      fieldPanel.add(sixthRow);
     
     
    		     //Add panels to frame
    	                	     c.add(fieldPanel, BorderLayout.CENTER);
     
     
    	                	      //Add functionality to button
    	        	      submitButton.addActionListener(this);
    	    	}
     
    	    	   public void actionPerformed(ActionEvent e)
    	    	   {
    	        	         //user clicks the combo box
    	        	         if (e.getSource() == stateCombo);
     
    	        	         if (e.getSource() == zipCombo);
    	    	   }
    }

  2. #2
    Junior Member
    Join Date
    Sep 2009
    Location
    Mount Morgan, Queensland, Australia
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Combo/TextField Help

    Place the line: zipCombo.setEditable(true); in the same area where you constructed the comboboxes, this will allow for editing. As for the other problem, use spacing after the labels. 1 after First Name, 1 after Last Name, 5 after Address and 14 after City.
    Last edited by TimW; October 25th, 2009 at 10:52 AM. Reason: Figured out problem

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Combo/TextField Help

    That helped. Thanks!! :-)

Similar Threads

  1. Loading contents of a Database into a Combo box
    By tyolu in forum JDBC & Databases
    Replies: 3
    Last Post: June 22nd, 2009, 09:14 AM