Results 1 to 5 of 5
  1. #1
    Marco065 is offline Member
    Join Date
    May 2015
    Posts
    3
    Rep Power
    0

    Default Java GUI Search and Update

    Hi

    I'm trying to make a update page where you search for person info by their number then have the ability to change any info and save it back to the binary field. I have the search working but i don't know how to do the update, so if anyone can help with this i would appreciate it.

    here my code:

    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.EOFException;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class UpdatePhotoDetails extends JFrame implements ActionListener
    {
    	private JLabel lbl1,lbl2,lbl3,lbl4,lbl5;
    	private JTextField txtName,txtTitle;
    	private JTextField txtPhotoNo;
    	private JComboBox cmbCategory,cmbGroup;
    	private JButton btnExit,btnDisplay;
    	private JPanel panel1, panel2;
    	private Container cn;
    	
    	private File out;
    	private RandomAccessFile rd; 
    	
    	private int photoNo;	// 4 bytes
    	private String name; // 30 bytes
    	private String title; // 20 bytes
    	private String category; // 15 bytes
    	private String group; //1 bytes
    	
    	long pos;
    	
    	UpdatePhotoDetails()
    	{
    		cn = getContentPane();
    		cn.setBackground(Color.cyan);
    		
    		cn.setLayout(null); 		//Absolute Layout Manager
    		
    		panel1 = new JPanel();
    		panel1.setLayout(null);		//Absolute Lay out Manager
    		
    		panel1.setBackground(Color.WHITE);
    		panel1.setBounds(10,10,770,100);
    		cn.add(panel1);
    		
    		lbl1= new JLabel("Video No",JLabel.LEFT);
    		lbl1.setFont(new Font("Serif", Font.BOLD,20));
    		lbl1.setBounds(10,20,150,50);
    		panel1.add(lbl1);
    		
    		txtPhotoNo= new JTextField(30);
    		txtPhotoNo.setFont(new Font("Serif", Font.BOLD,20));
    		txtPhotoNo.setBounds(200,20,80,50);
    		panel1.add(txtPhotoNo);
    		
    		btnDisplay = new JButton ("Display");
    		btnDisplay.setFont(new Font("Serif", Font.BOLD,20));
    		btnDisplay.setBounds(500,20,100,50);
    		btnDisplay.addActionListener(this);
    		panel1.add(btnDisplay);
    		
    		btnExit = new JButton ("Exit");
    		btnExit.setFont(new Font("Serif", Font.BOLD,20));
    		btnExit.setBounds(630,20,80,50);
    		btnExit.addActionListener(this);
    		panel1.add(btnExit);
    		
    
    		panel2 = new JPanel();
    		panel2.setLayout(null);		//Absolute Lay out Manager
    		
    		panel2.setBackground(Color.WHITE);
    		panel2.setBounds(10,150,770,300);
    		cn.add(panel2);
    		
    		lbl2= new JLabel("Name",JLabel.LEFT);
    		lbl2.setFont(new Font("Serif", Font.BOLD,20));
    		lbl2.setBounds(100,50,150,50);
    		panel2.add(lbl2);
    		
    		lbl3= new JLabel("Title",JLabel.LEFT);
    		lbl3.setFont(new Font("Serif", Font.BOLD,20));
    		lbl3.setBounds(100,100,150,50);
    		panel2.add(lbl3);
    		
    		lbl4= new JLabel("Category",JLabel.LEFT);
    		lbl4.setFont(new Font("Serif", Font.BOLD,20));
    		lbl4.setBounds(100,150,150,50);
    		panel2.add(lbl4);
    		
    		lbl4= new JLabel("Group",JLabel.LEFT);
    		lbl4.setFont(new Font("Serif", Font.BOLD,20));
    		lbl4.setBounds(100,200,150,50);
    		panel2.add(lbl4);
    		
    		txtName= new JTextField("Title",JTextField.LEFT);
    		txtName.setFont(new Font("Serif", Font.BOLD,20));
    		txtName.setBackground(Color.BLACK);
    		txtName.setForeground(Color.WHITE);
    		txtName.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
    		txtName.setOpaque(true);
    		txtName.setBounds(250,50,300,50);
    		panel2.add(txtName);
    		
    		txtTitle= new JTextField("Title",JTextField.LEFT);
    		txtTitle.setFont(new Font("Serif", Font.BOLD,20));
    		txtTitle.setBackground(Color.BLACK);
    		txtTitle.setForeground(Color.WHITE);
    		txtTitle.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
    		txtTitle.setOpaque(true);
    		txtTitle.setBounds(250,100,300,50);
    		panel2.add(txtTitle);
    		
    		String [] cat = {"1- Still Life","2- Wild Life","3- Landscape","4- Architecture"};
    		cmbCategory= new JComboBox(cat);
    		cmbCategory.setFont(new Font("Serif", Font.BOLD,20));
    		cmbCategory.setBackground(Color.BLACK);
    		cmbCategory.setForeground(Color.WHITE);
    		cmbCategory.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
    		cmbCategory.setOpaque(true);
    		cmbCategory.setBounds(250,150,80,50);
    		panel2.add(cmbCategory);
    		
    		String [] grp = {"A","B","C","D"};
    		cmbGroup= new JComboBox(grp);
    		cmbGroup.setFont(new Font("Serif", Font.BOLD,20));
    		cmbGroup.setBackground(Color.BLACK);
    		cmbGroup.setForeground(Color.WHITE);
    		cmbGroup.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
    		cmbGroup.setOpaque(true);
    		cmbGroup.setBounds(250,200,80,50);
    		panel2.add(cmbGroup);
    		
    		panel2.setVisible(false);	
    		
    	}
    	
    	public void actionPerformed(ActionEvent e) 
    	{
    		int no;
    		
    		if ( e.getSource()==btnExit)
    		{
    			try 
    			{
    				rd.close();
    				this.setVisible(false);
    			} catch (IOException e1) 
    			{
    				JOptionPane.showMessageDialog(null, "Cannot Close Photo File");
    			}
    			
    		}
    		if ( e.getSource()==btnDisplay)
    		{
    			
    			try
    			{
    				out = new File ("Photos.dat");
    				rd = new RandomAccessFile(out,"rw");
    
    				no=Integer.parseInt(txtPhotoNo.getText());
    
    				if ( rd.length()>0)
    				{  
    					rd.seek((long)0);
    					boolean eof = false;
    					boolean found = false;
    					do
    					{
    						try
    						{
    							pos = rd.getFilePointer();
    							photoNo = rd.readInt();
    							name = rd.readUTF();
    							title = rd.readUTF();
    							category = rd.readUTF();
    							group = rd.readUTF();
    							
    							if (no == photoNo)
    							{
    								found = true;
    								txtName.setText(name);
    								txtTitle.setText(title);
    								cmbCategory.setSelectedItem(category);
    								cmbGroup.setSelectedItem(group);
    								panel2.setVisible(true);
    							}
    							
    						}
    						catch( EOFException e1)
    						{
    							eof =true;
    						}
    					
    					}while ( !eof && !found);
    				
    					if ( ! found)
    					{
    						JOptionPane.showMessageDialog(null, "Invalid Photo No");
    						panel2.setVisible(false);
    					}
    				}
    				else
    					JOptionPane.showMessageDialog(null, "No Photo Details to Display");
    			}
    			catch(NumberFormatException ex3)
    			{
    				JOptionPane.showMessageDialog(null, "Photo No Not Numeric");
    			}
    			catch(IOException ex2)
    			{
    				JOptionPane.showMessageDialog(null, "Cannot Open File");
    			}
    			
    			//if ( e.getSource()==btnUpdate)
    			//{
    				//vid object to the values of the form
    				//seek the position of the video number in the file
    				//rd.seek((long)0);
    				//rd.seek(pos);
    				//write the details
    			//}
    		}
    		
    	}
    	
    	
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java GUI Search and Update

    how to do the update
    Where is the data that is to be updated?
    A flat file or a database?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Marco065 is offline Member
    Join Date
    May 2015
    Posts
    3
    Rep Power
    0

    Default Re: Java GUI Search and Update

    It's a flat file

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java GUI Search and Update

    One way to update data in a flat file (size permitting) is to read the whole file into a String, update the contents of the String and then write out the updated String to the file.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Marco065 is offline Member
    Join Date
    May 2015
    Posts
    3
    Rep Power
    0

    Default Re: Java GUI Search and Update

    I've got it working now thanks :D

Similar Threads

  1. Replies: 1
    Last Post: 12-22-2014, 06:12 PM
  2. Replies: 1
    Last Post: 12-01-2012, 03:20 AM
  3. Replies: 0
    Last Post: 02-24-2012, 08:39 AM
  4. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  5. Replies: 5
    Last Post: 08-26-2008, 03:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •