Results 1 to 10 of 10
Thread: Problems with new Frame
- 02-28-2011, 08:57 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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
but it used to before I made the NewRec class into it's own class file this is the NewRec Class file codeJava 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);
I would be willing to pay somone to fix this for me as I need this to workJava 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 ); } }
Joe
- 03-01-2011, 01:35 AM #2
I'll do it. But it'll cost you $1000.
- 03-01-2011, 02:20 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
- 03-01-2011, 04:36 PM #4
Hehe, I was just giving you a hard time. So, any luck then?
- 03-01-2011, 04:42 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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
- 03-01-2011, 05:03 PM #6
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.
- 03-01-2011, 05:05 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
quad64bit, yes they where nested
- 03-01-2011, 05:11 PM #8
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.
- 03-01-2011, 06:16 PM #9
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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
- 03-01-2011, 06:17 PM #10
Similar Threads
-
How to set Jpanel in the center of frame when Increase the size of frame
By justbeller in forum AWT / SwingReplies: 4Last Post: 01-18-2011, 08:22 AM -
how to open one frame to another frame
By tukadiya in forum AWT / SwingReplies: 1Last Post: 12-20-2010, 08:27 PM -
Java slave Frame access to its owner main frame problem
By cagdaseckin in forum New To JavaReplies: 0Last Post: 12-10-2010, 10:40 AM -
Returning focus to a frame after hiding another frame
By fletcher in forum AWT / SwingReplies: 7Last Post: 11-02-2009, 06:31 PM -
Frame problems
By gary in forum AWT / SwingReplies: 2Last Post: 06-20-2007, 01:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks