How to implement a Java Swing application for a touch screen

We created the Point of Sale system, and now we need to implement it on touch screens? Do I need to change the code in turn for this to work.

And we use the keyboard to enter values ​​- let them say the quantity. Is there a java way to pop up the keyboard (e.g. android) when I focus on JTextField?

+6
source share
5 answers

Here is a simple example of a pop-up keyboard implementation:

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(); py += 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); } } } 
+3
source

If you don’t need multi-touch, the usual mouse drivers for use with most touch screen controllers will simply screen the touch screen with a normal mouse when a mouse click is emulated with a finger touching the screen.

As for the virtual keyboard, there are starches on Windows and MacOSX, but it's probably best to create one in an application if you can.

If you require multiple taps or problems with specific touch screen controllers, there are several options.

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

JavaFX seems to support touch, Intel has a tutorial: https://software.intel.com/en-us/articles/using-javafx-to-implement-multi-touch-with-java-on-windows-8- desktop . You may be able to get this to work with the swing, as there are methods for placing Swing in JavaFX and JavaFX in Swing, you can look for other answers to do the interaction between them.

There was a MT4J project, but it seems non-existent. It doesn't seem to work with Swing or JavaFX.

+2
source

You should be able to provide your virtual keyboard using something like JWindow and KeyboardFocusManager

0
source

We have implemented a custom Look-and-feel for our Swing application with touch support so that everything looks bigger (all buttons, check boxes, ..., even JTree instances), so that they are easy to modify using your finger.

Such a solution can save you the effort of transforming your entire user interface into a user-friendly interface.

0
source

You must change your user interface and interaction design, thereby changing the code. Check out the Windows UX recommendations for touch - https://msdn.microsoft.com/en-us/library/windows/desktop/dn742468.aspx

0
source

Source: https://habr.com/ru/post/921170/


All Articles