Solved

Adding additional box/JFrame ComboBox

Posted on 2010-11-19
2
548 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
2 Comments
 

Author Comment

by:Rowley4
Comment Utility
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
Comment Utility
use a BorderLayout for your panel


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

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now