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: JComboBox won't add array of numbers

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default JComboBox won't add array of numbers

    I'm creating a JFrame that allows the user to change the font and font size of a piece of text by using 2 JCheckBoxes. The first JCheckBox gives the user every font available and it works fine. The second JCheckBox is where I'm having problems. It should have an array of numbers 1-100 so the user can change the size of font. I have posted the error I am getting below:

    Error:
    SetFont.java:32: cannot find symbol
    symbol : method add(int[])
    location: class javax.swing.JComboBox
    jcbFonts.add(sizes);
    ^
    1 error

    import javax.swing.*;
    import java.awt.*;
    import java.awt.GraphicsEnvironment;
     
    public class SetFont extends JFrame 
    {
       private JPanel p1, p2;
       private JLabel jlblFontName, jlblFontSize;
       private JComboBox jcbFonts, jcbSizes;
       private JTextArea jtxtWelcome;
       private JCheckBox  jckbBold, jckbItalic;
     
       public SetFont()                                                                // Default Constructor
    	{	
    		p1 = new JPanel();
    		jlblFontName = new JLabel("Font Name");
     
    		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();   // create instance of java.awt.GraphicsEnvironment()
    		String[] fontNames = e.getAvailableFontFamilyNames();								  // get all available fonts for user
    		jcbFonts = new JComboBox(fontNames);
     
    		jlblFontSize = new JLabel("Font Size");
     
    		int[] sizes = new int[100];													  		  	  // populate array with font sizes 1-100
    		for(int i = 1; i < 100; i ++)
    		{
        		jcbFonts.add(sizes);
    		}
     
    		p1.add(jlblFontName);																		  // add all 4 components to p1
    		p1.add(jcbFonts);
    		p1.add(jlblFontSize);
    		p1.add(jcbSizes);
    		add(p1,BorderLayout.NORTH);
     
    		add(jtxtWelcome = new JTextArea("Welcome to Java"));
    		jtxtWelcome.setSize(3, 20);
     
    		p2 = new JPanel();
    		p2.add(jckbBold = new JCheckBox("Bold"));
    		jckbBold.setMnemonic('B');
    		p2.add(jckbItalic = new JCheckBox("Italic"));
    		jckbItalic.setMnemonic('I');
     
    		add(p2,BorderLayout.SOUTH);
    	}
     
       public static void main(String[] args) 
       {
    	   SetFont frame = new SetFont();
    	   frame.setTitle("Set Font Details");
    	   frame.pack();
    	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   frame.setLocationRelativeTo(null);	 
    	   frame.setVisible(true);
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,941
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: JComboBox won't add array of numbers

    The error messages says there is no add() method that takes an int array.
    Does the JComboBox class have an add() method? What args does it take?
    Look at the API doc: Java Platform SE 7

    Look at how the code creates a JComboBox for the font names.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox won't add array of numbers

    I have found and added the addItem() method to get this:

    for(int i = 1; i < 100; i ++)
    {
       jcbFonts.addItem(sizes);
    }

    It is compiling but I am getting this error when I run it:

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1045)
    at java.awt.Container.add(Container.java:365)
    at SetFont.<init>(SetFont.java:38) <-- this line is highlighted in the error
    at SetFont.main(SetFont.java:55)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,941
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: JComboBox won't add array of numbers

    What variable has a null value at line 38? Find the variable and then find why it is null.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox won't add array of numbers

    I have added:
    jcbSizes = new JComboBox();

    This has taken care of the error in main I was getting.
    I have also changed the for loop to:
    for(int i = 1; i < 100; i ++)
    {
    jcbSizes.addItem(sizes);
    }

    The only problem I have now is that instead of adding an array of 1-100, the JCheckbox has added 100 identical lines of hexidecimal code, as shown below:

    jcheckbox.JPG

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,941
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: JComboBox won't add array of numbers

    What is sizes? If you want int values, why not add i?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    mwardjava92 (March 29th, 2013)

  8. #7
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox won't add array of numbers

    I took your advice and added 'i', it works now. Thank you.

Similar Threads

  1. Replies: 4
    Last Post: February 10th, 2013, 11:58 PM
  2. Add extended JPanel on selected index change of JComboBox
    By mikejr76 in forum AWT / Java Swing
    Replies: 3
    Last Post: February 20th, 2012, 10:32 PM
  3. Components I add to JApplet won't appear!
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2012, 11:47 AM
  4. My java program won't add up the numbers?
    By meaganicole in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 5th, 2012, 12:02 PM
  5. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 10
    Last Post: March 31st, 2011, 06:18 AM