hello everyone.
ok i got a program (see below i know its long but just copy and paste and it will work),, my only problem is i need to include a search method (i.e. where i enter CDName and it prints out the rest of the details of the CD.)
if neone knows how to do it pleez , pleez help me.
package cdminiproject;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class DatabaseGUI extends JFrame implements ActionListener
{
// DatabaseGUI class instance variables
private JTextArea textarea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane( textarea );
private JFileChooser filedlg = null;
private JPanel statusPanel = new JPanel();
private JLabel lbStatusPanel = new JLabel();
private JMenuBar menubar = new JMenuBar();
private JMenu database = new JMenu( "Database" );
private JMenu action = new JMenu( "Actions" );
private JMenuItem loadDB = new JMenuItem( "Load" );
private JMenuItem saveDB = new JMenuItem( "Save" );
private JMenuItem addNewCD = new JMenuItem( "Add New CD Entry..." );
private JMenuItem updateCD = new JMenuItem( "Update CD Information..." );
private JMenuItem printAllCD = new JMenuItem( "Print All CDs Details" );
private JMenuItem sortAllCD = new JMenuItem( "Sort By Artist Name" );
private JMenuItem exit = new JMenuItem( "Exit" );
private Vector cdVector = new Vector();
// update cd entry dialog
private JDialog uptdlg_dialog = new JDialog( this, "Update CD Entry", true );
private JOptionPane uptdlg_inputCdNamePane = new JOptionPane();
private JPanel uptdlg_panel = new JPanel( new GridLayout( 4, 2, 30, 50 ) );
private JLabel uptdlg_lbCdName = new JLabel( "Enter CD Name" );
private JLabel uptdlg_lbArtistName = new JLabel( "Enter Artist Name" );
private JLabel uptdlg_lbNoOfTracks = new JLabel( "Enter No. of Tracks" );
private JTextField uptdlg_tfCdName = new JTextField( 100 );
private JTextField uptdlg_tfArtistName = new JTextField( 100 );
private JTextField uptdlg_tfNoOfTracks = new JTextField( 3 );
private JButton uptdlg_btnCancel = new JButton( "Cancel" );
private JButton uptdlg_btnOK = new JButton( "OK" );
private int searchedCDIndex = -1;
public DatabaseGUI()
{
Container content = getContentPane();
BorderLayout borderLayout = new BorderLayout();
// setting layout of frame
content.setLayout( borderLayout );
setTitle( "CD Collection" );
setBounds( 10, 10, 600, 500 );
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable( false );
setJMenuBar( menubar );
// adding text area control
textarea.setEditable( false );
textarea.setTabSize( textarea.getTabSize() + 10 );
content.add( scrollPane, BorderLayout.CENTER );
// adding status panel
lbStatusPanel.setText( "Application is ready" );
lbStatusPanel.setSize( 500, 30 );
statusPanel.setSize( 500, 50 );
statusPanel.setBorder( BorderFactory.createLoweredBevelBorder() );
statusPanel.add( lbStatusPanel );
content.add( statusPanel, BorderLayout.SOUTH );
// add menu items' mnemonics
action.setMnemonic( 'A' );
database.setMnemonic( 'D' );
loadDB.setMnemonic( 'L' );
saveDB.setMnemonic( 'S' );
exit.setMnemonic( 'X' );
addNewCD.setMnemonic( 'N' );
updateCD.setMnemonic( 'U' );
printAllCD.setMnemonic( 'P' );
sortAllCD.setMnemonic( 'R' );
// add accelerators to menu items
loadDB.setAccelerator( KeyStroke.getKeyStroke( 'L', Event.CTRL_MASK ) );
saveDB.setAccelerator( KeyStroke.getKeyStroke( 'S', Event.CTRL_MASK ) );
addNewCD.setAccelerator( KeyStroke.getKeyStroke( 'N', Event.CTRL_MASK ) );
updateCD.setAccelerator( KeyStroke.getKeyStroke( 'U', Event.CTRL_MASK ) );
printAllCD.setAccelerator( KeyStroke.getKeyStroke( 'P', Event.CTRL_MASK ) );
sortAllCD.setAccelerator( KeyStroke.getKeyStroke( 'R', Event.CTRL_MASK ) );
// add menu items to menu
database.add( loadDB );
database.add( saveDB );
database.addSeparator();
database.add( exit );
action.add( addNewCD );
action.add( updateCD );
action.add( printAllCD );
action.addSeparator();
action.add( sortAllCD );
// add action listener to menu items
loadDB.addActionListener( this );
saveDB.addActionListener( this );
exit.addActionListener( this );
addNewCD.addActionListener( this );
updateCD.addActionListener( this );
printAllCD.addActionListener( this );
sortAllCD.addActionListener( this );
// add menu to menubar
menubar.add( database );
menubar.add( action );
setVisible( true );
}
public void actionPerformed( ActionEvent e )
{
Object source = e.getSource();
// main events performed from user
if( source == loadDB )
{
loadDBHandler();
}
else if( source == saveDB )
{
saveDBHandler();
}
else if( source == exit )
{
exitHandler();
}
else if( source == addNewCD )
{
addNewCDHandler();
}
else if( source == updateCD )
{
updateCDHandler();
}
else if( source == printAllCD )
{
printAllCDHandler();
}
else if( source == sortAllCD )
{
sortAllCDHandler();
}
else if( source == uptdlg_btnOK )
{
CDEntry updatedCD = new CDEntry();
updatedCD.setCdName( uptdlg_tfCdName.getText() );
updatedCD.setArtistName( uptdlg_tfArtistName.getText() );
Integer x = new Integer( uptdlg_tfNoOfTracks.getText() );
updatedCD.setNoOfTracks( x.intValue() );
// close dialog
uptdlg_dialog.dispose();
// replace original cd entry with updatedCD
cdVector.setElementAt( updatedCD, searchedCDIndex );
// empty all text fields
uptdlg_tfCdName.setText( "" );
uptdlg_tfArtistName.setText( "" );
uptdlg_tfNoOfTracks.setText( "" );
}
else if( source == uptdlg_btnCancel )
{
uptdlg_dialog.dispose();
}
}// actionPerformed
public int loadDBHandler()
{
//filedlg.setCurrentDirectory( JFileChooser.d );
filedlg = new JFileChooser();
int result = filedlg.showOpenDialog( this );
if( result == JFileChooser.CANCEL_OPTION )
return 0;
CDEntry cd;
File file = new File( filedlg.getSelectedFile().getAbsolutePath() );
System.out.println( file.getAbsolutePath() );
boolean EOF = false;
boolean isLoadSuccess = true;
try
{
DataInputStream stream = new DataInputStream(
new FileInputStream( file ) );
while( ! EOF )
{
try
{
cd = new CDEntry();
String s = "";
char ch;
while( ( ch = stream.readChar() ) != ',' )
{
s = s + ch;
}
cd.setCdName( s );
//System.out.println( s );
s = "";
while( ( ch = stream.readChar() ) != ',' )
{
s = s + ch;
}
cd.setArtistName( s );
//System.out.println( s );
s = "";
while( ( ch = stream.readChar() ) != '\n' )
{
s = s + ch;
}
Integer x = new Integer( s );
cd.setNoOfTracks( x.intValue() );
//System.out.println( s );
//add cd to cdVector
cdVector.add( cd );
}
catch( EOFException e )
{
EOF = true;
}
}// while
}
catch( IOException e )
{
System.out.println( e.getMessage() );
}
// is load opration success or fail
if( isLoadSuccess )
{
JOptionPane info = new JOptionPane();
info.showMessageDialog( this, "File Loaded Successfully" );
// showing message in status panel
lbStatusPanel.setText( "File has loaded" );
}
else
{
JOptionPane info = new JOptionPane();
info.showMessageDialog( this, "File Could not Load Successfully" );
}
return 1;
}// loadDBHandler
public int saveDBHandler()
{
filedlg = new JFileChooser();
filedlg.setCurrentDirectory( filedlg.getCurrentDirectory() );
int result = filedlg.showSaveDialog( this );
if( result == JFileChooser.CANCEL_OPTION )
return 0;
CDEntry cd;
File file = new File( filedlg.getSelectedFile().getAbsolutePath() );
System.out.println( file.getAbsolutePath() );
int i;
boolean isSaveSuccess = true;
try
{
file.createNewFile();
DataOutputStream stream = new DataOutputStream(
new FileOutputStream( file ) );
for( i = 0; i < cdVector.size(); i ++ )
{
cd = (CDEntry)cdVector.elementAt( i );
String s;
s = cd.getCdName();
s.trim();
s = s + ",";
s = s + cd.getArtistName();
s.trim();
s = s + ",";
Integer x = new Integer( cd.getNoOfTracks() );
String t = x.toString();
s = s + t;
// writing fields of cd separated by ','
stream.writeChars( s );
// writunbg end of line character after one record
stream.writeChar( '\n' );
}
stream.close();
}
catch( IOException e )
{
System.out.println( e.getMessage() );
}
// is load opration success or fail
if( isSaveSuccess )
{
JOptionPane info = new JOptionPane();
info.showMessageDialog( this, "File Saved Successfully" );
// showing message in status panel
lbStatusPanel.setText( "File has saved" );
}
else
{
JOptionPane info = new JOptionPane();
info.showMessageDialog( this, "File Could not Save Successfully" );
}
return 1;
}// saveDBHandler
public void exitHandler()
{
dispose();
System.exit( 0 );
}
public void addNewCDHandler()
{
final JDialog dialog = new JDialog( this, "Add New CD Entry", true );
JPanel panel = new JPanel( new GridLayout( 4, 2, 30, 50 ) );
JLabel lbCdName = new JLabel( "Enter CD Name" );
JLabel lbArtistName = new JLabel( "Enter Artist Name" );
JLabel lbNoOfTracks = new JLabel( "Enter No. of Tracks" );
final JTextField tfCdName = new JTextField( 100 );
final JTextField tfArtistName = new JTextField( 100 );
final JTextField tfNoOfTracks = new JTextField( 3 );
final JButton btnOK = new JButton( "OK" );
final JButton btnCancel = new JButton( "Cancel" );
final CDEntry cd = new CDEntry();
dialog.setBounds( 50, 30, 360, 280 );
dialog.setResizable( false );
Container content = dialog.getContentPane();
content.setLayout( new BorderLayout() );
panel.add( lbCdName );
panel.add( tfCdName );
panel.add( lbArtistName );
panel.add( tfArtistName );
panel.add( lbNoOfTracks );
panel.add( tfNoOfTracks );
panel.add( btnOK );
panel.add( btnCancel );
content.add( panel, BorderLayout.CENTER );
// if user hit OK button of dialog
btnOK.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
cd.setCdName( tfCdName.getText() );
cd.setArtistName( tfArtistName.getText() );
Integer x = new Integer( tfNoOfTracks.getText() );
cd.setNoOfTracks( x.intValue() );
// close dialog
dialog.dispose();
// add new cd entry to vector
cdVector.add( cd );
}
} );
// if user hit Cancel button of dialog
btnCancel.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
dialog.dispose();
}
} );
// show dialog
dialog.setVisible( true );
// showing message in status panel
lbStatusPanel.setText( "New entry has added" );
}// addNewCDHandler
public void updateCDHandler()
{
CDEntry searchedCD = null;
int i;
String cdName = "";
boolean isCdFound = false;
// get cd name from user
// that record is to be updated
uptdlg_inputCdNamePane.setOptionType( uptdlg_inputCdNamePane.OK_OPTION );
cdName = uptdlg_inputCdNamePane.showInputDialog( "Enter CD Name" );
// search the cdVector for CD entry
// having required CD name
for( i = 0; i < cdVector.size(); i ++ )
{
searchedCD = (CDEntry)cdVector.elementAt( i );
if( (searchedCD.getCdName().compareTo( cdName )) == 0 )
{
searchedCDIndex = i;
isCdFound = true;
break;
}
}
if( ! isCdFound )
{
JOptionPane m = new JOptionPane();
m.showMessageDialog( this, "CD Entry with the given CD name does not exist" );
}
else
{
// display existing values of CD Entry in dialog
uptdlg_dialog.setBounds( 50, 30, 360, 280 );
uptdlg_dialog.setResizable( false );
Container content = uptdlg_dialog.getContentPane();
content.setLayout( new BorderLayout() );
uptdlg_panel.add( uptdlg_lbCdName );
uptdlg_panel.add( uptdlg_tfCdName );
uptdlg_panel.add( uptdlg_lbArtistName );
uptdlg_panel.add( uptdlg_tfArtistName );
uptdlg_panel.add( uptdlg_lbNoOfTracks );
uptdlg_panel.add( uptdlg_tfNoOfTracks );
uptdlg_panel.add( uptdlg_btnOK );
uptdlg_panel.add( uptdlg_btnCancel );
content.add( uptdlg_panel, BorderLayout.CENTER );
// if user hit OK button of dialog
uptdlg_btnOK.addActionListener( this );
// if user hit Cancel button of dialog
uptdlg_btnCancel.addActionListener( this );
// show dialog
uptdlg_dialog.setVisible( true );
// showing message in status panel
lbStatusPanel.setText( "Entry has updated" );
}// if-else serchedCd is null
}// updateCDHandler
public void printAllCDHandler()
{
CDEntry cd;
int i;
textarea.setText( "\tDisplaying CDs Information\n" );
textarea.append( "Artist Name\t" + "CD Name\t" + "No. of Tracks\n\n" );
for( i = 0; i < cdVector.size(); i ++ )
{
cd = (CDEntry)cdVector.elementAt( i );
textarea.append( cd.getArtistName() + "\t" +
cd.getCdName() + "\t" +
cd.getNoOfTracks() + "\n" );
}
}// printAllCDHandler
public void sortAllCDHandler()
{
//System.out.println( "i'm in sort" );
CDEntry curr = null;
CDEntry next = null;
CDEntry temp = null;
int i, j;
for( i = 0; i < cdVector.size(); i ++ )
{
for( j = 0; j < ((cdVector.size() - 1) - i); j ++ )
{
curr = (CDEntry)cdVector.elementAt( j );
next = (CDEntry)cdVector.elementAt( j + 1 );
if( ((curr.getArtistName()).compareTo( next.getArtistName() )) > 0 )
{
temp = next;
cdVector.setElementAt( curr, j + 1 );
cdVector.setElementAt( next, j );
}
}
}
// after sorting refresh screen
printAllCDHandler();
}// sortAllCDHandler
public class CDEntry
{
private String cdName;
private String artistName;
private int noOfTracks;
// constructor
public CDEntry()
{
cdName = null;
artistName = null;
noOfTracks = 0;
}
public CDEntry( String cdName, String artistName, int noOfTracks )
{
this.cdName = cdName;
this.artistName = artistName;
this.noOfTracks = noOfTracks;
}
// setter and getter methods for instance variables
void setCdName( String cdName )
{
this.cdName = cdName;
}
String getCdName()
{
return cdName;
}
void setArtistName( String artistName )
{
this.artistName = artistName;
}
String getArtistName()
{
return artistName;
}
void setNoOfTracks( int noOfTracks )
{
this.noOfTracks = noOfTracks;
}
int getNoOfTracks()
{
return noOfTracks;
}
}
public static void main(String[] args)
{
DatabaseGUI gui = new DatabaseGUI();
}
}