简体   繁体   中英

Java Swing feedback button on a tablet touch screen

Is it possible obtain a feedback from JButton in a Java Swing application on a tablet?

I am using Windows 8 and if I switch a physical mouse to the device, motion feedback from JButton is in the typical way, but if I use a finger, the feedback disappears. I have tried overriding methods customizing my inherited JButtons, and a estended etc., but I haven't hoped the goal... I guess it is related with when we touch the screen with a mouse, you only click a point on the screen, but if you touches with a finger, there are several pixels selected.

Any idea?

Thank you so so much!

Im not entirely sure what you mean by feedback, but I THINK the answer to the question your asking is no. Swing was never designed for that sort of interface. However if the feedback you are referring to is something like the button highlighting and swelling when clicked, this is usually something that should happen on its own. If as I suspect you are referring to a hover action being performed when youtouch but dont 'tap' the button, then there is likely no way for you to control that. As an alternative, if your application is not yet mature, you may want to consider switching from swing to JavaFX which uses CSS to give you a large amount of control over things like this.

I've got an acceptable solution. I will try to explain it as simple and complete as possible.

First of all, you have to use a JButton extended class like these:

    import java.awt.Color;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    import javax.swing.JButton;

    /**
     * Customized <code>JButton</code> to obtained a feedback user experience of movement.
     * @author Gabriel Moreno.
     */
    public class FeedbackTouchScreenButton extends JButton {

        public FeedbackTouchScreenButton() {
            super();

            this.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(final MouseEvent e) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Color bg = e.getComponent().getBackground();
                                e.getComponent().setBackground(Color.BLUE); // For example
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e1) {
                                    e1.printStackTrace();
                                }
                                e.getComponent().setBackground(bg);
                            }
                        }).start();
                } // mouseClicked()

                @Override
                public void mouseEntered(MouseEvent e) {}
                @Override
                public void mouseExited(MouseEvent e) {}
                @Override
                public void mousePressed(MouseEvent e) {}
                @Override
                public void mouseReleased(MouseEvent e) {}
            });
        } // FeedbackTouchScreenButton()

    } // FeedbackTouchScreenButton

When you customize the action performed of the concerned button, you will have to throw (carefully) another thread. For example:

    // ---
        btnExample.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(final java.awt.event.ActionEvent evt) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        btnExampleActionPerformed(evt);
                    } // run()
                }).start();
            }
        });
    // ---

This example apparently works. But actually only it seems... :-)

The reason is because on the tablet screen the 'onPressed' state of the component doesn't works with a finger like with a mouse pointer.

THE KEY: 'onClick' = 'onPressed' + 'onRelease'.

And 'onClick' is correctlty done on the touch screen. It is the moment when the trick is done, when you release your finger from the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM