Hi! I'm a student and I have a project in Java and its title is Virtual Keyboard....can you please help me on how to create that virtual keyboard pls...and also can you please leave your e-mail add so that i can chat with you....thanks a lot! :lol:

Dani AI

Generated

A practical, maintainable approach is to treat the virtual keyboard as a small MVC system: a data model (rows of key labels and modifiers), a view (panels laid out as keyboard rows), and a controller (listeners that send characters to the current text target). Generating key components from arrays keeps the UI code short and makes it easy to add alternate layouts (numeric, international) later. Use Swing layout managers (GridLayout/GridBag/BoxLayout) to arrange rows and groups instead of absolute positioning. (docs.oracle.com)

Make each key a small component with an ActionListener that inserts its label into the currently focused text component; get the focus owner with the KeyboardFocusManager and call the text component API to insert text. Example pattern (illustrative, not complete):

String[] keys = {"a","b","c"};
JButton[] btns = new JButton[keys.length];
for (int i = 0; i < keys.length; i++) {
    final String ch = keys[i];
    btns[i] = new JButton(ch);
    btns[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
            if (focus instanceof javax.swing.text.JTextComponent) {
                ((javax.swing.text.JTextComponent)focus).replaceSelection(ch);
            } else {
                // fallback if target isn't a Swing text component
            }
        }
    });
    panel.add(btns[i]);
}

Use the text component API rather than trying to synthesize KeyEvents when the target is inside the same JVM—replaceSelection is the correct, thread-safe insert method. Also use KeyboardFocusManager to find the focus owner. (docs.oracle.com)

If the keyboard must send input to other applications, use java.awt.Robot to post native key events, but be aware of platform differences, VK mapping limits, and possible privilege restrictions (Robot can throw AWTException on unsupported platforms). Mapping Unicode to VK_* keycodes can become complex; test on every target OS. (docs.oracle.com)

Practical tips: keep key components non-focusable (so clicks don’t steal the caret), centralize modifier state (Shift/Caps/AltGr) in the model, and unit-test different layouts. The strategy outlined here builds on the quick suggestions from and while adding a reliable insertion technique and a safe fallback for cross-application input. (docs.oracle.com)

Recommended Answers

All 12 Replies

JButtons and KeyListeners.

JButtons and KeyListeners.

slap some jlabels on them, then put them on a jframe and some jpanels. post your code here and we'll point at out screens and say that should stay... that should go... and so on.

I recommend using arrays for any component type you choose. That will make the program very short and more readable. Otherwise you will have hundreds of lines of declarations.

thank you! ill post to you my work soon....will you help me?

Sure there will be lots of experts helping you if you could post the program you have done.

Better yet if you come up with more specific question in the prorgam ratheer than posting the whole program.

---------------------

hmm, seems some school has assigned a class full of kids this assignment. I see it popping up on several forums all over the net, and all at about the same time.
All the same question, and pretty much the same attitude ("do my work for me").

Agreed jwenting.....agreed bigtime!

hmm, seems some school has assigned a class full of kids this assignment. I see it popping up on several forums all over the net, and all at about the same time.
All the same question, and pretty much the same attitude ("do my work for me").

what do you mean? :(

He means (and I don't think his comment was directly targeted at you, more toward the new generation of CS and IS college students...) that there are many students that just post their assignment on a forum and expect people to do their work for them. And, when that work isn't completed for them in a very small amount of time the tendency of the student seems to be rude to the rest of the forum.

Programming helps those who help themselves.

Regards,

Nate

Programming helps those who help themselves.

Very correct. Practice makes perfect and it is very much applicable to programming.

------------

hi phaelax

He means (and I don't think his comment was directly targeted at you, more toward the new generation of CS and IS college students...) that there are many students that just post their assignment on a forum and expect people to do their work for them. And, when that work isn't completed for them in a very small amount of time the tendency of the student seems to be rude to the rest of the forum.

Programming helps those who help themselves.

Regards,

Nate

thanks....sorry maybe i dont get what he mean for that...well, anyway i already start my project and sooner i'll post it here so that you'll check it if it's okei....ryt? thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.