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:
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:
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 );
}
}
}
}
Re: Adding my own object to a Container...not working.
Re: Adding my own object to a Container...not working.
Quote:
error on my GUI class
Can you post the full text of the error message that you get?
Re: Adding my own object to a Container...not working.
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.
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.
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.. =/
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?
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!