Java JOptionPane.showConfirmDialog hot key - java

I am displaying a confirmation dialog in Java using JOptionPane.showConfirmDialog. The dialog shows a Yes No confirmation to the user. This is called as follows:
int result = JOptionPane.showConfirmDialog(sessionObjects.getActiveComponent(),
"Are you sure you want to exit?", "My App", JOptionPane.YES_NO_OPTION);
The question is as this is a simple confirmation, can the user press y for yes and n for no? At present the user has to click on the buttons?
Thanks,
Andez

There is another simpler possibility: you can press alt in a program and then release it in a same way:
public static void pressAlt(){
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ALT);
} catch (AWTException ex) {
Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void releaseAlt(){
try {
Robot r = new Robot();
r.keyRelease(KeyEvent.VK_ALT);
} catch (AWTException ex) {
Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex);
}
}
then when you call:
pressAlt();
int confirm = JOptionPane.showConfirmDialog(... bla bla bla ...
,JOptionPane.YES_NO_OPTION);
releaseAlt();
the behavior is exactly the same, as you desired...

You already have "hotkeys" (mnemonics) for the buttons: Alt+Y for "Yes" and Alt+N for "No".
You can also hit Tab to toggle between them and Space to press.

No, but you can create your own JDialog that could do that.

Look at Stopping Automatic Dialog Closing

Related

Selenium WebDriver, TestNG with Java

I am unable to Handle the Print window/popup when I click on a Print button on the Web application. I need to be able to either Close this Window, Click on Print or Cancel. I am not sure whether this is a pop-up or a window.
Could some one help me?
see whether any web elements are visible if you hover the mouse over the popup.
if web elements are visible then its web application pop up
if no web elements are visible then its windows popup
However you can ignore the popup by sending escape key. the following code will work. i just tried, it worked.
public class demo extends parent {
WebDriver driver = new FirefoxDriver();
#Test
public void launch() throws InterruptedException {
driver.get("https://www.google.co.in");
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement el = driver.findElement(By.xpath(".//*[#id='hplogo']"));
el.click();
Thread.sleep(10000);
el.sendKeys(Keys.CONTROL + "p"); // trying to invoke print pop up
Thread.sleep(10000);
r.keyPress(KeyEvent.VK_ESCAPE); //dismissing it by sending escape keys
}
}
hope you will get some idea here :)

When will the java.awt.Robot fail to click?

I use java.awt.Robot to perform some mouse behaviors on my PC. The code is simple like below:
import java.awt.Robot;
import java.awt.event.InputEvent;
public class RobotProxy {
public static void main(String[] args) {
// TODO Auto-generated method stub
RobotProxy robotProxy = new RobotProxy();
try {
robotProxy.foo();
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception there...");
}
}
public void foo() throws Exception{
Thread.sleep(3000);
Robot robot = new Robot();
robot.mouseMove(501, 296);
leftClick(robot);
robot.mouseMove(505, 296);
leftClick(robot);
robot.mouseMove(509, 296);
leftClick(robot);
}
public void leftClick(Robot robot) throws Exception{
Thread.sleep(1000);
System.out.println("before Click...");
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
System.out.println("after Click...");
}
}
You can find that I use the combination of java.awt.Robot.mousePress(InputEvent.Button1_MASK) and java.awt.Robot.mouseRelease(InputEvent.Button1_MASK) to perform the mouse left click behavior.
It works fine at most time but fails sometimes. For example, the left click behavior for a kind of software's check box will fail. I can make sure I send the click command to java.awt.Robot but just nothing happens. What's more incredible is that java.awt.Robot.mouseMove(int x, int y) still works in that situation.
PC's OS is Windows8.1
The software is not market available and it's just a Windows native app written by cpp. The button on the software can be clicked but not for check box.
If the situation makes you confused, pls just tell me when will the java.awt.Robot fail to click. Thanks for your help in advance.
The problem is there is no delay between the robot.mousePress and robot.mouseRelease commands.
Here is an example of what you could add in between the two to fix the issue
Thread.sleep(100); a delay for 100ms. about as fast as you can hear, click click

Java sockets - weird response

