Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a Java Swing application to Touch Screen

We have built a Point of Sale system and now we require to implement it to Touch screens? Do we need to change any code in turn to allow this to work.

And we are using the Keyboard to enter values - let's say quantity - Is there a java way of popping up a key board (like android) when I focus on a JTextField?

like image 903
Chan Avatar asked Jul 24 '12 03:07

Chan


People also ask

Does Java Swing work on mobile?

There is no way you can use swing in android, because android is not based on JavaSE, while swing is. android uses a special java that is designed to run on DVM . Even if their is no compatibility issue. Swing is used for desktop apps which differ in their UI completely from mobile apps.

Can Java Swing be used in web applications?

Webswing is a web server that allows you to run any Java Swing application inside your web browser, using only pure HTML5.

Is swing good for GUI?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.


2 Answers

Here is a simple example on how to implement a pop-up keyboard:

enter image description here

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class MainFrame extends JFrame
{
    private JTextField txt;
    private PopUpKeyboard keyboard;

    public MainFrame()
    {
        super("pop-up keyboard");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        txt = new JTextField(20);
        keyboard = new PopUpKeyboard(txt);

        txt.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                Point p = txt.getLocationOnScreen();
                p.y += 30;
                keyboard.setLocation(p);
                keyboard.setVisible(true);
            }
        });
        setLayout(new FlowLayout());
        add(txt);

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

        public PopUpKeyboard(JTextField txt)
        {
            this.txt = txt;
            setLayout(new GridLayout(3, 3));
            for(int i = 1; i <= 9; i++) createButton(Integer.toString(i));
            pack();
        }

        private void createButton(String label)
        {
            JButton btn = new JButton(label);
            btn.addActionListener(this);
            btn.setFocusPainted(false);
            btn.setPreferredSize(new Dimension(100, 100));
            Font font = btn.getFont();
            float size = font.getSize() + 15.0f;
            btn.setFont(font.deriveFont(size));
            add(btn);
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();
            txt.setText(txt.getText() + actionCommand);
        }
    }
}
like image 161
Eng.Fouad Avatar answered Oct 01 '22 22:10

Eng.Fouad


If you don't need multi-touch, the normal mouse drivers for use with most touch screen controllers will just have the touch-screen emulate a normal mouse where a finger touching the screen is emulated as a mouse click.

As for a virtual keyboard, there are crummy ones built into Windows and MacOSX but it would probably be best to build one into the application if you can.

If you need multi touch or have issues with specific touch screen controllers, there are a few options.

Your best bet in swing, at least on windows, seems to be this project: http://www.michaelmcguffin.com/code/JWinPointer/

JavaFX appears to have touch support, Intel has a tutorial: https://software.intel.com/en-us/articles/using-javafx-to-implement-multi-touch-with-java-on-windows-8-desktop. You might be able to get this working with swing somehow as there are methods to host Swing in JavaFX and JavaFX in Swing, you might look for other answers to accomplish interop between both.

There was project MT4J, but it seems to be defunct. It doesn't seem to work with Swing or JavaFX.

like image 34
Charlie Avatar answered Oct 01 '22 23:10

Charlie