Results 1 to 18 of 18
  1. #1
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default pls Help to create an on screen keyboard

    hi guys..

    As a part of college project i have asked to create a bank simulator in java eclipse. But the inputs are supposed to be typed in by the user from an onscreen keyboard. i tried a lot to create an onscreen keyboard. in fact i succeeded in creating one. But i dont know how to the connect the keyboard and the textboxes to which user enters. in my program when my button clicks nothing happens... pls help me..

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: pls Help to create an on screen keyboard

    Can you show the relevant bit of code for the button click?
    Please make sure you wrap the code in code tags so it keeps its formatting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Tolls View Post
    Can you show the relevant bit of code for the button click?
    Please make sure you wrap the code in code tags so it keeps its formatting.
    This my program...

    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    import java.io.PrintWriter;
    import java.io.PrintWriter;
    import java.io.File;
    
    
    public class Java1 extends JFrame implements ActionListener
    {
    	
    	
    	JButton MyButton = new JButton("Show Keybaord");
    
    	JTextField txt1 = new JTextField(10);
    	JPanel jf = new JPanel();
        
    public static void main(String[] args)
    		{
    			Java1 bm = new Java1();
    		}
    		
    public Java1()
    {
    	
    setLayout(null);
    setSize(850,700);
    setTitle("Bank Simulator");
    MyButton.setBounds(550,200,150,100);
    add(MyButton);
    MyButton.addActionListener(this);
    txt1.setBounds(350, 150, 150, 100);
    add(txt1);
    txt1.setEnabled(true);
    setVisible(true);
    
    
    }
    public void actionPerformed(ActionEvent e)
    {
    
    	
    	
    	if (e.getSource()== MyButton)
    	{
    	
    	jf.add(txt1, BorderLayout.SOUTH);
    	
    	
    	 
    	   String nos = "1234567890";
    	   String alphabet = "abcdefghij";
    	   String alphabet1 = "klmnopqrs";
    	   String alphabet2 = "tuvwxyz";
    	   
    	   JFrame myFrame = new JFrame();
    	   myFrame.setSize(586, 205);
    	   JPanel myPanel = new JPanel();
    	   JPanel myPanel1 = new JPanel();
    	   JPanel myPanel2 = new JPanel();
    	   JPanel myPanel3 = new JPanel();
    	   JButton[] btn1 = new JButton[10];
    	   JButton[] btn2 = new JButton[10];
    	   JButton[] btn3 = new JButton[9];
    	   JButton[] btn4 = new JButton[7];
    	   
    	   
    	   for (int i =0; i< nos.length(); i++)
    	    { 
    		   btn1[i] = new JButton(nos.substring(i, i + 1));
    		   myPanel.add(btn1[i]);
    		   btn1[i].addActionListener(this);
    		   
    		   
    		   
    	    }
           
    
    	    for (int i = 0; i < alphabet.length(); i++) {
    
    	        btn2[i] = new JButton(alphabet.substring(i, i + 1));
    	        myPanel1.add(btn2[i]);
    	       
    	    }
    	    for(int i = 0; i < alphabet1.length();i++){
    	    	 btn3[i] = new JButton(alphabet1.substring(i, i + 1));
    	    	 myPanel2.add(btn3[i]);
    	    }
    	    
    	    for (int i = 0; i < alphabet2.length(); i++){
    	    	
    	    	btn4[i] = new JButton(alphabet2.substring(i, i + 1));
    	    	myPanel3.add(btn4[i]);
    	    }
    	    JPanel outer = new JPanel();
    	    outer.add(myPanel);
    	    outer.add(myPanel1);
    	    outer.add(myPanel2);
    	    outer.add(myPanel3);
    	    myFrame.add(outer);
    	    myFrame.setVisible(true);
    	   
    	    
    	    for(int i=0;i<btn1.length;i++){
    	        if(e.getSource()==btn1[i]){
    	        	String alreadyDisplayed = txt1.getText(); //get the existing text
    	        	String toDisplay = alreadyDisplayed + Integer.toString(i, i+1);// append the position to the text
    	        	txt1.setText(toDisplay);// display the text  
    //insert above code here;
    	        }
    	    }   
    	    
    	    
    	}
    	
    	
    	    
    	    
    }
    	
    }
    Last edited by Tolls; 03-21-2017 at 09:58 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: pls Help to create an on screen keyboard

    I did ask you to wrap the code in code tags.
    I've done this for you now, but please remember to do it in the future.

    Your issue is you are trying to share a single ActionListener among all the buttons.
    That won't work well.

    Create one method, showKeyboard(), which is called by the action listener on the original button.
    Don't make the main class implement ActionListener, use a lambda:
    Java Code:
    MyButton.addActionListener(e -> showKeyboard());
    If you've not done lambda expressions yet, use an anonymous listener:
    Java Code:
            MyButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showKeyboard();
                }
            });
    That gets your keyboard side out of the way.
    Now, in the keyboard, each button should have it's own listener (or lambda) which calls a keyboardButtonPressed(String character) method, which will process the character.

    Java Code:
    <yourKeyButton>.addActionListener(...
        keyboardButtonPressed(<the character for the button>);
    ...);
    The '....' will vary depending on whether you can use lambdas or not.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Tolls View Post
    I did ask you to wrap the code in code tags.
    I've done this for you now, but please remember to do it in the future.

    Your issue is you are trying to share a single ActionListener among all the buttons.
    That won't work well.

    Create one method, showKeyboard(), which is called by the action listener on the original button.
    Don't make the main class implement ActionListener, use a lambda:
    Java Code:
    MyButton.addActionListener(e -> showKeyboard());
    If you've not done lambda expressions yet, use an anonymous listener:
    Java Code:
            MyButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showKeyboard();
                }
            });
    That gets your keyboard side out of the way.
    Now, in the keyboard, each button should have it's own listener (or lambda) which calls a keyboardButtonPressed(String character) method, which will process the character.

    Java Code:
    <yourKeyButton>.addActionListener(...
        keyboardButtonPressed(<the character for the button>);
    ...);
    The '....' will vary depending on whether you can use lambdas or not.
    sorry for the late reply..i had been working on this... so the first things u told me ..i did... i created a method and added the action listener as u told me.. so for theres no errors. but the next one.. to add action listeners to the buttons on the keyboard i didnt get it.. i am like a begineer.. the lectures here in my classes are a real mess .. so pls help me on this..

    so do i need to add action listener for the all the buttons inside showkeyboard method itself or anywhere else.. as u can see i have created 4 button arrays and have added those to panels.. and the 4 panels to a single new frame. the next prob is i cant direct the text to a single textbox.. bcos i have several textboxes to be filled... so first i will have to add action listeners to all the buttons out there..how.. can u use the same variables on my program so that it will be easy for me.. sorry for puttin u in a messs...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: pls Help to create an on screen keyboard

    This is what you have at the moment for your number buttons:
    Java Code:
    for (int i =0; i< nos.length(); i++)
    { 
       btn1[i] = new JButton(nos.substring(i, i + 1));
       myPanel.add(btn1[i]);
       btn1[i].addActionListener(this);
    }
    You want to change that call to addActionListener so it uses your new keyboardButtonPressed method, passing in the character that has been pressed.
    You have that value already as you just set the label for the JButton to it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Tolls View Post
    This is what you have at the moment for your number buttons:
    Java Code:
    for (int i =0; i< nos.length(); i++)
    { 
       btn1[i] = new JButton(nos.substring(i, i + 1));
       myPanel.add(btn1[i]);
       btn1[i].addActionListener(this);
    }
    You want to change that call to addActionListener so it uses your new keyboardButtonPressed method, passing in the character that has been pressed.
    You have that value already as you just set the label for the JButton to it.
    for (int i =0; i< nos.length(); i++)
    {
    btn1[i] = new JButton(nos.substring(i, i + 1));
    myPanel.add(btn1[i]);
    btn1[i].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    String keyboardButtonPressed = nos.substring(i, i + 1);
    }
    });

    this is the way i did for the first array of buttons...is it right ??

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: pls Help to create an on screen keyboard

    Well, is that calling a method inside the actionPerformed to do the work?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Tolls View Post
    Well, is that calling a method inside the actionPerformed to do the work?
    i think i got ur point.. for a demo i did this..
    for (int i =0; i< nos.length(); i++)
    {
    btn1[i] = new JButton(nos.substring(i, i + 1));
    myPanel.add(btn1[i]);
    btn1[i].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    keyboardButtonPressed();
    }

    });



    the char will be passed in the brackets of the functions...right.. but now where do i need to create the method..is it inside the keyboard or outside.. and how to create it.. i tried
    public String keyboardButtonPressed(char a)
    {
    String b =

    }

    is it like this..i am doing this outside the keyboard() method.. and what command should i include after the string b command..how to extract the value of button clicked..

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: pls Help to create an on screen keyboard

    Outside the keyboard part, so the method is part of your main class.

    That method will then do something like:
    Java Code:
    public String keyboardButtonPressed(char a)
    {
        // append the character to the text field.
        // Note, you may need to be using a String instead of a char.
        // I'm not sure about that bit!
    }
    For the appending bit you might (not sure) need to get the current text then append the character to it and reset the text?
    I don't think there's actually an append method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Tolls View Post
    Outside the keyboard part, so the method is part of your main class.

    That method will then do something like:
    Java Code:
    public String keyboardButtonPressed(char a)
    {
        // append the character to the text field.
        // Note, you may need to be using a String instead of a char.
        // I'm not sure about that bit!
    }
    For the appending bit you might (not sure) need to get the current text then append the character to it and reset the text?
    I don't think there's actually an append method.
    the appending is fine..i think i did it..but how to get the value of that button
    public String keyboardButtonPressed(char a)
    {

    String alreadyDisplayed = username.getText();
    String toDisplay = alreadyDisplayed +

    }

    so after the '+' sign the value of button should be added...right...? but how... is there a function for that..and other problem is when i try to access the array in the method is shows an error..is it bcos its out of focus..

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,006
    Rep Power
    34

    Default Re: pls Help to create an on screen keyboard

    shows an error
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    the error that shows is : btn1 cannot be resolved to a variable
    here by i am providing the whole code i did..

    Java Code:
    
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    import java.io.PrintWriter;
    import java.io.File;
    
    
    public class BankSimulator extends JFrame implements ActionListener
    
    
    {
    
    	
    	JTextField username = new JTextField(10);
    	JTextField password = new JTextField(10);
    	JTextField name = new JTextField(10);
    	JButton logbtn = new JButton("Log In");
    	JButton regbtn = new JButton("Register");
    	JButton showkeyboard = new JButton("showkeyboard");
    	
    	
    	
    	public static void main(String[] args)
    	{
    		BankSimulator bm = new BankSimulator();
    		
    	}
    	
    public BankSimulator()
    
    {
    	setLayout(null);
    	setSize(850,700);
    	setTitle("Bank Simulator");
    	username.setBounds(350, 150, 130, 20);
    	add(username);
    	password.setBounds(350,180,130,20);
    	add(password);
    
    	logbtn.setBounds(341,220,150,30);
    	add(logbtn);
    	logbtn.addActionListener(this);
    	regbtn.setBounds(341,255,150,30);
    	add(regbtn);
    	regbtn.addActionListener(this);
    	showkeyboard.setBounds(341, 270, 150, 30);
    	add(showkeyboard);
    	showkeyboard.addActionListener(new ActionListener()
    	{
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			showkeyboard();
    		}
    		
    	});
    	setVisible(true);
    	
    }
    
    public void showkeyboard()
    {
    	
    	   String nos = "1234567890";
    	   String alphabet = "abcdefghij";
    	   String alphabet1 = "klmnopqrs";
    	   String alphabet2 = "tuvwxyz";
    	   
    	   JFrame myFrame = new JFrame();
    	   myFrame.setSize(586, 205);
    	   JPanel myPanel = new JPanel();
    	   JPanel myPanel1 = new JPanel();
    	   JPanel myPanel2 = new JPanel();
    	   JPanel myPanel3 = new JPanel();
    	   JButton[] btn1 = new JButton[10];
    	   JButton[] btn2 = new JButton[10];
    	   JButton[] btn3 = new JButton[9];
    	   JButton[] btn4 = new JButton[7];
    	   
    	   
    	   for (int i =0; i< nos.length(); i++)
    	    { 
    		   btn1[i] = new JButton(nos.substring(i, i + 1));
    		   myPanel.add(btn1[i]);
    		   btn1[i].addActionListener(new ActionListener(){
    			   public void actionPerformed(ActionEvent e) 
    			   {
    				 keyboardButtonPressed();  
    			   }
    			   
    	    });
        
    
    	    for (int i = 0; i < alphabet.length(); i++) {
    
    	        btn2[i] = new JButton(alphabet.substring(i, i + 1));
    	        myPanel1.add(btn2[i]);
    	       
    	    }
    	    for(int i = 0; i < alphabet1.length();i++){
    	    	 btn3[i] = new JButton(alphabet1.substring(i, i + 1));
    	    	 myPanel2.add(btn3[i]);
    	    }
    	    
    	    for (int i = 0; i < alphabet2.length(); i++){
    	    	
    	    	btn4[i] = new JButton(alphabet2.substring(i, i + 1));
    	    	myPanel3.add(btn4[i]);
    	    }
    	    JPanel outer = new JPanel();
    	    outer.add(myPanel);
    	    outer.add(myPanel1);
    	    outer.add(myPanel2);
    	    outer.add(myPanel3);
    	    myFrame.add(outer);
    	    myFrame.setVisible(true);
    	   
    	    
    	   
    	    }   
    	    
    	    
    }
    	   
    public String keyboardButtonPressed(char a)
    {
    		
    	String alreadyDisplayed = username.getText();
    	String toDisplay = alreadyDisplayed + btn1[i].value;
    	
    }
    
    public void actionPerformed(ActionEvent e)2
    {
    	
    	if (e.getSource()== showkeyboard)
    	{
    		
    		
    	}
    	
    	
    }
    
    }

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,006
    Rep Power
    34

    Default Re: pls Help to create an on screen keyboard

    btn1 cannot be resolved to a variable
    The source line was left off the error message. What line was the error on?

    It means the compiler can not find a definition for the variable: btn1 that is in scope where it was referenced. Make sure the definition for btn1 is in scope for all the places it is used.

    Note: The ActionEvent object passed to the listener method has a reference to the object that caused the event. There is no need to do a lookup in an array.
    Last edited by Norm; 03-21-2017 at 10:08 PM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Norm View Post
    The source line was left off the error message. What line was the error on?

    It means the compiler can not find a definition for the variable: btn1 that is in scope where it was referenced. Make sure the definition for btn1 is in scope for all the places it is used.

    Note: The ActionEvent object passed to the listener method has a reference to the object that caused the event. There is no need to do a lookup in an array.
    The error i got is in line no 130...actually i really dont know how to extract the value of the button so that it will be displayed on th textbox..pls help..

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,006
    Rep Power
    34

    Default Re: pls Help to create an on screen keyboard

    how to extract the value of the button
    Look at the API doc for the button's class to see what methods it has that will return the value you want. Also look at the API doc for the classes that the button class extends. They follow the methods for the button class in the Methods inherited from ... section.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Bilal is offline Member
    Join Date
    Mar 2017
    Posts
    11
    Rep Power
    0

    Default Re: pls Help to create an on screen keyboard

    Quote Originally Posted by Norm View Post
    Look at the API doc for the button's class to see what methods it has that will return the value you want. Also look at the API doc for the classes that the button class extends. They follow the methods for the button class in the Methods inherited from ... section.
    i think i can use the the gettext function...below is the code i did..
    Java Code:
    public String keyboardButtonPressed(char a)
    {
    		
    	String alreadyDisplayed = username.getText();
    	String toDisplay = alreadyDisplayed + btn1[i].getText();
    	
    }
    but i am getting an error were i use the btn1[i].getText().. its saying 'i' cannot be resolved to a variable...

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: pls Help to create an on screen keyboard

    Your method is declared as accepting a char.
    That char is the value of the button.
    So this call:
    Java Code:
           for (int i =0; i< nos.length(); i++)
            { 
               btn1[i] = new JButton(nos.substring(i, i + 1));
               myPanel.add(btn1[i]);
               btn1[i].addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e) 
                   {
                     keyboardButtonPressed();  
                   }
                    
            });
    Needs to change to pass in the text for the button, which you havealready calculated here:
    Java Code:
    nos.substring(i, i + 1)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. On Screen Keyboard help
    By XxClouDxx in forum New To Java
    Replies: 3
    Last Post: 10-02-2013, 01:14 PM
  2. embed link to on-screen keyboard
    By nknigi in forum New To Java
    Replies: 2
    Last Post: 03-02-2013, 09:30 AM
  3. Printing keyboard input to screen.
    By adwodon in forum New To Java
    Replies: 4
    Last Post: 12-16-2010, 10:40 AM
  4. Help with on screen keyboard Capslock
    By linkinlog0123 in forum AWT / Swing
    Replies: 2
    Last Post: 11-17-2010, 05:46 AM
  5. Need help with on-screen keyboard (first post)
    By tom_k in forum New To Java
    Replies: 7
    Last Post: 07-31-2008, 02:07 AM

Tags for this Thread

Posting Permissions

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