2 Replies - 1265 Views - Last Post: 27 March 2009 - 11:24 AM Rate Topic: -----

#1 antitru5t   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 19-January 09

Adding a selected Index from a JList box using the JButton. I'm so

Posted 25 March 2009 - 07:57 PM

Hi everyone,

I'm brutally stuck on JAVA.
My teacher has mentioned about retrieving from vectors. But, I seriously do not get it at all. His assignment is painstakingly hard.

I need to get my Movie Titles, number of copies. I want to implement (Combo Box), type of movie, DVD or Blu ray and Pricing when I press the JButton "Buy".

The main idea is that I want to get things to show up.
I have been using the getSelectedIndex() feature and I've been getting values like -1. Eek?!? :S

How am I supposed to use vectors to get getSelectedIndex()?

Here is my coding so far, if you want to look at it and try to fix it.

For a visual image of the assignment, here it is.

Visual Image of Assignment

MOVIEBUY.JAVA


///////////////////////////////////////// 
///			NATHAN CHAN			/// 
///		   ASSIGNMENT 3			/// 
///	  INSTRUCTOR: GILBERT TSUI	 /// 
///		 COURSE: CSIS 1275		 /// 
///		   SECTION:  01			/// 
///	 DUE DATE: MARCH 31st 2009	 /// 
/////////////////////////////////////////	  
  
// Please run this program at a 1280 x 1024 resolution. 
  
import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.lang.*; 
import java.util.*; 
import javax.swing.border.*; 
  
public class MovieBuy extends JApplet implements ActionListener, ListSelectionListener 
{ 
				ImageIcon imageClickForMovies = new ImageIcon("clickForMovies1.GIF"); 
				JButton buttonclickForMovies = new JButton(imageClickForMovies); 
		MovieList movieList = new MovieList(); 
				//String[] data01 = {"Speed Racer", "Star Trek XI", "The Dark Knight", "The X Files 2", "Hulk", "Iron Man", "Made of Honor"}; 
				Vector vtMovieTitles = new Vector(); 
				JList listbox01 = new JList(vtMovieTitles); 
				//String[] data02 = {"50", "3", "0", "34", "78", "0", "25"}; 
				Vector vtDVDStock = new Vector(); 
				JList listbox02 = new JList(vtDVDStock); 
				//String[] data03 = {"23", "0", "78", "5", "100", "0", "56"}; 
				Vector vtbluRayStock = new Vector(); 
				JList listbox03 = new JList(vtbluRayStock); 
				ButtonGroup DVDBluRayGroup = new ButtonGroup(); 
				JRadioButton radDVD = new JRadioButton("DVD", true); 
				JRadioButton radBluRay = new JRadioButton("blu Ray", false); 
				String[] copies = {"1 Copy(ies)", "2 Copy(ies)", "3 Copy(ies)", "4 Copy(ies)", "5 Copy(ies)", "6 Copy(ies)", "7 Copy(ies)"}; 
				JComboBox cmbCopies = new JComboBox(copies); 
				Label label1999 = new Label("$19.99"); 
				Label label3599 = new Label("$39.99"); 
				ImageIcon imageBuy = new ImageIcon("buyLogo1.GIF"); 
				JButton buttonBuy = new JButton(imageBuy); 
				DefaultListModel dataJListInput = new DefaultListModel(); // 
				JList listArea = new JList(dataJListInput); 
				JScrollPane listAreaScrollPane = new JScrollPane(listArea); 
				JButton btnClearButton = new JButton("Clear"); 
				JButton btnClearAll = new JButton("Clear All"); 
				Label lblTotal = new Label("Total:"); 
				Label lblEnterCreditCard = new Label("Enter Credit Card#:"); 
				JPasswordField passEnterCreditCard = new JPasswordField(); 
				JButton btnSubmit = new JButton("Submit"); 
				Label lblKeyPadPassword = new Label("Use Keypad to enter password for settings"); 
				JPasswordField passKeyPad = new JPasswordField(10); 
				JButton buttonA = new JButton("A"); 
				JButton buttonB = new JButton("B"); 
				JButton buttonC = new JButton("C"); 
				JButton buttonD = new JButton("D"); 
				JButton buttonE = new JButton("E"); 
				JButton buttonF = new JButton("F"); 
				JButton buttonG = new JButton("G"); 
				JButton buttonH = new JButton("H"); 
				JButton buttonI = new JButton("I"); 
				JButton buttonJ = new JButton("J"); 
				JButton buttonEnter = new JButton("Enter"); 
				JButton buttonreset = new JButton("Reset"); 
				// Panels 
				JPanel panel01 = new JPanel(new GridLayout(0,5,8,8)); 
				JPanel panel01a = new JPanel(); 
				JPanel panel01b = new JPanel(); 
				JPanel panel01c = new JPanel(); 
				JPanel panel01d = new JPanel(); 
				JPanel panel01e = new JPanel(new GridLayout(3,0)); 
				JPanel panel01eA = new JPanel(new GridLayout(0,2)); 
				JPanel panel01eB = new JPanel(new GridLayout(0,2)); 
				JPanel panel02 = new JPanel(new FlowLayout()); 
				JPanel panel03 = new JPanel(new GridLayout(0,2,8,8)); 
				JPanel panel03a = new JPanel(new GridLayout(1,0)); 
				JPanel panel03b = new JPanel(new GridLayout(5,0)); 
				JPanel panel03b1 = new JPanel(new GridLayout(0,2)); 
				JPanel panel04 = new JPanel(new FlowLayout());	
	  JPanel panel04a = new JPanel(new GridLayout(4,0));	
				JPanel panel04a1 = new JPanel(); 
	  JPanel panel04a2 = new JPanel(new GridLayout(2,5,3,3));	
	  JPanel panel04a3 = new JPanel(new FlowLayout(FlowLayout.CENTER));				  
				
