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 7 of 7

Thread: How can I put a JComboBox inside an array and display it?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How can I put a JComboBox inside an array and display it?

    I am writing a program for a college assignment, creating objects and storing them in an array, other options will be there too.
    I am trying to figure out how to use JComboBoxes, to give the user set options, instead of:
    Please select an option:
    Press 1 for x
    Press 2 for y
    ________


    How can I go about having a JComboBox put into the message [] and then display it? Other windows displayed to the user would have probably 2 or 3 JComboBoxes, so if I can get it sorted for the one, I can for the others....I hope.
    public void menuArmourOrWeapon()
    	{
    		int option = 0;
    		String msgInfo = new String("Please choose which type of item you wish to add.");
    		String msgArmour = new String("1. Armour");
    		String msgWeapon = new String("2. Weapon");
    		JTextField input = new JTextField("");
     
    		Object message[] = new Object[5];
     
    		message[0] = myIcon;
    		message[1] = msgInfo;
    		message[2] = msgArmour;
    		message[3] = msgWeapon;
    		message[4] = input;
     
    		int response = JOptionPane.showConfirmDialog(null, message, "Item Data Entry", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, anIcon);
     
    		    if(response == JOptionPane.CANCEL_OPTION)
    		    {
    		    }
    		    else
    		    {
    		    	try
    		    	{
    		    		option = Integer.parseInt(input.getText());
     
    		    			if(option == 1)
    		    			{
    		    				augment(option);
    		    			}
    		    			else if (option == 2)
    		    			{	
    		    				augment(option);
    		    			}
    		    	}
    		    	catch (Exception e)
    		    	{
    		    		JOptionPane.showMessageDialog(null, "Data Input Error" + e + "\nPlease Try Again");
    		    	}
    		    }
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,646
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: How can I put a JComboBox inside an array and display it?

    There are other versions of the showConfirmDialog() method that will show the contents of an array in a combo box format. Look at the API doc.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I put a JComboBox inside an array and display it?

    Thank you Norm. I have got that working.
    Now is there any way that I can have more than the one drop down box with this, as it will only take the one array for the options?


    Edit:
    The window I need to display would have
    String
    JTextField
    String
    JTextField
    JComboBox
    JComboBox
    JComboBox

    There would be an extra JComboBox, depending on previous selections. It might be easier to put each JComboBox on it's own, but if I can avoid that, it would be good.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,646
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: How can I put a JComboBox inside an array and display it?

    have more than the one drop down box
    The JOptionPane class's methods only return one value so it wouldn't support a dialog with more than one drop down.
    If you want a dialog window that a user can select more than one thing, you will need to write a custom dialog that gives the user the choices and saves the selections so the program can retrieve them.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I put a JComboBox inside an array and display it?

    Thank you again, Norm. So I found some code online to help me with that, so it seems I am almost sorted.
    Just one more problem. I am having trouble setting all the options for the drop down.
    Using
    labelItem.setText(this.selectionValues[0]);
    as below, I know is only setting the first element to labelItem. But if I try to use
    labelItem.setText(this.selectionValues.toString);
    I get
    Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
    I also had the memory location showing up as the only option in the drop down, but I haven't been able to get it again.

    So, how do I get my pesky array of Strings, to display in my drop down box?

    public Component getListCellRendererComponent(JList list, Object selectionValues, int index, boolean isSelected, boolean cellHasFocus) 
        {
            this.selectionValues = (String[]) selectionValues;
     
            labelItem.setText(this.selectionValues[0]);
     
            if (isSelected)
            {
                labelItem.setBackground(Color.BLUE);
                labelItem.setForeground(Color.YELLOW);
            }
            else 
            {
                labelItem.setForeground(Color.BLACK);
                labelItem.setBackground(Color.LIGHT_GRAY);
            }
     
            return this;
        }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,646
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: How can I put a JComboBox inside an array and display it?

    What statement gives that error?

    if toString is a method it needs an ending () to say so.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I put a JComboBox inside an array and display it?

    It was the toString without the () that was screwing me over.
    Thank you again.

Similar Threads

  1. JComboBox and array problems
    By tristannvk in forum Collections and Generics
    Replies: 6
    Last Post: December 23rd, 2012, 09:39 AM
  2. where to put the nodes into the array?
    By blaster in forum Algorithms & Recursion
    Replies: 9
    Last Post: March 11th, 2012, 07:03 PM
  3. Display a JTextField inside a JPanel
    By nivangerow in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 8th, 2012, 12:28 PM
  4. Not able to put some files inside Zip file
    By chinnu in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 23rd, 2011, 05:17 AM
  5. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM