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

Thread: Adding my own object to a Container...not working.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:

    /*** 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:

    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
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

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

    Also at java-forums.org

Similar Threads

  1. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  2. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  3. [Jade-Java]Distributed Container
    By ermasto in forum Algorithms & Recursion
    Replies: 0
    Last Post: January 25th, 2011, 05:08 AM
  4. Design Java Generic Container-please help!!!
    By zhoujackji in forum Collections and Generics
    Replies: 1
    Last Post: November 13th, 2010, 04:55 AM
  5. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 08:47 AM