Results 1 to 6 of 6
  1. #1
    idlackage is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default 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.

    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()) );
          }
          
        }
      }
      
    }
    Thanks for any help.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Java Code:
    public TransformWindow( PaintCanvas canvas )
    looks suspicious, never ever mix AWT and Swing!

    Your JTextField References are all null, so your check for
    Java Code:
    event.getSource()
    will always fail. Remember that Java is pass by value.
    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

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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
      }
    As PhHein says, Java is pass by value.
    That is, that "new JTextField()" call is not doing anything to the y2_textField variable, which will still be pointing at null.

  4. #4
    idlackage is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    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?

    looks suspicious, never ever mix AWT and Swing!
    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?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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) );

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by idlackage View Post
    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?
    Hard to tell, I don't know what a PaintCanvas is. Mixing "heavyweight and lightweight components" causes some strange GUI effects, search on Google, there are some articles about it.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Text-field templates.
    By jdipierro in forum New To Java
    Replies: 4
    Last Post: 05-14-2010, 12:48 AM
  2. Refreshing Text Field Value
    By nehakuls in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-13-2009, 11:05 AM
  3. How To add the Text field to the TextArea or TextPane
    By Chintan Patel in forum AWT / Swing
    Replies: 3
    Last Post: 04-08-2009, 02:46 AM
  4. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  5. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2008, 11:01 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
  •