Results 1 to 10 of 10
  1. #1
    jdmoore1 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Problems with new Frame

    Hey All,

    I am new ish to Java and I have downloaded some pre built code and I have read through it I saw that the author of the code had classes in one huge file so i decided to separate them into there own class file however it seems it has broken the original code and I am not sure how to fix it I was wondering if someone could take a look.

    for example the code shown below will not open the new dialog box
    Java Code:
     else if ( evt.getSource() == add ) {
             JOptionPane.showMessageDialog(null,
                     "1: Enter a unique item ID. \n" +
                     "2: Enter the item name and device type. \n" +
                     "3: Enter:  \n" +
                     "- Current Stock \n" +
                     "- Price (GBP) \n" +
                     "4: Then press enter.\n",
                     "Add Record",
                     JOptionPane.INFORMATION_MESSAGE) ;
                 newRec = new NewRec(tcpC,  table, pData);
    but it used to before I made the NewRec class into it's own class file this is the NewRec Class file code
    Java Code:
    package joe.kev.james;
    
     //Import Relavent Java Libaries
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    
    
    
    public class NewRec extends Dialog
            implements ActionListener {
       public JTextField recID, itemName, deviceType, currentStock,
                    price;
       public JLabel recIDLabel,  itemNameLabel, deviceTypeLabel,
                      currentStockLabel, priceLabel;
       public JButton cancel, save;
       //public Record data;
       public int recIDNum, toCont;
       public JTable table ;
       public JPanel addressPanel ;
       public String pData[] [] ;
       public boolean recExists = false ;
       public tcpClient tclient ;
       public int fileLength = 0, numEntries = 0;
       public boolean myDebug = false;
       public String columnNames[]  = {"Item ID", "Item Name",
           "Device Type",  "Current Stock", "Price (GBP)"} ;
      
      // public int ERROR = 1, iii = 0, numEntries = 0, fileLength = 0;
    
       /** *****************************************************************************
        * The NewRec() constructor is used to initialize the NewRec dialog.
        ***************************************************************************** */
       public NewRec( tcpClient t_client, JTable tab,
                            String p_Data[] []  )
       {
          super( new Frame(), "New Record", true );
    
    
          table = tab ;
          pData = p_Data ;
          tclient = t_client ;
          
          setSize( 400, 250 );
       }
    
       /** *****************************************************************************
        * Method: NewSetup() does the actual label, textfield, button
        * setup and declares the layout manager that is used. It
        * 1- sets the size of the dialog
        * 2- creates the text fields
        * 3- creates the labels
        * 4- creates the buttons
        * 5- adds the fields, labels, and buttons to the dialog
        *    context.
        * 6- Adds an event listener to the
        *    a- recID JTextfield
        *    b- save  JButton
        *    c- Cancel JButton
        ***************************************************************************** */
       public void  NewSetup() {
    
          setLayout( new GridLayout( 12, 2 ) );
    
          /** Create JTextFields */
          recID      = new JTextField( 10 );
          recID.setText("" + fileLength ) ;
          recID.enable( false );
          itemName  = new JTextField( 10 );
          deviceType   = new JTextField( 10 );
          currentStock    = new JTextField( 10 );
          price       = new JTextField( 10 );
    
          /** Create JLabels */
          recIDLabel      = new JLabel( "Item ID" );
          itemNameLabel  = new JLabel( "Item Name" );
          deviceTypeLabel   = new JLabel( "Device Type" );
          currentStockLabel    = new JLabel( "Current Stock" );
          priceLabel       = new JLabel( "Price (GBP)" );
    
          /** Create JButtons */
          save = new JButton( "Save Changes" );
          cancel = new JButton( "Cancel" );
    
          /** Add ActionListeners to the JButtons */
          recID.addActionListener( this );
          save.addActionListener( this );
          cancel.addActionListener( this );
    
          /** Add JLabels, JTextFields, and JButtons to the Dialog context */
          add( recIDLabel );
          add( recID );
          recID.setText( "" + ( getNumEntries()+1 ) );
          add( itemNameLabel );
          add( itemName );
          add( deviceTypeLabel );
          add( deviceType );
          add( currentStockLabel );
          add( currentStock );
          add( priceLabel );
          add( price );
          add( save );
          add( cancel );
    
          //c.add( addressPanel, BorderLayout.CENTER )  ;
       }
    
       /** *****************************************************************************
        * Method: actionPerformed() is the event handler that reesponds
        *         to the GUI events generated by the NewRecord dialog.
        ***************************************************************************** */
       public void actionPerformed( ActionEvent e )
       {
             if ( e.getSource() == cancel ) {
             NewClear();
          }
          else if ( e.getSource() == recID )  {
             if ( recID.getText().equals(null) || recIDNum <= 0 ||
                    recIDNum > 300   ) {
                JOptionPane.showMessageDialog(null,
                     "A recID entered was:  null or blank, or not between 0 and 300. which is invalid.\n" +
                     "Please enter a number greater than 0 and less than 300.", "RecID Entered",
                     JOptionPane.ERROR_MESSAGE) ;
             }
             else  {
                for ( int i = 0 ; i <= getNumEntries() ; i++ ) {
                   if ( Integer.parseInt( pData[ i  ] [ 0 ] ) == recIDNum ) {
                      recIDNum = i ;
                      JOptionPane.showMessageDialog(null,
                           "A recID entered " + recID.getText() + " already exists.",
                           "RecID Exists",
                           JOptionPane.ERROR_MESSAGE) ;
                      break ;
                   }
                }
             }
          }
          else if ( e.getSource() == save ) {
             System.out.println( "\n1a: Currrently in add() class actionPerformed() method and save construct." ) ;
             if ( recID.getText().equals("") ) {
                JOptionPane.showMessageDialog(null,
                     "A recID entered was:  null or blank, which is invalid.\n" +
                     "Please enter a number greater than 0 and less than 251.", "RecID Entered",
                     JOptionPane.INFORMATION_MESSAGE) ;
                return ;
             }
             else if ( (itemName.getText() != "") && (deviceType.getText() != "")
                  && (currentStock.getText()  != "")  ) {
                System.out.println( "\n1b: Currrently in add() class - checking for duplicate recID." ) ;
                System.out.println( "\n1ba: The value of getNumEntries is " + getNumEntries() ) ;
                recIDNum = Integer.parseInt( recID.getText() ) ;
                for ( int i = 0 ; i <= getNumEntries() ; i++ ) {
                   System.out.println( "\n1bb: Currrently in add() class - checking for duplicate recID. Index =s " + i + "and recIDNum =s " + recIDNum) ;
                   if ( Integer.parseInt( pData[ i  ] [ 0 ] ) == recIDNum ) {
                      recIDNum = i ;
                      recExists = true ;
                      break ;
                   }
                }
                System.out.println( "\n1c: Currrently in add() class actionPerformed() method- the value recExists of is " + recExists) ;
                if ( recExists ) {
                   JOptionPane.showMessageDialog(null,
                     "A recID entered " + recID.getText() + " already exists. \nPlease enter a unique number.", "RecID Exists",
                     JOptionPane.ERROR_MESSAGE) ;
                   recExists = false ;
                }
                else {
                   System.out.println( "\n1d: Currrently in add() class actionPerformed() method - getting data for add." ) ;
                   recIDNum = getNumEntries() + 1 ;
                   pData[ recIDNum  ] [ 0 ] =  recID.getText()  ;
                   sysPrint("A new record is being added at " +
                         pData[ recIDNum  ] [ 0 ]   );
    
                   pData[ recIDNum  ] [ 1 ] =  itemName.getText().trim() ;
                   pData[ recIDNum  ] [ 2 ] =  deviceType.getText().trim() ;
                   pData[ recIDNum  ] [ 3 ] =  currentStock.getText().trim()  ;
                   pData[ recIDNum  ] [ 4 ] =  price.getText().trim() ;
    
                   table = new JTable( pData, columnNames );
                   this.repaint();
                   setEntries( getEntries() + 1 );
                   sendData( "Add;; " + pData[ recIDNum  ] [ 0 ] + ";; "
                               + pData[ recIDNum  ] [ 1 ] + ";; "
                               + pData[ recIDNum  ] [ 2 ] + ";; "
                               + pData[ recIDNum  ] [ 3 ] + ";; "
                               + pData[ recIDNum  ] [ 4 ] + ";;;" ) ;
    
                   toCont = JOptionPane.showConfirmDialog(null,
                       "Do you want to add another record? \nChoose one",
                       "Choose one",
                       JOptionPane.YES_NO_OPTION);
    
                   if ( toCont == JOptionPane.YES_OPTION  )  {
                      recID.setText( "" );
                      itemName.setText( ""  );
                      deviceType.setText( ""  );
                      currentStock.setText( ""  );
                      price.setText( ""  );
                   }
                   else {
                      NewClear();
                   }
                }
             }
    
          }
    
       }
    
       /** *****************************************************************************
        *  Method: newClear() is used to cleanup and exit the NewRecord dialog.
        ***************************************************************************** */
       public void NewClear()
       {
          System.out.println( "\n1e: Currrently in add() class clear() method." ) ;
          setVisible( false );
    
    }
    }
    I would be willing to pay somone to fix this for me as I need this to work

    Joe

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I'll do it. But it'll cost you $1000.

  3. #3
    jdmoore1 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    I'll do it. But it'll cost you $1000.
    Thanks dude:eek: but I sorted the problem if was an oversight on my behalf I forgot to add this code
    Java Code:
    setVisible( true );
    silly as when I look back but it was late at night.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Hehe, I was just giving you a hard time. So, any luck then?

  5. #5
    jdmoore1 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Hey dude, It works ish the form displays now which is good however not all the methods work now that I have separated the classes from one big file into separate files. I am a student doing it as a project you see so I am hitting my head against a brick wall on this as I dont know whats up with the code?

    are you able to look dude if I sent the code in a zip to you as it is too big to post all the code on this forum.

    Joe

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    In the original source, were the classes nested, or were they just in the same file? Nested classes require extra work to make them external.

  7. #7
    jdmoore1 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    quad64bit, yes they where nested

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Ok, so java has some funny behavior with nested classes involving scope. When you nest a class, it has access to all members of the parent class by default. So if you pull it out of the parent class, it will likely break totally, since all the variables were referenced directly. You have a couple choices:
    1) You could leave it nested, and re-evaluate what it is you are trying to do (I suggest this)
    2) You could pull it out and refactor the hell out of the code, and add accessor methods to everything.

    Nesting a class makes sense when the class will only be used in the context of the parent class. It does not make sense however when the nested class will be used to any large extent outside of the parent class.

    A good example of proper nested class use might be the implementation of an interface - for instance an action listener, or an internal data structure like a Node class inside a Tree class. The nodes are only used in the context of the tree, therefor it makes sense to nest them.

  9. #9
    jdmoore1 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    quad64bit,

    Thanks dude, I shall leave it as it is with the classes nested, as now I understand what nested classes are I can see why the author left them in :)

    Joe

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

Similar Threads

  1. Replies: 4
    Last Post: 01-18-2011, 09:22 AM
  2. how to open one frame to another frame
    By tukadiya in forum AWT / Swing
    Replies: 1
    Last Post: 12-20-2010, 09:27 PM
  3. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 11:40 AM
  4. Replies: 7
    Last Post: 11-02-2009, 07:31 PM
  5. Frame problems
    By gary in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2007, 02:21 PM

Posting Permissions

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