Results 1 to 6 of 6
- 06-07-2010, 01:45 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
Text field does not realize its being called
I'm not sure if this is because I'm initializing the textfields inside a method instead of the constructor, but in any case I cannot get anything to be printed out no matter which field I type into.
Thanks for any help.Java Code:public class TransformWindow extends JFrame { private JTextField x1_textField; private JTextField x2_textField; private JTextField y1_textField; private JTextField y2_textField; private Shape shape[]; private int shapeX1; private int shapeX2; private int shapeY1; private int shapeY2; private PaintCanvas canvas; private TextHandler handler; public TransformWindow( PaintCanvas canvas ) { super( "Tranform shape" ); setLayout( new GridLayout(2,2,10,5) ); this.canvas = canvas; shape = canvas.getShape(); shapeX1 = shape[0].getX1(); shapeX2 = shape[0].getX2(); shapeY1 = shape[0].getY1(); shapeY2 = shape[0].getY2(); newPanel( "x1", x1_textField, Integer.toString(shapeX1) ); newPanel( "x2", x2_textField, Integer.toString(shapeX2) ); newPanel( "y1", y1_textField, Integer.toString(shapeY1) ); newPanel( "y2", y2_textField, Integer.toString(shapeY2) ); canvas.setShape( shape ); handler = new TextHandler(); } private void newPanel( String text, JTextField textField, String coords ) { JLabel label = new JLabel( text ); textField = new JTextField(); JPanel panel = new JPanel(); panel.setLayout( new BorderLayout(5,10) ); panel.add( label, BorderLayout.WEST ); panel.add( textField, BorderLayout.CENTER ); textField.addActionListener( handler ); textField.setText( coords ); add( panel ); } // Private inner class for text event handling private class TextHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { // Determine the text field in which values were inputted if( event.getSource() == x1_textField ) { shape[0].setX1( Integer.parseInt(x1_textField.getText()) ); System.out.println( "test output string" ); } else if( event.getSource() == x2_textField ) { shape[0].setX2( Integer.parseInt(event.getActionCommand()) ); } else if( event.getSource() == y1_textField ) { shape[0].setY1( Integer.parseInt(event.getActionCommand()) ); } else if( event.getSource() == y2_textField ) { shape[0].setY2( Integer.parseInt(event.getActionCommand()) ); } } } }
- 06-07-2010, 02:16 PM #2
looks suspicious, never ever mix AWT and Swing!Java Code:public TransformWindow( PaintCanvas canvas )
Your JTextField References are all null, so your check forwill always fail. Remember that Java is pass by value.Java Code:event.getSource()
Last edited by PhHein; 06-07-2010 at 02:24 PM.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 06-07-2010, 04:42 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
As PhHein says, Java is pass by value.Java Code:// Your call to newPanel. newPanel( "y2", y2_textField, Integer.toString(shapeY2) ); // newPanel() itself private void newPanel( String text, JTextField textField, String coords ) { // do stuff textField = new JTextField(); // do more stuff }
That is, that "new JTextField()" call is not doing anything to the y2_textField variable, which will still be pointing at null.
- 06-07-2010, 05:11 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
Thanks to both for your replies! I moved every command regarding the textfields to the constructor and it worked, but is there a more efficient way to go about this?
This was never talked about in class so I have no idea why this is. Is it bad form like using a switch statement, or just something wrong?looks suspicious, never ever mix AWT and Swing!
- 06-07-2010, 05:14 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You could still use that method, but initialise the JTextField beforehand, and not in the method.
So:
Java Code:y2_textField = new JTextField(); newPanel( "y2", y2_textField, Integer.toString(shapeY2) );
- 06-07-2010, 05:19 PM #6
Similar Threads
-
Text-field templates.
By jdipierro in forum New To JavaReplies: 4Last Post: 05-14-2010, 12:48 AM -
Refreshing Text Field Value
By nehakuls in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 11-13-2009, 11:05 AM -
How To add the Text field to the TextArea or TextPane
By Chintan Patel in forum AWT / SwingReplies: 3Last Post: 04-08-2009, 02:46 AM -
get numeric value from a text field
By Lehane_9 in forum New To JavaReplies: 2Last Post: 06-14-2008, 03:19 AM -
Regarding Text Field
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-05-2008, 11:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks