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

Thread: Need to condense if-statements

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to condense if-statements

    Hey everyone! I'm working on my final program for my Intro to Java class for the semester. Essentially, the program I thought of is supposed to have a JLabel at the top saying "I can tell you the colors of certain fruits!", a JComboBox that contains the names of 5 fruits, and a JButton. When the user selects a fruit from the JComboBox and presses the button, a JLabel is added that says "<selected fruits> are <respective color>!". In addition to that, the JLabel that is added when the button is pressed is supposed to be set to the color of the fruit. THAT is where my problem lays. As of right now, I simply have five if-statements (one for each fruit) that determine which fruit is selected,and set the color using the setForeground() method. I would really like to condense these if-statements somehow, but I truly don't know how! I was thinking maybe I could create a container array that is parallel to the fruit and color arrays, but I don't even know if a container array is possible to make!

    Help is VERY much appreciated!

    Here is my code:

    import javax.swing.*;
    import java.awt.Color;
    import java.awt.*;
    import java.awt.event.*;
     
    //The class extends JFrame for GUI purposes, and implements ActionListener
    //so events can take place within the JFrame.
    public class FinalProject extends JFrame implements ActionListener
    {
    	//First, I declare GUI elements: the JLabel that lets the user know what the program does,
    	//my choice of font, the "Submit Fruit" button that the user will press
    	//once they have selected their fruit, and a place holder label that will eventually
    	//display a final message.
    	JLabel label = new JLabel("I can tell you the colors of certain fruits!");
    	Font font = new Font("Times New Roman", Font.BOLD, 20);
    	JButton submit = new JButton("Submit Fruit");
    	JLabel lastLabel = new JLabel(" ");
     
    	//The following two parallel arrays contain the selection of fruits and their respective colors.
    	String[] fruit = {"Apples", "Bananas",  "Blueberries", "Limes", "Oranges"};
    	String[] color = {"red", "yellow",  "blue", "green", "orange"};
     
    	//Now the JComboBox is created, and the fruit array is used as an argument so that the fruits
    	//will all appear in the drop down menu.  Also, constants that will be used for the width and height
    	//of the JFrame are also declared.
    	JComboBox fruitChoice = new JComboBox(fruit);
    	final int WIDTH = 425;
    	final int HEIGHT = 200;
    	Container con = getContentPane();
     
    	//Here is where the constructor is formed, and all necessary elements will be added.
    	public FinalProject()
    	{
    		super("Final Program");
    		//First, the size of the JFrame is set, default close operation is set to exit on close,
    		// the layout is set, and the font of the first label is set.
    		setSize(WIDTH, HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new FlowLayout());
    		label.setFont(font);
    		//Now the GUI components will be added, adding an action listener to the submit button
    		//so that it can trigger an event.
    		con.add(label);
    		con.add(fruitChoice);
    		con.add(submit);
    		submit.addActionListener(this);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		int index = fruitChoice.getSelectedIndex();
    		for(int x = 0; x < 5; x++)
    		{
    			if(x == index)
    			{
    				lastLabel.setText(fruit[x] + " are " + color[x]);
    				lastLabel.setFont(font);
     
    				if(x == 0)
    				{
    					lastLabel.setForeground(Color.RED);
    				}
    				if(x == 1)
    				{
    					lastLabel.setForeground(Color.YELLOW);
    				}
    				if(x == 2)
    				{
    					lastLabel.setForeground(Color.BLUE);
    				}
    				if(x == 3)
    				{
    					lastLabel.setForeground(Color.GREEN);
    				}
    				if(x == 4)
    				{
    					lastLabel.setForeground(Color.ORANGE);
    				}
     
    				con.add(lastLabel);
    			}
    		}
    		invalidate();
    		validate();
    	}
    	public static void main(String[] args)
    	{
    		FinalProject frame = new FinalProject();
    		frame.setVisible(true);
    	}
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to condense if-statements

    Sorry for the excessive comments by the way, kind of part of the assignment haha

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to condense if-statements

    Also, switch statements are out of the question because we haven't learned them in the class yet. We are supposed to stick to what we have learned in the book.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need to condense if-statements

    setForeGround() actually needs a Color object in order to process. So, instead of providing it with the Color final fields, why don't you try saving the RGB values of colors in a collection and in only single statement, you can just load the value from the matched index to the statement.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to condense if-statements

    You could put the Colors in an array and use the value of x to index that array.

Similar Threads

  1. Is Key Pressed statements
    By Yo Cas Cas in forum AWT / Java Swing
    Replies: 6
    Last Post: August 27th, 2011, 12:48 AM
  2. [SOLVED] Help with nested if statements.
    By joon in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 17th, 2011, 10:25 AM
  3. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  4. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM

Tags for this Thread