Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: On Screen Keyboard help

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Post On Screen Keyboard help

    I am new to Java GUI and I wanted to make an onscreen keyboard.
    I just need help regarding the caps lock button and the shift button..

    any ideas on to how to implement the caps lock and shift function?

    here's the code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
     
    @
    SuppressWarnings("serial")
    public class MainFrame extends JFrame {
            private JTextField textField;
            private PopUpKeyboard keyboard;
        public MainFrame() {
            super("pop-up keyboard");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            textField = new JTextField(20);
            keyboard = new PopUpKeyboard(textField);
     
            textField.addMouseListener(new MouseAdapter() {@
                Override
                public void mouseClicked(MouseEvent e) {
                    Point p = textField.getLocationOnScreen();
                    p.y += 30;
                    keyboard.setLocation(p);
                    keyboard.setVisible(true);
                }
            });
            setLayout(new FlowLayout());
            add(textField);
     
            pack();
            setLocationByPlatform(true);
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {@
                Override
                public void run() {
                    new MainFrame().setVisible(true);
                }
            });
        }
     
        private class PopUpKeyboard extends JDialog implements ActionListener {
            private JTextField textField;
     
            public PopUpKeyboard(JTextField textField) {
                    this.textField=textField;
                this.setResizable(false);
                this.getContentPane().setPreferredSize(new Dimension(1000, 250));
                this.setLocation(50, 50);
                keyboardVariables();
     
            }
     
            private void keyboardVariables() {
     
                        String firstRow[] = {
                            "~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "Back Space"
                        };
                        String secondRow[] = {
                            "Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "|"
                        };
                        String thirdRow[] = {
                            "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"
                        };
                        String fourthRow[] = {
                            "Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?"
                        };
                        String fifthRow[] = {
                            "Space"
                        };
     
                        JButton first[];
                        JButton second[];
                        JButton third[];
                        JButton fourth[];
                        JButton fifth[];
     
                setLayout(new BorderLayout());
                Font font;
                float size;
                JPanel jpNorth = new JPanel();
                JPanel jpCenter = new JPanel();
                JPanel jpKeyboard = new JPanel();
                JPanel jpNote = new JPanel();
                add(jpNorth, BorderLayout.NORTH);
                add(jpCenter, BorderLayout.CENTER);
                add(jpKeyboard, BorderLayout.SOUTH);
                jpNorth.setLayout(new BorderLayout());
                jpCenter.setLayout(new BorderLayout());
                jpKeyboard.setLayout(new GridLayout(5, 1));
                pack();
     
                first = new JButton[firstRow.length];
                JPanel p = new JPanel(new GridLayout(1, firstRow.length));
                for (int i = 0; i < firstRow.length; i++) {
                    JButton b = new JButton(firstRow[i]);
                    b.setPreferredSize(new Dimension(100, 50));
                    b.addActionListener(this);
                    first[i] = b;
                    p.add(first[i]);
                }
                jpKeyboard.add(p);
     
                second = new JButton[secondRow.length];
                p = new JPanel(new GridLayout(1, secondRow.length));
                for (int i = 0; i < secondRow.length; ++i) {
                    second[i] = new JButton(secondRow[i]);
                    second[i].addActionListener(this);
                    p.add(second[i]);
     
                }
                jpKeyboard.add(p);
     
                third = new JButton[thirdRow.length];
                p = new JPanel(new GridLayout(1, thirdRow.length));
                JToggleButton toggleButton = new JToggleButton("CapsLock");
                toggleButton.addActionListener(this);
                p.add(toggleButton);
                for (int i = 0; i < thirdRow.length; ++i) {
                    third[i] = new JButton(thirdRow[i]);
                    third[i].addActionListener(this);
                    p.add(third[i]);
                }
                jpKeyboard.add(p);
     
                fourth = new JButton[fourthRow.length];
                p = new JPanel(new GridLayout(1, fourthRow.length));
                for (int i = 0; i < fourthRow.length; ++i) {
                    fourth[i] = new JButton(fourthRow[i]);
                    fourth[i].addActionListener(this);
                    p.add(fourth[i]);
                }
     
                p.add(new JPanel());
                jpKeyboard.add(p);
                fifth = new JButton[fifthRow.length];
                p = new JPanel(new GridLayout(1, fifthRow.length));
                for (int i = 0; i < fifthRow.length; ++i) {
                    JButton b = new JButton(fifthRow[i]);
                                    fifth[i] = b;
                    fifth[i].addActionListener(this);
                    b.setPreferredSize(new Dimension(20, 10));
                    p.add(fifth[i]);
                }
                jpKeyboard.add(p);
            }
     
                    public void actionPerformed(ActionEvent e) {
                            String action = e.getActionCommand();
                            boolean capslock = false;
                            boolean shift = true;
                            if(action == "Back Space"){
                                    textField.setText(textField.getText().substring(0, textField.getText().length()-1));
                            }else if (action == "Tab"){
                                    textField.setText(textField.getText() + "\t");
                            }else if (action == "Space"){
                                    textField.setText(textField.getText() + " ");
                            }else if (action == "Enter"){
                                    textField.setText(textField.getText() + "\n");
                            }else if(action == "Shift"){
                                    shift = false; //here
                            }else if(action == "CapsLock"){
                                    textField.setText(textField.getText().toUpperCase()); //here
                            }
                            else{
                                    textField.setText(textField.getText() + action.toLowerCase());
                            }
                    }
        }
    }

    thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: On Screen Keyboard help

    Also posted at On Screen Keyboard help
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. using keyboard to move image
    By viper_pranish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2013, 10:53 AM
  2. how to use the keyboard up,down,right,left
    By carmz in forum AWT / Java Swing
    Replies: 9
    Last Post: February 24th, 2013, 07:11 AM
  3. Replies: 7
    Last Post: July 1st, 2012, 12:47 PM
  4. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  5. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM