Link to home
Start Free TrialLog in
Avatar of Rowley4
Rowley4

asked on

Adding additional box/JFrame ComboBox

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

Avatar of Rowley4
Rowley4

ASKER

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?


ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial