Results 1 to 7 of 7
Like Tree3Likes
  • 2 Post By Tolls
  • 1 Post By gimbal2

Thread: Why no output in the second TextField?Simple Java Program

  1. #1
    soche123 is offline Member
    Join Date
    Jun 2014
    Posts
    1
    Rep Power
    0

    Default Why no output in the second TextField?Simple Java Program

    I've done a lot of hardwork for this assignment but I don't know what's why the second textfield which is for output is blank.

    All I want is to get the input from user in textfield a and display it in textfield b in lower case.

    public void KeyPressed (KeyEvent ke) {
    String letter2="";
    if(ke.getKeyCode()==KeyEvent.VK_A)
    {
    letter2=letter2+"a";

    btextfield.setText(letter2);
    label3.setText(letter2);
    }
    }

    This is the whole code of my program.
    Java Code:
    import javax.swing.JFrame;
    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    
    class cls extends JFrame implements KeyListener
    { 
     JLabel label2; 
    JLabel label1; 
    JTextField atextfield; 
    JTextField btextfield; 
    JButton b; 
     JLabel label3;
     
    
    int count, length; 
    
    public cls() { 
    
    label1=new JLabel();
    label1.setText("Input");
    
    label2=new JLabel();
    label2.setText("OutPut");
     label3=new JLabel();
     
      
    setLayout(new FlowLayout()); 
     
    atextfield = new JTextField(7); 
    btextfield = new JTextField(7); 
    b = new JButton("Convert"); 
    
     add(label1);
    add(atextfield); 
     add(label2);
    add(btextfield); 
    add(b); 
    add(label3);
     
    b.addKeyListener(this); 
    atextfield.addKeyListener(this); 
    
     
    } 
    
     
    public void KeyPressed (KeyEvent ke) { 
    String letter2=""; 
    if(ke.getKeyCode()==KeyEvent.VK_A)
    {
    letter2=letter2+"a";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_B)
    {
    letter2=letter2+"b";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_C)
    {
    letter2=letter2+"c";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_D)
    {
    letter2=letter2+"d";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_E)
    {
    letter2=letter2+"e";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_F)
    {
    letter2=letter2+"f";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_G)
    {
    letter2=letter2+"g";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_H)
    {
    letter2=letter2+"h";
    }
    if(ke.getKeyCode()==KeyEvent.VK_J)
    {
    letter2=letter2+"j";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_K)
    {
    letter2=letter2+"k";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_L)
    {
    letter2=letter2+"l";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_M)
    {
    letter2=letter2+"m";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_I)
    {
    letter2=letter2+"i";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_M)
    {
    letter2=letter2+"m";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_N)
    {
    letter2=letter2+"n";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_O)
    {
    letter2=letter2+"o";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_P)
    {
    letter2=letter2+"p";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_Q)
    {
    letter2=letter2+"q";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_R)
    {
    letter2=letter2+"r";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_V)
    {
    letter2=letter2+"v";
    }
    else if(ke.getKeyCode()==KeyEvent.VK_Z)
    {
    letter2=letter2+"z";
    
    }
    else if(ke.getKeyCode()==KeyEvent.VK_W)
    {
    letter2=letter2+"w";
    }
    btextfield.setText(letter2); 
    label3.setText(letter2);
    
    }
    
    public void keyPressed( KeyEvent e ) { }
    public void keyReleased( KeyEvent e ) { }
    public void keyTyped( KeyEvent e ) {}
    
    
    
    
     
    public static void main(String[] args) { 
    cls obj=new cls();
     
       obj.setSize(400,300);
       obj.setVisible(true);
       obj.setTitle("Assignment coded by Nadeem");
       obj.setDefaultCloseOperation(obj.EXIT_ON_CLOSE);
       }
       }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Why no output in the second TextField?Simple Java Program

    Well, you haven't said what the problem is. But I would suggest you focus on just taking in some text in a text field and when you are done, display it on the console. Once you get that working you can focus on the next part of the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Why no output in the second TextField?Simple Java Program

    My understsnding: when a key is pressed, keyPressed is called. But that method is empty.
    Your method is called KeyPressed which is different.

    Konrad

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Why no output in the second TextField?Simple Java Program

    This is why it's good practice to annotate any methods you are overriding with @Override.
    The compiler will then inform you if you have got something wrong with the method signature.
    gimbal2 and kneitzel like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Why no output in the second TextField?Simple Java Program

    Hi Tolls,

    I fear that this wouldn't help because he is not overriding methods of a superclass. He is implementing methods of an interface.

    But coding guidelines and their enforcement would help. A method simply starts lower case all the time.

    With kind regards,

    Konrad

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Why no output in the second TextField?Simple Java Program

    Quote Originally Posted by kneitzel View Post
    Hi Tolls,

    I fear that this wouldn't help because he is not overriding methods of a superclass. He is implementing methods of an interface.
    You still use @Override.
    kneitzel likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Why no output in the second TextField?Simple Java Program

    Thank you Tolls and gimbal2! I missed that completely so far.

    So another thing that I have to adjust in my code now :)

    With kind regards,

    Konrad

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  2. Output for the following Java Program
    By dhirajb1989 in forum New To Java
    Replies: 2
    Last Post: 09-09-2013, 05:17 AM
  3. Need Help With Simple Input Output Program
    By Mastermime in forum New To Java
    Replies: 12
    Last Post: 12-18-2012, 03:49 AM
  4. Replies: 5
    Last Post: 08-03-2012, 10:47 AM
  5. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 05:06 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
  •