Results 1 to 8 of 8
  1. #1
    Tombomb is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Question Adding my own object to a Container...not working.

    Okay, so this is my first post. Hello all!
    I am writing a program for my dad to use at his cabinet making business. The program is being designed to create the dimensions for cabinet doors. My problem is, on the second display I have a JComboBox I want to show up. I created a new class to use to instantiate this box, and now it doesn't show up because I can't add it to my Container. I tried changing my ComboBox.java to extend JComboBox so it would be considered a Component, buuuut that didn't work so well for me. So instead, the ComboBox.java is considered an object I made and not a component, therefore my container will not allow me to add it so it shows up. Any ideas and/or solutions would be wonderful! Thank You!

    PS. My ComboBox class compiles with no errors, the only error on my GUI class is that I can't add my own object.

    here is the portion of my GUI that I am trying to add the box to:

    Java Code:
    /*** ComboBox Being Created ***/
     
       public ComboBox createComboBox()
       {
       	boxPanel = new JPanel();
    		boxDoorType = new ComboBox( createDoubleDoorDisplay(),
    											 createSingleDoorOptions(),
    											 createDoubleDoorOptions() );
     
    		boxPanel.add( boxDoorType );
    		c.add( boxPanel );
    		return boxDoorType;
       }

    And here is my ComboBox class:

    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Font.*;
    import java.awt.Dimension.*;
     
     
     
    public class ComboBox implements ItemListener
    {
    	/*** Class Variables ***/
     
    	private String[] boxOptions  = new String[ 3 ];
    	private JPanel[] boxDisplays = new JPanel[ 3 ];
     
    	private JComboBox cmbxDoorType;
     
    	private JPanel createDoubleDoorDisplay;
    	private JPanel createSingleDoorOptions;
    	private JPanel createDoubleDoorOptions;
     
    	/*** Constructor ***/
    	public ComboBox( JPanel pCreateDoubleDoorDisplay,
    						  JPanel pCreateSingleDoorOptions,
    						  JPanel pCreateDoubleDoorOptions,
    						  String pBoxOption1,
    						  String pBoxOption2,
    						  String pBoxOption3)
    	{
     
    		createDoubleDoorDisplay = pCreateDoubleDoorDisplay;
    		createDoubleDoorDisplay.setVisible( false );
     
    		createSingleDoorOptions = pCreateSingleDoorOptions;
    		createSingleDoorOptions.setVisible( false );
     
    		createDoubleDoorOptions = pCreateDoubleDoorOptions;
    		createDoubleDoorOptions.setVisible( false );
    	}
     
    	/*** Accessors ***/
     
    	public String[] getBoxOptions()
    	{
    		return boxOptions;
    	}
     
    	public JPanel[] getBoxDisplays()
    	{
    		return boxDisplays;
    	}
     
    	/*** Mutators/Transformers ***/
     
    	public void setBoxOptions( String pBoxOptions, int pInt )
    	{
    		boxOptions[ pInt ] = pBoxOptions;
    	}
     
    	public void setBoxDisplays( JPanel pBoxDisplays, int pInt )
    	{
    		boxDisplays[ pInt ] = pBoxDisplays;
    		pBoxDisplays.setVisible( true );
    	}
     
    	public JComboBox createComboBox( String pBoxOption1,
    						  						String pBoxOption2,
    						  						String pBoxOption3 )
       {
       	setBoxOptions( pBoxOption1, 0 );
       	setBoxOptions( pBoxOption2, 1 );
       	setBoxOptions( pBoxOption3, 2 );
     
     
    		cmbxDoorType = new JComboBox( boxOptions );						                 // JComboBox is Deprecated.
    		cmbxDoorType.addItemListener( this );
     
       	return cmbxDoorType;
       }
     
    	public void itemStateChanged( ItemEvent e )
       {
       	if ( e.getStateChange() == ItemEvent.SELECTED )
       	{
    			if ( boxDisplays[ cmbxDoorType.getSelectedIndex() ].equals( 0 ) )
    			{
    				setBoxDisplays( createDoubleDoorDisplay, 0 );
    				createDoubleDoorDisplay.setVisible( true );
    			}
     
    			else if ( boxDisplays[ cmbxDoorType.getSelectedIndex() ].equals( 1 ) )
    			{
    				setBoxDisplays( createSingleDoorOptions, 1 );
    				createSingleDoorOptions.setVisible( true );
    			}
     
    			else if ( boxDisplays[ cmbxDoorType.getSelectedIndex() ].equals( 2 ) )
    			{
    				setBoxDisplays( createDoubleDoorOptions, 2 );
    				createDoubleDoorOptions.setVisible( true );
    			}
     
       	}
       }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Adding my own object to a Container...not working.


  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Adding my own object to a Container...not working.

    error on my GUI class
    Can you post the full text of the error message that you get?

  4. #4
    Tombomb is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Adding my own object to a Container...not working.

    Java Code:
    D:\Users\Tommy\Documents\My Code\Cabinet Door Project\ProjectGUI\src\ProjectGUI.java:131: error: constructor ComboBox in class ComboBox cannot be applied to given types;
    		boxDoorType = new ComboBox( createDoubleDoorDisplay(),
    		              ^
      required: JPanel,JPanel,JPanel,String,String,String
      found: JPanel,JPanel,JPanel
      reason: actual and formal argument lists differ in length
    D:\Users\Tommy\Documents\My Code\Cabinet Door Project\ProjectGUI\src\ProjectGUI.java:135: error: no suitable method found for add(ComboBox)
    		boxPanel.add( boxDoorType );
    		        ^
        method Container.add(Component,Object,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,Object) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component,int) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(String,Component) is not applicable
          (actual and formal argument lists differ in length)
        method Container.add(Component) is not applicable
          (actual argument ComboBox cannot be converted to Component by method invocation conversion)
        method Component.add(PopupMenu) is not applicable
          (actual argument ComboBox cannot be converted to PopupMenu by method invocation conversion)
    2 errors
    
    Process completed.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Adding my own object to a Container...not working.

    Check the constructors for the ComboBox class. Is there one that matches how you are trying to call it?
    What datatype is boxDoorType? The compiler can not find an override to the add() method that takes that datatype.

  6. #6
    Tombomb is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Adding my own object to a Container...not working.

    There is not a method from the JComboBox class that can add an object, only a component. the ComboBox class is a class that I made, which creates a JComboBox. boxDoorType is of dataType ComboBox ( The class I created ), A.K.A. an object, not a component which is what I think the problem is, but I can't figure out a solution lol.. =/

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Adding my own object to a Container...not working.

    Can you make your class to extend a class that the add() method will take?

  8. #8
    Tombomb is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Adding my own object to a Container...not working.

    Yes, that is what I ended up doing. I had tried that before I posted because I knew that would work, but I could not get the super() statement to work the way I wanted it to when it was extended, but finally managed to get it to go my way. :D Thank you!

Similar Threads

  1. Adding keyListener to a 2D object.
    By Guy in forum New To Java
    Replies: 2
    Last Post: 07-24-2011, 08:20 PM
  2. Replies: 3
    Last Post: 03-24-2011, 06:18 AM
  3. Adding multiple components to 1 container index
    By mmcnitt in forum AWT / Swing
    Replies: 3
    Last Post: 03-07-2011, 03:59 PM
  4. Help with adding a Container within a Container
    By JoKeR313 in forum New To Java
    Replies: 4
    Last Post: 01-02-2011, 04:59 AM
  5. Replies: 3
    Last Post: 04-21-2009, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •