Fiona
Posts: 4
Nickname: fi
Registered: Jun, 2002
|
|
Re: jswing & a io rw dat file application
|
Posted: Jun 17, 2002 1:14 PM
|
|
hey charles ...
my code now is slightly different to what i had originally posted here in the forum. i'm not to set any values into for the text field as this program is to "create" a new randomAccessFile in a binary format. i have two other class files that are to be incorporated into the main java file of the program that allow me 1. to sort and 2. to set to fields to null for "rw". so i cannot put any properties in.
my main code file looks like this:-
package book; import javax.swing.*; import javax.swing.border.Border; import javax.swing.ImageIcon; import java.text.DecimalFormat; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.plaf.*; import javax.swing.plaf.basic.*;
public class BookListA extends JFrame { RandomAccessFile raf;
/** mainframe */ Font textFont; private JRootPane root = new JRootPane(); /** creating the three panels to make up the content pane */ private JPanel blankJP = new JPanel(); private JPanel boxLabels = new JPanel( new GridLayout(9, 1) ); private JPanel boxTextFields = new JPanel( new GridLayout(8, 1) ); private JPanel bookLabels = new JPanel(); private JPanel east = new JPanel(); private JPanel bookTextFields = new JPanel(); private JPanel editPane = new JPanel(); private JFileChooser fileChoose = new JFileChooser(); private JDialog fileDlg; JLabel blank = new JLabel( "" ); JLabel isdn = new JLabel( "ISDN #:" ); JTextField isdnText = new JTextField("", 15); JLabel title = new JLabel( "Title:" ); JTextField titledText = new JTextField("", 15); JLabel author = new JLabel( "Author:" ); JTextField authorText = new JTextField("", 15); JLabel publisher = new JLabel( "Publisher:" ); JTextField publisherText = new JTextField("", 15); JLabel edition = new JLabel( "Edition:" ); JTextField editionText = new JTextField("", 15); JLabel year = new JLabel( "Year:" ); JTextField yearText = new JTextField("", 15); JLabel price = new JLabel( "Price:" ); JTextField priceText = new JTextField("", 15); JLabel comments = new JLabel( "Comments:" ); JTextField commentsText = new JTextField( "", 15 );
JButton addB = new JButton( "Add Book" ); JButton deleteB = new JButton( "Delete Book" ); JButton previousB = new JButton( "Previous Book" ); JButton nextB = new JButton( "Next" );
/** declaring the jfrm constraints */ private static final boolean RESIZABLE = false; private static final boolean CLOSABLE = true; private static final boolean MAXIMIZABLE = false; private static final boolean ICONIFIABLE = true; private static final boolean MODAL = false;
/** the constructor */ public BookListA( String titleText ) {
super( titleText ); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getScreenSize(); int width = 400, height = 200; setBounds((d.width - width)/2, (d.height - height)/2, width, height); setResizable(false); getContentPane().setLayout( new FlowLayout() );
/** level 1 menu objects */ JToolBar jtb = new JToolBar(); JMenu file = new JMenu("File"); JMenu sort = new JMenu("Sort"); JMenuBar jmb = new JMenuBar(); jmb.add( file ); jmb.add( sort );
/** level 2 menu items */ NewAction na = new NewAction(); file.add( na ).setMnemonic( 'N' ); jtb.add( na ); OpenAction op = new OpenAction(); file.add( op ).setMnemonic( 'O' ); jtb.add( op ); SaveAction sa = new SaveAction(); file.add( sa ).setMnemonic( 'S' ); jtb.add( sa ); ExitAction ex = new ExitAction(); file.add( ex ).setMnemonic( 'E' ); jtb.add( ex ); AuthorAction au = new AuthorAction(); sort.add( au ).setMnemonic( 'A' ); jtb.add( au ); TitleAction ta = new TitleAction(); sort.add( ta ).setMnemonic( 'T' ); jtb.add( ta );
setJMenuBar( jmb ); boxLabels.add( blank ); boxLabels.add( isdn ); boxLabels.add( title ); boxLabels.add( author ); boxLabels.add( publisher ); boxLabels.add( edition ); boxLabels.add( year ); boxLabels.add( price ); boxLabels.add( comments );
boxTextFields.add( isdnText ); boxTextFields.add( titledText ); boxTextFields.add( authorText ); boxTextFields.add( publisherText ); boxTextFields.add( editionText ); boxTextFields.add( yearText ); boxTextFields.add( priceText ); boxTextFields.add( commentsText );
//add the action commands to the third JPanel( editPane ) editPane.add( addB ); editPane.add( deleteB ); editPane.add( previousB ); editPane.add( nextB ); addB.setEnabled( true ); addB.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { //throws IOException /** note the code here is trying to access the randomAccessFile or create a new one by using the add button but I am having difficulty with it?..
JFileChooser fileChooser = new JFileChooser(); try { book.BookDetails bookDetail = getBookDetails(); String[] values = { String.valueOf( bookDetail() ), bookDetail.getIsdn(), bookDetail.getTitle(), bookDetail.getAuthor(); bookDet ail.getPublisher(); bookDetail.getEdition(); bookDetail.getYear(); bookDetail.ge tPrice(); bookDetail.getComments(); BookListA.setFieldValues( values ); } catch ( NumberFormatException nfe ) { JOptionPane.showMessageDialog( new JFrame(), "Invalid Entry", "Invalid Number Format", JOptionPane.ERROR_MESSAGE );
*/ } } ); deleteB.setEnabled( true ); deleteB.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { } } );
previousB.setEnabled( true ); previousB.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { } } ); nextB.setEnabled( true ); nextB.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { } } ); //fill the content pane( cp ) Container cp = getContentPane(); bookLabels.add( boxLabels ); boxTextFields.setBorder( BorderFactory.createTitledBorder( "Book Details" ) ); boxTextFields.setBackground( Color.cyan ); bookTextFields.add( boxTextFields ); cp.setLayout(new BorderLayout()); cp.add( boxLabels, BorderLayout.WEST ); cp.add( boxTextFields, BorderLayout.CENTER ); cp.add( blankJP, BorderLayout.EAST ); cp.add( editPane, BorderLayout.SOUTH ); setSize( 300, 250 ); setVisible( false );
root.getContentPane();
/** creating the constructor of the internal frame * including its size, layout, calling in constraints set * and adding a message to the messagebox. */ JInternalFrame jfrm = new JInternalFrame( "Internal Frame", CLOSABLE, ICONIFIABLE ); jfrm.setPreferredSize( new Dimension( 375, 300 ) ); JDesktopPane jdt = new JDesktopPane(); jdt.setLayout( new FlowLayout() ); jdt.add( jfrm ); getContentPane().add( jdt, BorderLayout.EAST ); setSize( 500, 300 ); setVisible( true ); JOptionPane.showMessageDialog( jfrm, "Created by FiFi Applications!" ); } /** creating the menu object classes */ class NewAction extends AbstractAction { public NewAction() { super( "New", new ImageIcon( "new.gif" )); } public void actionPerformed( ActionEvent e ) { } } class OpenAction extends AbstractAction { public OpenAction() { super( "Open", new ImageIcon( "open.gif" )); } public void actionPerformed( ActionEvent e ) { int result = fileChoose.showOpenDialog( BookListA.this ); File file = fileChoose.getSelectedFile(); if ( file != null && result == JFileChooser.APPROVE_OPTION ) try { FileReader fr = new FileReader( file ); char[] charBuffer = new char[4096]; int charsRead = fr.read( charBuffer, 0, charBuffer.length ); while ( charsRead != -1 ) { charsRead = fr.read( charBuffer, 0, charBuffer.length ); } } catch( IOException ioe ) { ioe.printStackTrace(); } } } class SaveAction extends AbstractAction { public SaveAction() { super( "Save", new ImageIcon( "save.gif" ) ); } public void actionPerformed( ActionEvent e ) { } }
class ExitAction extends AbstractAction { public ExitAction() { super( "Exit", new ImageIcon( "exit.gif" ) ); } public void actionPerformed( ActionEvent e ) { System.exit(0); } } class AuthorAction extends AbstractAction { public AuthorAction() { super( "by Author" ); } public void actionPerformed( ActionEvent e ) {
// BookSort doesAuthorContain = new BookSort(); // book.BookSort sortAuthor = new BookSort();
/** here I?ve been working upon the sort methods of title and author but gave up since I didn?t have the randomAccess dat file even connecting yet :\
String choice = ""; while (!(choice.equalsIgnoreCase("x"))){ String isdnText = JOptionPane.showInputDialog( "Enter a book isdn#:"); Book book = new Book( isdnText ); String message = "You have selected:\n" + " Title: " + book.BookListA( title ) + "\n" + " Price: " + book.BookListA( price ) + "\n\n" + "Press Enter to continue or enter 'x' to exit:"; choice = JOptionPane.showInputDialog(null, message, "Book", JOptionPane.PLAIN_MESSAGE); }//end while System.exit(0); */ } }
class TitleAction extends AbstractAction { public TitleAction() { super( "by Title" ); } public void actionPerformed( ActionEvent e ) { } }
/** naming the main frame */ public static void main( String[] args ) { new BookListA( "The Book List!" ); } }
the other two class files that go with this is
1. sort method
public class BookSort{ private String title; private String author;
public BookSort(String Author, String Title) { title = Title; author = Author; } boolean doesTitleContain(String word) { String lowerCaseTitle = title.toLowerCase(); String lowerCaseWord = word.toLowerCase(); return lowerCaseTitle.indexOf(lowerCaseWord) >= 0; } boolean doesAuthorContain(String word) { String lowerCaseAuthor = author.toLowerCase(); String lowerCaseWord = word.toLowerCase(); return lowerCaseAuthor.indexOf(lowerCaseWord) >= 0; } public String showInfo() { return "Title: " + title + "\n" + "Author: " + author + "\n"; } }
and
2. the bookDetails class file creating an array for the textfields setting them to null public class BookDetails { public String isdn; public String title; public String author; public String publisher; public String edition; public String year; public String price; public String comments; //constructor BookDetails () { setIsdn( "0" ); setTitle("0"); setAuthor("0"); setPublisher("0"); setEdition("0"); setYear("0"); setPrice("0"); setComments("0"); } public void setIsdn(String isdn){this.isdn = isdn; } public void setTitle(String title){this.title = title; } public void setAuthor(String author){this.author = author; } public void setPublisher(String p){this.publisher = p; } public void setEdition(String e){this.edition = e; } public void setYear(String y){this.year = y; } public void setPrice(String p){this.price = p; } public void setComments(String c){this.comments = c; }
public String getIsdn() { return isdn;} public String getTitle() { return title;} public String getAuthor() { return author; } public String getPublisher() { return publisher; } public String getEdition(){ return edition; } public String getYear() { return year; } public String getPrice() { return price; } public String getComments(){ return comments; } }
anyways ... i'm still plugging away at trying to workout accessing or creating a randomAccess file in binary format .... and still loggerheading it >8{
but that jswing is cool stuff even if i'd be having problems ... ahahaha ...
let me know what you think ... thanks again charles :)
fi
|
|