Solved

Adding additional box/JFrame ComboBox

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 32
maven module vs maven project 3 47
ejb message driven bean mdb creation steps 2 24
Cisco ASA: Java web start no go, asdm launcher no go 3 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

828 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