Java Swing feedback button on a tablet touch screen - java

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.

Related

Java - How to "unhide" a JFrame

I am quite new to Java, but familiar with native Android dev so bear with me xD. I created an application that creates a JFrame. Then I set the closeOperation to: setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);.
It performs as expected, the frame is hidden and this is what I want (when I close). I need the application to keep on running (only once instance), because I am running a thread in the background that is performing an operation.
My actionListener on my button in my JFrame currently does this: setVisible(false);
My question is this, how can I maximize the JFrame again after it has been hidden? Would it be possible to display the frame when the user clicks on the minimized application in the task bar? Is there some type of listener that I need to implement?
Thanks in advance, any advice will be appreciated
UPDATE
For this solution to work correctly you need to do the following. Also have a look at XtremeBaumer's answer for this to make sense.
On JFrame creation setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);.
When you want to minimize the app (on click possibly) frame.setState(Frame.ICONIFIED);.
When you want to maximize the app again frame.setState(Frame.NORMAL); in windowDeiconified event.
One last thing, if you want to also minimize your app when the user clicks on the exit button (red x) add this to the windowClosing event frame.setState(Frame.ICONIFIED);.
this.addWindowListener(new WindowListener(){
#Override
public void windowActivated(WindowEvent e) {
}
#Override
public void windowClosed(WindowEvent e) {
}
#Override
public void windowClosing(WindowEvent e) {
setState(Frame.ICONIFIED)
}
#Override
public void windowDeactivated(WindowEvent e) {
}
#Override
public void windowDeiconified(WindowEvent e) {
this.setVisible(true);
//this should be what you want
}
#Override
public void windowIconified(WindowEvent e) {
}
#Override
public void windowOpened(WindowEvent e) {
}
});
i hope this solves your question. add it to your JFrame

Java 2D Game - MouseClicked & MouseMoved methods

Problem
I have two MouseEvent handlers (mouseClicked and mouseMoved), and they both work, but only separately.
If I click the mouse, the action gets processed fine. (bullet gets fired)
If I move the mouse, the action gets processed fine. (the sprite moves)
The problem occurs when I do both actions at the same time (moving mouse whilst clicking). The mouseMoved event goes through fine, but the mouseClicked event doesn't get called.
The below code is in my Game class constructor, which is extending JPanel.
this.addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent e){
//This creates a new bullet, and adds to an array to get drawn.
bullet = new Bullet(105, e.getY()+5, bulletTexture);
bulletsOnScreen.add(bullet);
e.consume();
}
});
this.addMouseMotionListener(new MouseAdapter(){
#Override
public void mouseMoved(MouseEvent e){
//This increments the sprites position on the screen.
player.setYPosition(e.getY()-50);
e.consume();
};
});
What I've tried
I have tried using SwingWorker objects to run the mouseMoved in a background thread, but the results are the same (bullet doesn't fire).
#Override
public void mouseMoved(MouseEvent e){
SwingWorker myWorker = new SwingWorker<Void, Void>(){
#Override
protected Void doInBackground() throws Exception {
player.setYPosition(e.getY()-50);
e.consume();
return null;
}
};
myWorker.execute();
};
I have also tried to check for a mouseClick within the mouseMoved method, again to no success.
public void mouseMoved(MouseEvent e){
if(e.getButton() == MouseEvent.MOUSE_CLICKED){
//Create bullet and add to array (but this never gets called)
}
player.setYPosition(e.getY()-50);
e.consume();
};
If anyone has any ideas or pointers, it would be great thanks.
Here's some code I cooked to have full working example of behavior you described.
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
System.out.println("Pressed");
}
#Override
public void mouseClicked(MouseEvent e) {
System.exit(1);
}
});
frame.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent e) {
System.out.println("Moved");
}
});
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
frame.setVisible(true);
}
});
}
}
As you will see when moving around frame you will get lots of "Moved" events.
Most interesting for you will be part that when you press mouse button down you will see "Pressed". If you release without moving there will be clicked event ( app will exit ). Closing app might be a bit extreme but we have to be sure that we don't miss that event.
But when you press down mouse button, hold it and move, you won't see any move event triggered. Then, when you release, you won't get clicked event fired either.
I'm not sure about this part but it looks like mouseClicked will be triggered only if mousePressed and mouseReleased event occur one after another.
In your game, when you click and move your mouse at the same time you basically do
mousePressed -> mouseMoved -> mouseReleased
but this doesn't fire mouseClicked as a result.
My advice on that would be that you handle mousePressed instead of mouseClicked or try to adapt MouseAdapter#mouseDragged method to your needs.

Java - MousePress Hold and Release event listener

hello is there a way mouse even that can Hold the mouse and release cause I can't find it on google.
so for example this image..
When the jTextBox is **** when he click the button, he see the words oops...
then after he release the click of mouse the jTextBox will back to **** again
I know this code already but the mouseevent is I only don't know
Yes. You will want to implement the MouseListener interface with a new class and add this new Listener against your button with the following;
button.addMouseListener(new YourMouseListener());
An example custom MouseListener might look like this.
class YourMouseListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
// Insert code to show password
}
#Override
public void mouseReleased(MouseEvent e) {
// Insert code to hide password again
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
I hope this helps.
You'll need a Robot object. This can do things as follows:
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
The Mousebutton is pressed until you do this:
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
This should do what you want.

MouseListener in Java Swing sometimes not respond

I've implemented right mouse click for open menu listener on my main Jframe, it works fine except one problem. One out of 5 (give or take) clicks it not responding, this can be very annoying for the user. Here is my code:
contentPane = new JPanel();
contentPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
{
//Do Stuff
}
}
});
Can you please help me
You won't get clicks from sub-components of contentPane.
I think your problem is that you have added things to your panel. When the user clicks at regions occupied by a sub-component, that sub-component get's the click event.
Quick fix: I would recommend you to add the same mouse listener to all sub-components.
You are not "clicking"
A click is when the mouse is pressed and release really quickly. If you are not careful you might get events for (for instance) "pressed, moved, released" instead of "clicked".
Quick fix: use mouseReleased event instead.
Use this Code instead:
private MouseAdapter listener = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (downer) {
downer = false;
if (new Rectangle(e.getComponent().getLocationOnScreen(), e.getComponent().getSize())
.contains(e.getLocationOnScreen())) {
downer = false;
// CODE
new Thread(new Runnable(){
public void run(){
//Your Listener code
}
}).start();
/// COde
}
}
}
boolean downer = false;
public void mousePressed(java.awt.event.MouseEvent e) {
downer = true;
}
};
This code only reacts if you press on the component and release on the component AND starts a new Thread for the custom task. This should work allways, because the AWT Thread isnt blocked with long calculations.

Java on a Mac: How do I detect when my application receives focus?

I looked through ApplicationListener, and they don't have it in there. On a Mac, it's when that application has the equivalent of focus; its menu is in the top menu bar.
Also, if you know this, could you tell me how my application can request to de-focus itself?
Implementations of windowActivated() and windowDeactivated() in WindowListener or WindowAdapter will tell you when a window is activated or deactivated. You don't need ApplicationListener for that.
Addendum: Although not required in this case, a transparent implementation of the additional functionality specified in ApplicationListener may be found in this example.
Addendum: See also How to Write Window Listeners.
Addendum: I think I see what you mean. In the example OSXAdapter, which uses -Dapple.laf.useScreenMenuBar=true, the menus disappear when the last window (HIDE_ON_CLOSE by default) closes. It's less than optimal, but the About… and Preferences menus remain in the application menu; choosing either restores the screen menu. Another possibility is to modify the dock menu in com.apple.eawt.Application.
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class WindowTest extends JFrame implements ActionListener,
WindowListener, WindowFocusListener, WindowStateListener {
public static final void main(String args[]) throws Exception {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new WindowTest("One");
new WindowTest("Two");
}
});
}
public WindowTest(String name) {
super(name);
this.setName(name);
this.setLayout(new GridLayout(0, 1));
createButton("Back");
createButton("Front");
createButton("Hide");
this.addWindowListener(this);
this.addWindowFocusListener(this);
this.addWindowStateListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
}
private void createButton(String name) {
JButton b = new JButton(name);
this.add(b);
b.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if ("Back".equals(s)) {
this.toBack();
} else if ("Front".equals(s)) {
this.toFront();
} else {
this.setExtendedState(JFrame.ICONIFIED);
}
}
#Override
public void windowOpened(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowClosing(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowClosed(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowIconified(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowDeiconified(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowActivated(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowDeactivated(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowGainedFocus(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowLostFocus(WindowEvent e) {
System.out.println(e);
}
#Override
public void windowStateChanged(WindowEvent e) {
System.out.println(e);
}
}
could you tell me how my application
can request to de-focus itself?
You can try:
frame.toBack();
If that doesn't work then you can iconify your application in which case focus should go to the previous application.
frame.setExtendedState(...);
The Java programming language is platform-independent. Rather than reading Apple's reference documentation, you should be using the official Java API Reference Documentation. There you will find documentation for JFrame, WindowListener, and WindowAdapter. You can register a WindowListener on a JFrame, using the addWindowListener function. The window listener may be used to intercept and handle a variety of window-related events including activated/deactived (which window is on top) or gained focus/ lost focus (which window will receive keyboard events). If you are supplying your own WindowListener and don't want to implement every single function, WindowAdapter is useful for that purpose as it implements WindowListener but provides empty definitions for each function. As for defocusing (in the sense that you mean), toBack may be used for that, while toFront does the opposite.
Edit
Most of this information was already given in previous posts; however, I added this to emphasize:
Java is a platform-independent language.
Java is a product of Sun Microsystems (now Oracle).
Consquently, using the official Java API Reference Documentation from Sun makes way more sense than relying on any reference documentation provided by Apple, because anything contained within the official API reference documentation will work on all platforms; whereas, anything from Apple's reference documentation may very well to be specific to Apple's implementation.
The reference documentation for JFrame from the official, authoritative reference documentation gives all the information necessary for answering the question (hence yet another reason to consult the official API reference documentation, rather than relying on Apple's documentation).

Categories