Help With .LastElement & .FirstElement

The same result is show regardless!

Page 1 of 1

9 Replies - 868 Views - Last Post: 05 May 2009 - 05:14 AM Rate Topic: -----

#1 robert_tonnessen@hotmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-November 08

Help With .LastElement & .FirstElement

Post icon  Posted 03 May 2009 - 03:35 PM

Hi all, I cant figure this out.....

I have written the following code based around a rental system for a video shop. The problem I am having is that the .LastElement shows the correct results i.e the last element. but the .FirstElement also shows the last element!?!?!

any tips?

private void cmdVectorFirstActionPerformed(java.awt.event.ActionEvent evt) {											   
try {
	   vectordb.firstElement();
	   txtClassification.setText(record.getClassification());
	   txtTitle.setText(record.getTitle());
	   txtDuration.setText(Integer.toString(record.getDuration()));
	   txtHireCost.setText(Float.toString(record.getHireCost()));

}
catch (Exception e){
	JOptionPane.showMessageDialog(this, "File Does Not Exist!", "Error", JOptionPane.ERROR_MESSAGE);
	
}
 
cmdAdd.setEnabled(true);
}											  

private void cmdVectorLastActionPerformed(java.awt.event.ActionEvent evt) {											  
try {
	  vectordb.lastElement();
	   txtClassification.setText(record.getClassification());
	   txtTitle.setText(record.getTitle());
	   txtDuration.setText(Integer.toString(record.getDuration()));
	   txtHireCost.setText(Float.toString(record.getHireCost()));

}
catch (Exception e){
	JOptionPane.showMessageDialog(this, "File Does Not Exist!", "Error", JOptionPane.ERROR_MESSAGE);
	
}

cmdAdd.setEnabled(true);// TODO add your handling code here:
} 



Cheers Chaps.



Is This A Good Question/Topic? 0
  • +

#3 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help With .LastElement & .FirstElement

Posted 03 May 2009 - 03:42 PM

assuming that vectorb is a Vector (you didn't post a lot of code)

vectorb.firstElement()
vectorb.lastElement()

returns, as their name say, the first and the last entry in the Vector
but as you don't store anywhere ... Object o = vectorb.firstElement();
your two methods are functionaly identical... do exactly the same thing
so no wonder same result
Was This Post Helpful? 1
  • +
  • -

#4 robert_tonnessen@hotmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-November 08

Re: Help With .LastElement & .FirstElement

Posted 03 May 2009 - 03:53 PM

Thanks for the Quick Reply, Here is more code for you to look over, please note that this is running two types of "system" the first is working with a file and I am slowly moving over to a vector so you will spot some duplication!

package video;
import java.io.*;
import java.util.Vector;
import javax.swing.JOptionPane;
/**
 *
 * @author  00c0
 */
public class frmVideo extends javax.swing.JFrame {
ObjectOutputStream output;
   ObjectInputStream input;
   videoRecord record;
   Vector <videoRecord> vectordb = new Vector <videoRecord>();
   File file = new File(new File(getClass().getResource("dataFiles").getFile()),"video.txt");

	/** Creates new form frmVideo */
	public frmVideo() {
		super ("The Video Shop");
		openData();
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		lblclassification = new javax.swing.JLabel();
		lbltitle = new javax.swing.JLabel();
		lblduration = new javax.swing.JLabel();
		lblhirecost = new javax.swing.JLabel();
		cmdNew = new javax.swing.JButton();
		cmdNext = new javax.swing.JButton();
		cmdAdd = new javax.swing.JButton();
		cmdFirst = new javax.swing.JButton();
		txtDuration = new javax.swing.JTextField();
		txtTitle = new javax.swing.JTextField();
		txtClassification = new javax.swing.JTextField();
		txtHireCost = new javax.swing.JTextField();
		cmdSearch = new javax.swing.JButton();
		txtSearch = new javax.swing.JTextField();
		lblsearch = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		lblLogo = new javax.swing.JLabel();
		jPanel3 = new javax.swing.JPanel();
		cmdOpen = new javax.swing.JButton();
		cmdVectorAdd = new javax.swing.JButton();
		cmdVectorFirst = new javax.swing.JButton();
		cmdVectorLast = new javax.swing.JButton();
		cmdNumberOfRecords = new javax.swing.JButton();
		txtRecordCount = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		lblclassification.setText("Classification");

		lbltitle.setText("Title");

		lblduration.setText("Duration");

		lblhirecost.setText("Hire Cost");

		cmdNew.setText("NEW");
		cmdNew.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdNewActionPerformed(evt);
			}
		});

		cmdNext.setText("NEXT");
		cmdNext.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdNextActionPerformed(evt);
			}
		});

		cmdAdd.setText("ADD");
		cmdAdd.setEnabled(false);
		cmdAdd.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdAddActionPerformed(evt);
			}
		});

		cmdFirst.setText("FIRST");
		cmdFirst.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdFirstActionPerformed(evt);
			}
		});

		txtDuration.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				txtDurationActionPerformed(evt);
			}
		});

		cmdSearch.setText("Search");
		cmdSearch.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdSearchActionPerformed(evt);
			}
		});

		lblsearch.setText("Enter Title To Find");

		jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

		lblLogo.setFont(new java.awt.Font("Jokerman", 0, 24));
		lblLogo.setForeground(new java.awt.Color(51, 51, 255));
		lblLogo.setText("Rob's Pirate Video Shop");

		jPanel3.setBackground(new java.awt.Color(51, 51, 255));
		jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

		cmdOpen.setBackground(new java.awt.Color(204, 255, 204));
		cmdOpen.setText("Open");
		cmdOpen.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdOpenActionPerformed(evt);
			}
		});

		cmdVectorAdd.setBackground(new java.awt.Color(204, 255, 204));
		cmdVectorAdd.setText("ADD");
		cmdVectorAdd.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdVectorAddActionPerformed(evt);
			}
		});

		cmdVectorFirst.setBackground(new java.awt.Color(204, 255, 204));
		cmdVectorFirst.setText("First");
		cmdVectorFirst.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdVectorFirstActionPerformed(evt);
			}
		});

		cmdVectorLast.setBackground(new java.awt.Color(204, 255, 204));
		cmdVectorLast.setText("Last");
		cmdVectorLast.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdVectorLastActionPerformed(evt);
			}
		});

		cmdNumberOfRecords.setText("Number Of Records?");
		cmdNumberOfRecords.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdNumberOfRecordsActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout.setHorizontalGroup(
			jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(jPanel3Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(jPanel3Layout.createSequentialGroup()
						.addComponent(cmdOpen)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(cmdVectorAdd)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(cmdVectorFirst)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(cmdVectorLast))
					.addGroup(jPanel3Layout.createSequentialGroup()
						.addComponent(cmdNumberOfRecords)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(txtRecordCount, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(84, Short.MAX_VALUE))
		);
		jPanel3Layout.setVerticalGroup(
			jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(jPanel3Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(cmdOpen)
					.addComponent(cmdVectorAdd)
					.addComponent(cmdVectorFirst)
					.addComponent(cmdVectorLast))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
				.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(cmdNumberOfRecords)
					.addComponent(txtRecordCount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
		);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(jPanel1Layout.createSequentialGroup()
				.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
							.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
								.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
									.addComponent(lbltitle)
									.addComponent(lblclassification)
									.addComponent(lblduration)
									.addComponent(lblhirecost)
									.addComponent(lblsearch))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
									.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
										.addComponent(txtHireCost)
										.addComponent(txtDuration)
										.addComponent(txtClassification, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
									.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
										.addComponent(txtSearch, javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(txtTitle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)))
								.addGap(79, 79, 79)
								.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
									.addComponent(cmdSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(cmdFirst, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(cmdAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(cmdNew, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(cmdNext, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
						.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
					.addGroup(jPanel1Layout.createSequentialGroup()
						.addGap(33, 33, 33)
						.addComponent(lblLogo)))
				.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		jPanel1Layout.setVerticalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(jPanel1Layout.createSequentialGroup()
				.addGap(7, 7, 7)
				.addComponent(lblLogo)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(lbltitle)
					.addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(cmdNew))
				.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(jPanel1Layout.createSequentialGroup()
						.addGap(6, 6, 6)
						.addComponent(cmdAdd)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addGroup(jPanel1Layout.createSequentialGroup()
								.addGap(82, 82, 82)
								.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
									.addComponent(lblsearch)
									.addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
							.addGroup(jPanel1Layout.createSequentialGroup()
								.addGap(11, 11, 11)
								.addComponent(cmdFirst)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(cmdNext)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
								.addComponent(cmdSearch)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
					.addGroup(jPanel1Layout.createSequentialGroup()
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
							.addComponent(txtClassification, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
							.addComponent(lblclassification))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
							.addComponent(txtDuration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
							.addComponent(lblduration))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
							.addComponent(lblhirecost)
							.addComponent(txtHireCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
				.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(jPanel1Layout.createSequentialGroup()
						.addGap(43, 43, 43)
						.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
					.addGroup(jPanel1Layout.createSequentialGroup()
						.addGap(18, 18, 18)
						.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
				.addGap(39, 39, 39))
		);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);

		pack();
	}// </editor-fold>

private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {									   

		
   try{
	   record = new videoRecord(
	   				txtClassification.getText(),
	   				txtTitle.getText(),
	   				Integer.parseInt(txtDuration.getText()), 
	   				Float.parseFloat(txtHireCost.getText()));
	   output.writeObject(record);
   }

catch (IOException e){
	JOptionPane.showMessageDialog(this, "Something wrong!", "Error", JOptionPane.ERROR_MESSAGE);
}
boxClear();
}									  

private void cmdNextActionPerformed(java.awt.event.ActionEvent evt) {										
videoRecord record;
	try {
	   
	   record = (videoRecord)input.readObject();
	   txtClassification.setText(record.getClassification());
	   txtTitle.setText(record.getTitle());
	   txtDuration.setText(Integer.toString(record.getDuration()));
	   txtHireCost.setText(Float.toString(record.getHireCost()));

}
catch (IOException e){
	JOptionPane.showMessageDialog(this, "End Of File!", "Error", JOptionPane.INFORMATION_MESSAGE);

}
  catch (ClassNotFoundException c){
   JOptionPane.showMessageDialog(this, "Something wrong!", "Error", JOptionPane.ERROR_MESSAGE);

  }   // TODO add your handling code here:
}									   

private void cmdNewActionPerformed(java.awt.event.ActionEvent evt) {									   
try {
  output = new ObjectOutputStream(new FileOutputStream(file));
}
catch (IOException e){
	JOptionPane.showMessageDialog(this, "Something wrong!", "Error", JOptionPane.ERROR_MESSAGE);
	cmdAdd.setEnabled(false);
}
cmdAdd.setEnabled(true); 
// TODO add your handling code here:
}									  

private void txtDurationActionPerformed(java.awt.event.ActionEvent evt) {											
// TODO add your handling code here:
}										   

private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {										 

	try {
		 input = new ObjectInputStream(new FileInputStream(file));
	   record = (videoRecord)input.readObject();
	   txtClassification.setText(record.getClassification());
	   txtTitle.setText(record.getTitle());
	   txtDuration.setText(Integer.toString(record.getDuration()));
	   txtHireCost.setText(Float.toString(record.getHireCost()));

}
catch (IOException e){
	JOptionPane.showMessageDialog(this, "File Does Not Exist!", "Error", JOptionPane.ERROR_MESSAGE);
	
}
  catch (ClassNotFoundException c){
   JOptionPane.showMessageDialog(this, "Something Wrong", "Error", JOptionPane.ERROR_MESSAGE);
   
  }
cmdAdd.setEnabled(true);
}										

private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {										  
 String searchstring = txtSearch.getText();
	videoRecord record;
	boolean watchflag = true;
	System.out.println("int");
	System.out.println("search string is"+ searchstring);
 try { System.out.println("start try");
		 while (watchflag==true){ System.out.println("start compare");
			 record = (videoRecord) input.readObject();
			 if ((record.getTitle()).equals(searchstring)){
				 
				 txtClassification.setText(record.getClassification());
				 txtTitle.setText(record.getTitle());
				 txtDuration.setText(Integer.toString(record.getDuration()));
				 txtHireCost.setText(Float.toString(record.getHireCost()));
				 System.out.println("record found");
				 watchflag=false;
					input = new ObjectInputStream(new FileInputStream(file));
	   record = (videoRecord)input.readObject();
				 
				 System.out.println("Flag set false");
			 }
		 }
		 }catch (IOException e){
	JOptionPane.showMessageDialog(this, "IO Exception!", "Error", JOptionPane.ERROR_MESSAGE);
	
}
  catch (ClassNotFoundException c){
   JOptionPane.showMessageDialog(this, "Something Wrong", "Error", JOptionPane.ERROR_MESSAGE);
   
  }
		 
	   System.out.println("end of code");
	   
}										 

private void cmdOpenActionPerformed(java.awt.event.ActionEvent evt) {										
openData();
}									   

private void cmdVectorAddActionPerformed(java.awt.event.ActionEvent evt) {											 

		
   try{
	   record = new videoRecord(
	   				txtClassification.getText(),
	   				txtTitle.getText(),
	   				Integer.parseInt(txtDuration.getText()), 
	   				Float.parseFloat(txtHireCost.getText()));
	   vectordb.add(record);
   }

catch (Exception e){
	JOptionPane.showMessageDialog(this, "Something wrong!", "Error", JOptionPane.ERROR_MESSAGE);
}
boxClear();// TODO add your handling code here:
}											

private void cmdVectorFirstActionPerformed(java.awt.event.ActionEvent evt) {											   
try {
	   vectordb.firstElement();
	   txtClassification.setText(record.getClassification());
	   txtTitle.setText(record.getTitle());
	   txtDuration.setText(Integer.toString(record.getDuration()));
	   txtHireCost.setText(Float.toString(record.getHireCost()));

}
catch (Exception e){
	JOptionPane.showMessageDialog(this, "File Does Not Exist!", "Error", JOptionPane.ERROR_MESSAGE);
	
}
 
cmdAdd.setEnabled(true);
}											  

private void cmdVectorLastActionPerformed(java.awt.event.ActionEvent evt) {											  
try {
	  vectordb.lastElement();
	   txtClassification.setText(record.getClassification());
	   txtTitle.setText(record.getTitle());
	   txtDuration.setText(Integer.toString(record.getDuration()));
	   txtHireCost.setText(Float.toString(record.getHireCost()));

}
catch (Exception e){
	JOptionPane.showMessageDialog(this, "File Does Not Exist!", "Error", JOptionPane.ERROR_MESSAGE);
	
}

cmdAdd.setEnabled(true);// TODO add your handling code here:
}											 

private void cmdNumberOfRecordsActionPerformed(java.awt.event.ActionEvent evt) {
int count = 0;
count = vectordb.size();
	txtRecordCount.setText(Integer.toString(count));	// TODO add your handling code here:
}

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new frmVideo().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton cmdAdd;
	private javax.swing.JButton cmdFirst;
	private javax.swing.JButton cmdNew;
	private javax.swing.JButton cmdNext;
	private javax.swing.JButton cmdNumberOfRecords;
	private javax.swing.JButton cmdOpen;
	private javax.swing.JButton cmdSearch;
	private javax.swing.JButton cmdVectorAdd;
	private javax.swing.JButton cmdVectorFirst;
	private javax.swing.JButton cmdVectorLast;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JLabel lblLogo;
	private javax.swing.JLabel lblclassification;
	private javax.swing.JLabel lblduration;
	private javax.swing.JLabel lblhirecost;
	private javax.swing.JLabel lblsearch;
	private javax.swing.JLabel lbltitle;
	private javax.swing.JTextField txtClassification;
	private javax.swing.JTextField txtDuration;
	private javax.swing.JTextField txtHireCost;
	private javax.swing.JTextField txtRecordCount;
	private javax.swing.JTextField txtSearch;
	private javax.swing.JTextField txtTitle;
	// End of variables declaration


public void openData(){
		try {
		 input = new ObjectInputStream(new FileInputStream(file));
		while (true){
   
	   record = (videoRecord)input.readObject();
	   vectordb.add(record);
	   System.out.println("TITLE===" + " " + record.getTitle());
	   System.out.println("CLASSIFICATION===" + record.getClassification());
	   System.out.println("HIRE COST==="+""+ record.getHireCost());
	   System.out.println("DURATION===" + record.getDuration());
	   System.out.println("***************************************************");
	   
	   
		}
}catch (EOFException e){
}
catch (IOException e){
	JOptionPane.showMessageDialog(this, "Something wrong!", "Error", JOptionPane.ERROR_MESSAGE);
	
}catch (ClassNotFoundException c){
   JOptionPane.showMessageDialog(this, "Something Wrong", "Error", JOptionPane.ERROR_MESSAGE);
   
  }
}
public void boxClear(){
   txtClassification.setText("");
   txtDuration.setText("");
   txtHireCost.setText("");
   txtTitle.setText("");   
}
}





sorry about all the pre Gen code, I will cut it out from now on!
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help With .LastElement & .FirstElement

Posted 03 May 2009 - 03:57 PM

View Postrobert_tonnessen@hotmail.com, on 3 May, 2009 - 02:53 PM, said:

sorry about all the pre Gen code, I will cut it out from now on!

Yes do so... and don't expect me to look at something wrong in an IDE generated code
And not a good idea to use an email address as username you will end up in all spam lists in the net
System administrators of this forum erase email addresses when theu figure into posts

This post has been edited by pbl: 03 May 2009 - 04:01 PM

Was This Post Helpful? 1
  • +
  • -

#6 robert_tonnessen@hotmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-November 08

Re: Help With .LastElement & .FirstElement

Posted 03 May 2009 - 04:02 PM

Sorry I dont get what your saying.
"IDE Gen code"? this is my code, I wrote it. But I wrote it in an IDE, Is that not good?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help With .LastElement & .FirstElement

Posted 03 May 2009 - 07:55 PM

View Postrobert_tonnessen@hotmail.com, on 3 May, 2009 - 03:02 PM, said:

Sorry I dont get what your saying.
"IDE Gen code"? this is my code, I wrote it. But I wrote it in an IDE, Is that not good?


You didn't wrote the GUI... you had you IDE to generate it...
if you write that code tell me what these three lines do ?

						.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
					.addGroup(jPanel1Layout.createSequentialGroup()
						.addGap(33, 33, 33)



nothing wrong about using a GUI code generator but do not expect us, human beings, to understand/decipher the hundreds of lines produced by a GUI code generator. We can fix GUI written by humans not that kind of stuff. Never seen a programmer using a GroupLayout "by hand"
Was This Post Helpful? 1
  • +
  • -

#8 robert_tonnessen@hotmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-November 08

Re: Help With .LastElement & .FirstElement

Posted 04 May 2009 - 08:00 AM

I think we might have our wires crossed, or you have taken an exception to me.

I am a student, not a programmer, and as a student I use an IDE named Netbeans, you might enjoy booting your 386, running a copy of notepad 1992 and coding in binary but here in the classroom of 2009 we use an IDE.

I did not ask you to read the hundred lines of GUI Pre gen code but infact lines of code that i did write myself. I simply asked why .LastElement was returning the same record as .FirstElement. you then said that I did not show much code so as a student and unaware as to which further code you might need I posted the entire lot.

Pbl I mean no disrespect but after reading some of your other posts I notice an underlying tone of annoyance and contempt for new programmers like myself, I would point out that this is a community of programmers for programmers and you are not forced to help.

I think we might have our wires crossed, or you have taken an exception to me.

I am a student, not a programmer, and as a student I use an IDE named Netbeans, you might enjoy booting your 386, running a copy of notepad 1992 and coding in binary but here in the classroom of 2009 we use an IDE.

I did not ask you to read the hundred lines of GUI Pre gen code but infact lines of code that i did write myself. I simply asked why .LastElement was returning the same record as .FirstElement. you then said that I did not show much code so as a student and unaware as to which further code you might need I posted the entire lot.

Pbl I mean no disrespect but after reading some of your other posts I notice an underlying tone of annoyance and contempt for new programmers like myself, I would point out that this is a community of programmers for programmers and you are not forced to help.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help With .LastElement & .FirstElement

Posted 04 May 2009 - 06:21 PM

I use an IDE myself :) Haven't touch a card key punch for 30 years.
And when a new processor is available on the market I die to buy one. So I don't have a 386.

There is a difference between an IDE and a GUI generator.
Swing is really easy to use and to really understand a GUI, if you want to use the "pedagogic" approach it is better to write it by yourself.
That will really teach you how it works and the interactions between ActionEvent and threads repainting your stuff.

A human written GUI will take 1/10 to 1/20 the number of lines of code than a generated one.

Now, if you want to finish your homework ASAP use a GUI generator no problem. If you want to make out a living out of programming I can tell you that never in the field (in real life) somebody will push a generated GUI in production... it is simply impossible to support it. When you San Francisco branch will call you in New York saying that the last out of 3 decimals is missing in your JTextField you will do what ? New York and San Francisco might use different IDE without problem: Java is Java. But if they both use different generator your are screwed. You will barely see an application in the field generated by one of these.

If you need a one shot program to fix a ponctual problem go ahead. If you think your stuff will be in production more than 2 weeks: forget about GUI generator. Human errors are hundred times easier to fix than computer generated errors.

We are here, or at least I am here, to help people fix THEIR Java code including their GUI problem... and I think I do a pretty good job. I am not here to try to figure out which checkbox a user didn't checked in when the GUI Generator offered:
- have this component to fired an event when changed
- have this component change color when cliked
...
I was happy to fix your first problem and then you came back with the second one.

I can probably fix 10 to 20 problems in this forum in one evening, I am in the living room working on my laptop while my beloved wife watch her soaps. If I try to fix a GUI Generated problem I could probably fix only one per evening.

Prefer to help people who wants to learn.... do not want to be unrespectable.

I had this discussion more than 20 times... the replies were always "I understand"

Happy coding and I'll be glad to help, if it is not GUI related.
Cheers

This post has been edited by pbl: 05 May 2009 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#10 robert_tonnessen@hotmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-November 08

Re: Help With .LastElement & .FirstElement

Posted 05 May 2009 - 02:14 AM

I understand.

I do wish to learn, I am 30 Years old! working full time as a field service engineer + full time university for my degree + have just become Microsoft Certified. The problem is with University education and the time you have to do your work. I have 3 hours contact per week shared with 20 other students and in my final year this will drop to 45mins shared with 50+ students. I wish I could write it all myself but time does not permit :(

Thanks pbl.

P.s so for my problem of FirstElement returning the same as LastElement are we saying this is a GUI problem or that I am not using the Method correctly? Before using the Vector I could move through my file ie. First/Last/Next/Previous but now that I use the Vector i cannot, I was thinking it had more to do with the way I was reading in to the vector with my while loop than a problem of displaying the record.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help With .LastElement & .FirstElement

Posted 05 May 2009 - 05:14 AM

I just said that both methods are exactly the same to no wonder they perform the same action and return the same result.

Might be linked to your GUI. Might be, don't want to accuse your GUI generator.
You see in any program you want to try to keep as separate as possible:
- the data the way it is stored (file, SQL databse, linked list, serial port monitoring devices, ....)
- the business rules (what the program computes, sale taxes, pay roll, ...)
- the user interface (GUI, console mode, file, ....

So if you decide to switch from GUI to console mode application, you shouldn't have to change the business rules part or the data part
if you decide to change a business rule, the way to compute sale taxes, you shouldn't have to change your GUI
if you decide to change the way you store your data, from flat file to SQL databse, the last thing you should have to change is your GUI

The problem with GUI generator is that, if not correctly used, they intermix all these levels into a spagetti absolutly impossible to decipher.
If I ask you tomorrow to convert your application from GUI based to console mode how long do you think it will take ?
We cannot, looking at your code, separate the logic from the GUI.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1