Solved

Adding additional box/JFrame ComboBox

Posted on 2010-11-19
2
558 Views
Last Modified: 2012-05-10
I need to enhance this program by adding an additional box that appears from the selection the user makes in the drop down menu. When a user selects a specific class form the menu an additional box will appear with the explanation of the class in it.  Here is what I have so far.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ComboBox{
	  String course[] = {"Menu","CIS 212","CIS 222","CIS 406","CIS 407","CIS 326","CIS 328","CIS 493"};
	  String desc[] = {	"Please choose a class from the menu",
			  			"System Modeling Theory",
			  			"Computer Ethics:",
			  			"Java Programming I",
	  					"Java Programming II",
	  					"Object Oriented Programming I",
	  					"Object Oriented Programming II",
	  					"Creating Web Databasese"};
	  JComboBox combo;
	  JLabel txt;

	  public static void main(String[] args) {
			ComboBox b = new ComboBox();
	  }

	  public ComboBox(){
			JFrame frame = new JFrame("Creating a JComboBox Component");
			JPanel panel = new JPanel();
			JPanel panel2 = new JPanel();
			combo = new JComboBox(course);
			combo.setBackground(Color.white);
			combo.setForeground(Color.blue);

			txt = new JLabel("Please choose a class from the menu.");
			panel.add(txt);
			frame.add(panel, BorderLayout.NORTH);

			panel2.add(combo);
			frame.add(panel2, BorderLayout.CENTER);

			combo.addItemListener(new ItemListener(){
				  public void itemStateChanged(ItemEvent ie){
						String str = (String)combo.getSelectedItem();
						int index = combo.getSelectedIndex();
						txt.setText(desc[index]);
				  }
			});
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setSize(400,400);
			frame.setVisible(true);
	  }
}

Open in new window

0
Comment
Question by:Rowley4
[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 Comments
 

Author Comment

by:Rowley4
ID: 34174904
Instead of adding a box. How would I add text such as: "This course focuses on ethical and legal issues, civil rights, and privacy considerations that organizations must take into account." To show up when the user hits CIS212? I add it to: String desc[] = {      "Create Web Database", but the text is too long and it wont wrap it. Can you tell me how to get this to wrap the text or pull a second box with this explanation on the bottom or under the pull down menu?


0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34177383
use a BorderLayout for your panel


                  JPanel panel = new JPanel(new BorderLayout());

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

726 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