				public void init() 
				{ 
						   movieList.createList(); // initialize movieList	
								setLayout(new GridLayout(4,1)); 
								add(panel01); 
										panel01.add(panel01a); 
										panel01.add(panel01b); 
										panel01.add(panel01c); 
										panel01.add(panel01d); 
										panel01.add(panel01e); 
								panel01b.add(listbox01); 
								panel01c.add(listbox02); 
								panel01d.add(listbox03); 
								listbox01.setFixedCellHeight(25); 
					  listbox01.setFixedCellWidth(240); 
					  listbox01.setVisibleRowCount(7); 
					  listbox01.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
								listbox01.addListSelectionListener(this); 
								listbox02.setForeground(Color.gray); 
								listbox02.setSelectionForeground(Color.gray); 
								listbox02.setSelectionBackground(Color.green); 
								listbox02.setFixedCellHeight(25); 
					  listbox02.setFixedCellWidth(240); 
					  listbox02.setVisibleRowCount(7); 
					  listbox02.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
								listbox02.addListSelectionListener(this); 
								listbox03.setForeground(Color.gray); 
								listbox03.setSelectionForeground(Color.gray); 
					  listbox03.setSelectionBackground(Color.yellow); 
								listbox03.setFixedCellHeight(25); 
					  listbox03.setFixedCellWidth(240); 
					  listbox03.setVisibleRowCount(7); 
					  listbox03.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);	
								listbox03.addListSelectionListener(this); 
								panel01b.setBorder(new TitledBorder("Movie Titles")); 
								panel01b.setBackground(Color.white); 
								panel01c.setBorder(new TitledBorder("DVD Stock")); 
								panel01c.setBackground(Color.white); 
								panel01d.setBorder(new TitledBorder("blu Ray Stock")); 
								panel01d.setBackground(Color.white); 
								add(panel02); 
								add(panel03); 
								add(panel04); 
								buttonclickForMovies.addActionListener(this); 
								buttonclickForMovies.setPreferredSize(new Dimension(255,225)); 
								panel01a.add(buttonclickForMovies); 
								panel01e.add(panel01eA); 
								panel01eA.add(radDVD); 
								panel01eA.add(label1999); 
								label1999.setFont(new Font("Arial", Font.BOLD, 12)); 
								label1999.setBackground(Color.green); 
								panel01e.add(panel01eB); 
								panel01eB.add(radBluRay); 
								panel01eB.add(label3599); 
								DVDBluRayGroup.add(radDVD); 
								DVDBluRayGroup.add(radBluRay); 
								label3599.setFont(new Font("Arial", Font.BOLD, 12)); 
								panel01e.add(cmbCopies); 
								label3599.setBackground(Color.yellow); 
								buttonBuy.setPreferredSize(new Dimension(315,225)); 
								buttonBuy.addActionListener(this); 
								panel02.add(buttonBuy,BorderLayout.CENTER); 
								panel03.add(panel03a); 
								panel03.add(panel03b); 
								Border blackline = BorderFactory.createLineBorder(Color.black);						  
								panel03a.setBorder(blackline); 
								panel03a.add(listAreaScrollPane); 
								panel03b.add(btnClearButton); 
								panel03b.add(btnClearAll); 
								panel03b.add(lblTotal); 
								lblTotal.setBackground(Color.cyan); 
								panel03b.add(panel03b1); 
								lblEnterCreditCard.setAlignment(Label.RIGHT); 
								lblEnterCreditCard.setFont(new Font("Arial", Font.BOLD, 12)); 
								panel03b1.add(lblEnterCreditCard); 
								panel03b1.add(passEnterCreditCard); 
								panel03b.add(btnSubmit); 
						   panel04.add(panel04a);	
			panel04a.setBackground(Color.red);	
			panel04a.setBorder(new TitledBorder("For Employee use ONLY"));	
			panel04a.setBackground(Color.red);	
			lblKeyPadPassword.setFont(new Font("Arial", Font.BOLD, 12));	
			panel04a.add(lblKeyPadPassword);	
						panel04a1.setBackground(Color.red);			  
								panel04a.add(panel04a1);	
								panel04a1.add(passKeyPad); 
			panel04a.add(panel04a2);	
			panel04a2.add(buttonA);	
			panel04a2.add(buttonB);	
			panel04a2.add(buttonC);	
			panel04a2.add(buttonD);	
			panel04a2.add(buttonE);	
			panel04a2.add(buttonF);	
			panel04a2.add(buttonG);	
			panel04a2.add(buttonH);	
			panel04a2.add(buttonI);	
			panel04a2.add(buttonJ);	
			panel04a.add(panel04a3);	
			panel04a3.setBackground(Color.red);	
			panel04a3.add(buttonEnter);	
			panel04a3.add(buttonreset);  
						
								} 
								
		   public void actionPerformed(ActionEvent e) 
				{ 
				   if(e.getSource() instanceof JButton) 
						{ 
								 if(e.getSource() == buttonclickForMovies) 
								  for(int i = 0; i < movieList.mList.length; i++) // add elements to vtMovieTitles 
										vtMovieTitles.addElement(movieList.mList[i].getMovieTitle()); 
												listbox01.setListData(vtMovieTitles); 
								  for(int i = 0; i < movieList.mList.length; i++)  // add elements to vtDVDStock	
												vtDVDStock.addElement(movieList.mList[i].getMovieStock('D')); 
												listbox02.setListData(vtDVDStock); 
								  for(int i = 0; i < movieList.mList.length; i++)  // add elements to vtblueRayStock 
									  vtbluRayStock.addElement(movieList.mList[i].getMovieStock('B')); 
												listbox03.setListData(vtbluRayStock); 
						 } 
				  
				  if(e.getSource() instanceof JButton) 
				  { 
					 if(e.getSource() == buttonBuy) 
						  { 
							 { 
							  // *********************** So, what should I write here.. can you give me some hints? :( 
								  } 
								  
						  }	
				  
				  } 
						  
			}	
						  
		  public void valueChanged(ListSelectionEvent e)	
		{ 
				  for(int i = 0; i < movieList.mList.length; i++) 
					 if(listbox01.getSelectedIndex() == i)	
						  { 
						  listbox02.setSelectedIndex(i); 
						  listbox03.setSelectedIndex(i); 
						  } 
				  } 
 } 





MOVIEINFO.JAVA

//For Assign3 Winter 2009	
  
class MovieInfo	
{	
  
  private String strMovieTitle;	
  private int intMovieDVD;	
  private int intMovieBlueRay;	
		
  private static double doubMovieDVDPrice = 19.99;	
  private static double doubMovieBlueRayPrice = 35.99;	
  
  MovieInfo(String title, int mDVD, int mBRay)	
  {	
	  
	 strMovieTitle = title;	
	  intMovieDVD = mDVD;	
	  intMovieBlueRay = mBRay;	
		  
  
  }	
  
  public String getMovieTitle()	
  {	
	return strMovieTitle;	
  }	
	  
	  
  public int getMovieStock(char which)	
  {	
	 if(which == 'D')	
		 {	
		   return intMovieDVD;	
		   }	
	  else  
		 {	
		   return intMovieBlueRay;	
		  }	
	}	  
	// you finish this	
	 // You can use "which" to determine which	
	 // type of movie to retrieve the stock for		  
	  
		
		
  public double getMoviePrice(char which)	
  {	
	 if(which == 'D')	
		  {	
			return doubMovieDVDPrice;	
			}	
	  else  
		  {	
			return doubMovieBlueRayPrice;	
			}	
	}	  
	 // you finish this	
	 // You can use "which" to determine which	
	 // type of movie to retrieve the stock for		  
	  
	  
  public void setMovieStock(char which, int amt)	
  {	
	 if(which == 'D')	
		 {	
		  intMovieDVD  = amt;	
		  }	
	  else	
		 {	
		  intMovieBlueRay = amt;	
		  }	
 }	  
		
	 // you finish this	
	 // You can use "which" to determine which	
	 // type of movie to set the stock for	  
		  
   public static void setMoviePrice(char which, double prce)	
	{	
	  if(which == 'D')	
		  {	
		   doubMovieDVDPrice = prce;	
			}	
		else	
		  {	
		   doubMovieBlueRayPrice = prce;	
		  }	
			  
	// you finish this	
	 // You can use "which" to determine which	
	 // type of movie to set the price for	
  }	
  
	  
	  
}	
  
///////////////////////////////	
  
class MovieList	
{	
  
  MovieInfo[] mList;	
	  
  public void createList()	
  {	
	  
	  mList = new MovieInfo[7];	
		mList[0] = new MovieInfo("Speed Racer",50,23);	
		mList[1] = new MovieInfo("Star Trek XI",3,0);	
		mList[2] = new MovieInfo("The Dark Knight",0,78);	
		mList[3] = new MovieInfo("The X Files 2",34,5);	
		mList[4] = new MovieInfo("Hulk",78,100);	
		mList[5] = new MovieInfo("Iron Man", 0,0);	
		mList[6] = new MovieInfo("Made of Honor",25,56);	
			
		MovieInfo.setMoviePrice('D', 19.99);	
		MovieInfo.setMoviePrice('B', 35.99);	
			
	////////////////////////////////////////////////	
	// IMPORTANT: PLEASE use "setMoviePrice()" method to	
	// set the PRICES FOR DVD OR Blu Ray here	
	//			19.99 FOR DVD	
	//			35.99 for Blu Ray	
	////////////////////////////////////////////////	
		
		
		  
  }	
  
} 






If you can take a look at the code and try to help me ASAP, that would be great. Thanks!

I have attempted this so far, which is not good, but I tried it anyways. It didn't work.

	  
	  else 
		  if(e.getSource() == buttonBuy)
			  {
				 {
				   Object firstSel = listbox01.getSelectedValue();  
					dataJListInput.addElement(firstSel);
				  }
				  
				  
				
		  
		  }





- Nathe

Is This A Good Question/Topic? 0
  • +

Replies To: Adding a selected Index from a JList box using the JButton. I'm so

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding a selected Index from a JList box using the JButton. I'm so

Posted 26 March 2009 - 06:02 PM

Don't really understand why you loop:

public void valueChanged(ListSelectionEvent e)	
{ 
		for(int i = 0; i < movieList.mList.length; i++) 
			if(listbox01.getSelectedIndex() == i)	
			{ 
				 listbox02.setSelectedIndex(i); 
				 listbox03.setSelectedIndex(i); 
			 } 
}



just do
int i = listbox01.getSelectedIndex() ;
listbox02.setSelectedIndex(i);
listbox03.setSelectedIndex(i);

Did you wrote that code or it was generated by your GUI generator ?
Was This Post Helpful? 0
  • +
  • -

#3 antitru5t   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 19-January 09

Re: Adding a selected Index from a JList box using the JButton. I'm so

Posted 27 March 2009 - 11:24 AM

I'm new to programming. I wrote it . . :blink:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1