Solved

Adding additional box/JFrame ComboBox

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 82
swing controls 2 35
batch vs regular insert in spring DAO 2 47
What is the use of Forwarding Class in java 1 51
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

738 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