I'm doing a simple server/client communication for my tic tac toe game. All it goes through actionPerformed where I first setIcon to the pressed button, then proceed my performTurn method (there is some win checking), then send the coordinates of the pressed button to the client and then i wait for the other player to send me data about his turn. all it goes well, but......... the first method that should be made (setIcon) is actually done after receiving data from the other player. it goes like this: Game.performTurn, Server.send, Server.receive, setIcon. do anyone know what java issue is it, that the setIcon method doesnt go first? thx lot
public void actionPerformed(ActionEvent e) {
if (clickable && Game.getTurn()) {
clickable = false;
if (Board.player) {
setIcon(X);
Game.performTurn(x, y, Value.FIRST);
try {
Server.send(new ObjToSend(x, y));
} catch (IOException ex) {
Logger.getLogger(XOButton.class.getName()).log(Level.SEVERE, null, ex);
}
try {
ObjToSend bla = Server.recieve();
Board.buttons[bla.getX()][bla.getY()].setIcon(O);
Game.performTurn(bla.getX(), bla.getY(), Value.SECOND);
Game.turn = true;
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(XOButton.class.getName()).log(Level.SEVERE, null, ex);
}

How to create a graph window in java

I'm using this code
private void botaoGrafADMouseClicked(java.awt.event.MouseEvent evt) {
try {
boolean[] b=new boolean[8];
if (Caixa9.isSelected()) b[0]=true; else b[0]=false;
if (Caixa11.isSelected()) b[1]=true; else b[1]=false;
if (Caixa10.isSelected()) b[2]=true; else b[2]=false;
if (Caixa12.isSelected()) b[3]=true; else b[3]=false;
b[4]=false;b[5]=false;b[6]=false;b[7]=false;
final LineChartDemo1 demo = new LineChartDemo1("Leitura A/D",b,"outad.txt",4);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Comunicacao.class.getName()).log(Level.SEVERE, null, ex);
}
}
to call an graph interface. But, when I do this, every time I call the graph, it generates on new window and, if I close on of these windows, the whole program is closed.
I'd want to know what am I doing wrong. How Can I avoid this (I would post a printscreen, but, as new user, I can't, it is on http://i.stack.imgur.com/4JLxQ.png I think
Edit: Image
JFrame has a default close operation (i.e. what happens when you close the window using your window manager) of EXIT_ON_CLOSE. Use JFrame.setDefaultCloseOperation to set a different value.
I don't know what the class LineChartDemo1 is, but you could probably set it as the content of a JDialog and call setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE) on each dialog. This way, when the user closes the dialog, only that window will close, the others will remain open.
I created a new netbeans JFrame and made reference to it like this:
InterfaceGrafico minhaInterface = new InterfaceGrafico("Leitura I/O",b,"outio.txt",8);
where the arguments where the same to generate the graph. In this "InterfaceGrafico" class:
public InterfaceGrafico(final String title,boolean[] b, String nomeArquivo, int col) {
try {
initComponents();
final LineChartDemo1 demo = new LineChartDemo1("Leitura I/O", b, "outio.txt", 8);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(InterfaceGrafico.class.getName()).log(Level.SEVERE, null, ex);
}
}
That means, I just shifted the code to another JFrame. I also commented the public void run method . Now I can close each graph generated without closing the whole application and the other generated graphs. In the "LineChart1" class, I added this
public void windowClosing(final WindowEvent evt){
if(evt.getWindow() == this){
dispose();
}
}

java stop people from closing

hi i have a full screen program which i dont want people to close unless they have a password i have this code at the moment
public void windowClosing(WindowEvent arg0)
{
System.out.println("HERE");
String inputValue = JOptionPane.showInputDialog("Please input the closeword");
if (inputValue != "closeplz")
{
}
}
in the if statement i want it to stop the method so that the program doesent close. any help would be greatly aprecheated thanks ste
You have to call
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
on (or within) the JFrame instance. Then the frame will not close unless you do it manually, though windowClosing() will still be called. Inside it, you can then conditionally call
System.exit(1);
which will end the application. Be sure to do any necessary cleanup first.
Check out Closing an Applicaton for a simple class to help you with this. You would need to provide the custom close action that prompts the user for the password.
Using your simple example the code would be:
Action ca = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
JFrame frame = (JFrame)e.getSource();
String inputValue = JOptionPane.showInputDialog("Please input the closeword");
if (! inputValue.equals("closeplz"))
{
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
};
CloseListener cl = new CloseListener(ca);

Categories

Resources