Solved

setBackground Color to all components of a class ( class lesson)

Posted on 2013-11-24
2
318 Views
Last Modified: 2013-11-24
Hi,

This should be pretty easy, but I can't get it to work. I'm in a Java Programming course and part of a homework assignment is to set the background component's colors.  We are suppose to use class Component's setBackground method to change's it colors (it add colors to each component of the class).

I've tried just about every combination I can find, but no success. I get an error on each one of the below.

component.setBackground(Color.MAGENTA);
Component.setBackground(Color.MAGENTA);
components.setBackground(Color.MAGENTA);
Components.setBackground(Color.MAGENTA);


It is part of my while statement

while (componentIterator.hasNext() )
      {
          Component elementComponent = (Component) componentIterator.next();
          
          Component.setBackground(Color.MAGENTA);
          
          
          
      }// end of while statemnet

Open in new window


The lesson is on ArrayList and Iterators.

Thanks for any help. I am just commenting it out, so I can finish with my lesson until I figure it out.


Full Code:

package componentscollection;
// Exercise 19.13 ComponentsCollection.java
// This application lists all components used in the application.
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class ComponentsCollection extends JFrame
{
   // JLabel and JTextField for reading name from user
   private JLabel nameJLabel;
   private JTextField nameJTextField;

   // JLabel and JComboBox for displaying series name
   private JLabel bookJLabel;
   private JComboBox bookJComboBox;

   // two JLabels for displaying book cover
   private JLabel pictureJLabel;
   private JLabel bookPictureJLabel;

   // JLabel and JList for displaying list of components
   private JLabel listComponentsJLabel;
   private JList listComponentsJList;

   // JButton to allow user interaction
   private JButton submitJButton;

   // instance variables
   private String[] bookList = { "Simply Series",
      "How To Program Series", "Developer Series" };
   private ArrayList componentsArrayList = new ArrayList();
   private Component[] components = new Component[ 9 ];
   private ArrayList outputArrayList = new ArrayList();
   
   // no-argument constructor
   public ComponentsCollection()
   {
      createUserInterface();
   }
   
   // create and position GUI components; register event handlers
   private void createUserInterface()
   {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components 
      contentPane.setLayout( null );
      
      // set up nameJLabel
      nameJLabel = new JLabel();
      nameJLabel.setBounds( 16, 15, 48, 21 );
      nameJLabel.setText( "Name:" );
      nameJLabel.setName( "nameJLabel" );
      nameJLabel.setOpaque( true );
      contentPane.add( nameJLabel );

      // set up nameJTextField
      nameJTextField = new JTextField();
      nameJTextField.setBounds( 64, 16, 172, 20 );
      nameJTextField.setName( "nameJTextField" );
      contentPane.add( nameJTextField );

      // set up bookJLabel
      bookJLabel = new JLabel();
      bookJLabel.setBounds( 16, 48, 48, 21 );
      bookJLabel.setText( "Book:" );
      bookJLabel.setName( "bookJLabel" );
      bookJLabel.setOpaque( true );
      contentPane.add( bookJLabel );

      // set up bookJComboBox
      bookJComboBox = new JComboBox( bookList );
      bookJComboBox.setBounds( 64, 48, 172, 21 );
      bookJComboBox.setName( "bookJComboBox" );
      contentPane.add( bookJComboBox );

      // set up pictureJLabel
      pictureJLabel = new JLabel();
      pictureJLabel.setBounds( 16, 80, 48, 21 );
      pictureJLabel.setText( "Picture:" );
      pictureJLabel.setName( "pictureJLabel" );
      pictureJLabel.setOpaque( true );
      contentPane.add( pictureJLabel );

      // set up bookPictureJLabel
      bookPictureJLabel = new JLabel();
      bookPictureJLabel.setBounds( 64, 80, 91, 119 );
      bookPictureJLabel.setIcon( new ImageIcon(
         "vbnetFEP1_thumb.jpg" ) );
      bookPictureJLabel.setName( "bookPictureJLabel" );
      contentPane.add( bookPictureJLabel );

      // set up listComponentsJLabel
      listComponentsJLabel = new JLabel();
      listComponentsJLabel.setBounds( 252, 16, 112, 21 );
      listComponentsJLabel.setText( "List of components:" );
      listComponentsJLabel.setName( "listComponentsJLabel" );
      listComponentsJLabel.setOpaque( true );
      contentPane.add( listComponentsJLabel );

      // set up listComponentsJList
      listComponentsJList = new JList();
      listComponentsJList.setBounds( 252, 40, 130, 168 );
      listComponentsJList.setName( "listComponentsJList" );
      contentPane.add( listComponentsJList );

      // set up submitJButton
      submitJButton = new JButton();
      submitJButton.setBounds( 296, 224, 76, 23 );
      submitJButton.setText( "Submit" );
      submitJButton.setName( "submitJButton" );
      contentPane.add( submitJButton );
      submitJButton.addActionListener(
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when submitJButton is pressed
            public void actionPerformed( ActionEvent event )
            {
               submitJButtonActionPerformed( event );
            }
            
         } // end anonymous inner class

      ); // end addActionListener
      
      // put all components in an array
      components = getContentPane().getComponents();

      // add all components to componentsArrayList
      for ( int i = 0; i < 9; i++ )
      {
         componentsArrayList.add( components[ i ] );
      }

      // set properties of application's window
      setTitle( "Components Collection" ); // set title bar string
      setSize( 406, 288 );                 // set window size
      setVisible( true );                  // display window
      
   } // end method createUserInterface
   
   // method to display a list of components
   private void submitJButtonActionPerformed( ActionEvent event )
   {
      outputArrayList.clear();
      
      Iterator componentIterator = componentsArrayList.iterator();
      
      // step e 
      while (componentIterator.hasNext() )
      {
          Component elementComponent = (Component) componentIterator.next();
          
          Component.setBackground(Color.MAGENTA);
          
          
          
      }// end of while statemnet
      
   } // end submitJButtonActionPerformed
   
   // main method
   public static void main( String args[] )
   {
      ComponentsCollection application = new ComponentsCollection();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } // end method main
   
} // end class ComponentsCollection

/**************************************************************************
 * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

Open in new window

0
Comment
Question by:bvanscoy678
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
bvanscoy678 earned 0 total points
ID: 39672852
Maybe I should use my variable?

  while (componentIterator.hasNext() )
      {
          Component elementComponent = (Component) componentIterator.next();
         
         elementComponent.setBackground(Color.MAGENTA);
         
         
         
      }// end of while statemnet
0
 

Author Closing Comment

by:bvanscoy678
ID: 39672889
I did a good job!

Just kidding. I'm not sure how to delete the questions, since I answered it myself.

Thanks, Brent
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running Java Servlet inside Eclipse Luna 6 168
how to hide hide popup page refresh second time? 10 161
thymeleaf natural templating vs JSP 2 162
Unhandled exception type Exception 18 52
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Read our guide on how to survive being on-call.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question